1 Replies - 6157 Views - Last Post: 25 June 2013 - 11:30 AM Rate Topic: -----

#1 LockOut   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-June 13

Using Unity Game Engine. Fixed Shooting Directing in C#

Posted 25 June 2013 - 07:27 AM

Hey guys,

first I want to say that I am on these fourms and not Unity is well its not working all that well!

So anyway. I am making a simple top down shooting game. I am using C# to write out a script for a shooting gun. the bullet works great! But when the GameObject moves around the bullet will continue to go toward the z axis but not the gameObjecs. If that makes sense!

here is the line of code for the BulletsAI

using UnityEngine;
using System.Collections;

public class bulletAI : MonoBehaviour {
	public GameObject player;
	public float speed;

	// Use this for initialization
	void Start () {
		Vector3 newVelocity = Vector3.zero;
		newVelocity.z = speed;
		rigidbody.velocity = newVelocity;
	}
	
	// Update is called once per frame
	void Update () {
		Vector3 newPosition = transform.position;
		newPosition.z += speed * Time.deltaTime;
		player.transform.position = newPosition;
	
	}
}



and here is the Shoot script

using UnityEngine;
using System.Collections;

public class shoot : MonoBehaviour {
	
	public GameObject bullet;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.Space))
		{
		Instantiate(bullet, transform.position, transform.rotation);
	}
}
}


I am not sure if this will work the same since I am using Unity. I come from Website development using HTML and CSS so C# is kinda new to me!

Any

Thanks Hope that I can get this sorted!

Is This A Good Question/Topic? 0
  • +

Replies To: Using Unity Game Engine. Fixed Shooting Directing in C#

#2 optix212   User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 540
  • Joined: 10-October 09

Re: Using Unity Game Engine. Fixed Shooting Directing in C#

Posted 25 June 2013 - 11:30 AM

You want the bullet to always be following a GameObject?

You should look into the Transform.LookAt() function in the Unity API, and also you should look into rigidbody.AddForce() instead of adding newPosition to your position every frame, as that could cause some serious memory issues.

Something like

public float speed = 10.0f;
public GameObject player;

void Update()
{
Transform.LookAt(player.transform.position);
this.gameObject.rigidbody.AddForce(Vector3.Forward * speed * Time.deltaTime);
}



Also, be wary that 99% of people on this forum aren't going to know what you are talking about when it comes to Unity, because most of these people have never dealt with the Unity API. In other words, the regular C# language, which is what we use here, does not contain a definition for GameObject, Transform, etc.. Those are all functions that are specific to Unity.

Further, if you are using a rigidbody on your bullet (I'm assuming it's to simulate the effects of gravity on the bullet), you should put all of the code dealing with its Transform and movement into the function FixedUpdate, like so:

public float speed = 10.0f;
public GameObject player;

void FixedUpdate()
{
Transform.LookAt(player.transform.position);
this.gameObject.rigidbody.AddForce(Vector3.Forward * speed * Time.deltaTime);
}



FixedUpdate is called once every fixed framerate frame. To fully understand the differences between Update and FixedUpdate, you should check out the link below:
http://answers.unity...fixedupdat.html

Note: When making video games, it is VERY easy for your data to end up somewhere it shouldn't have, or for your data to get changed somehow. This is why it is very important to encapsulate your variables.

If you've read the article on encapsulation, then you should know the final outcome of your bullet script would be something like:
private float speed = 10.0f;
public GameObject player;

public float Speed()
{
  get
    {
     return speed;
    }
  set
    {
     speed = value;
    }
}

void FixedUpdate()
{
Transform.LookAt(player.transform.position);
this.gameObject.rigidbody.AddForce(Vector3.Forward * speed * Time.deltaTime);
}



Now you can access your private speed variable through the public Speed() method that we have made.

This post has been edited by optix212: 25 June 2013 - 11:51 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1