Moving an Object Along its Local Axis

I am spawning a grenade from the the camera object. I would like to move the grenade upon the camera’s z-axis so that they will fly forward, away from the player.

Where would I go after this to apply force to it along the camera’s z-axis?

var explosive : Transform;
private var once : boolean = true;
function Update () {
	if(Input.GetAxis("Fire2")  once == true){
		Grenade = Instantiate(explosive, transform.position, transform.rotation);
		once = false;

	}
	if(Input.GetAxis("Fire2") == 0){
		once = true;
	}
}

You can try something like this (just an idea)…

Transform cam = Camera.main.transform;
Vector3 cameraRelativeRight = cam.TransformDirection(Vector3.forward);
Grenade.rigidbody.AddForce( cameraRelativeRight , ForceMode.Inpulse);

So it should look like this?

var explosive : Transform;
private var once : boolean = true;
var cam = Camera.main.transform;
function Update () {
	var cameraRelativeRight = cam.TransformDirection(Vector3.forward);
	if(Input.GetAxis("Fire2")  once == true){
		Grenade = Instantiate(explosive, transform.position, transform.rotation);
		Grenade.rigidbody.AddForce( cameraRelativeRight , ForceMode.Impulse);
		once = false;

	}
	if(Input.GetAxis("Fire2") == 0){
		once = true;
	}
}

You can use the Grenades local axis, so that if you choose a new target later it will be easier to modify/update.

So

Grenade = Instantiate(explosive, transform.position, transform.rotation);
Grenade.rigidbody.AddForce( Grenade.transform.forward, ForceMode.Impulse);

The grenade will always move down it’s z axis no matter how it’s instantiated, no more need for cameraRelativeRight. All transforms have a .up, .right, and .forward to get local axis (so cam.forward would also work, and be faster than cam.TransformDirection(Vector3.forward))

Thanks! That’s EXACTLY what I wanted (which is why I made the instantiated grenade a variable in the first place)

Now I’m getting this:


for this:

var explosive : Transform;
private var once : boolean = true;
function Update () {
	if(Input.GetAxis("Fire2")  once == true){
		Grenade = Instantiate(explosive, transform.position, transform.rotation);
		Grenade.rigidbody.AddForce( Grenade.transform.forward, ForceMode.Impulse);
		once = false;

	}
	if(Input.GetAxis("Fire2") == 0){
		once = true;
	}
}

Those are unusual, it seems to be some sort of threading issue.

I can’t help much with those but double check that you decalre your Grenade variable somewhere;

var Grenade : Transform = …

Otherwise it could be having some unusual casting or autoinstantiating issues (allowing Unityscript to determine variable creation and casting is usually a bad idea)

using this

var Grenade : Transform = Instantiate(Grenade, transform.position, transform.rotation);

did not work at all.

Great. So what do I do? (I am also getting other errors in Unity as described in this thread)

I’m sorry I’m not sure, are you doing any threading at all? Are you calling it within an Editor script?

As for the other error, it looks like MyDetonator is returning a null reference you’ll have to talk to the person who made the Detonator script or figure out why MyDetonator is null.

Great. No. I am not calling this script within an editor script. Should I?

The last one and most important means that you use to have a GameObject with some script, that now is missing. Perhaps you’ve change its name or deleted.

Remember when you select an error message, you can se its origins by double click on them.