Game Object keeps flying up Y axis by itself

#pragma strict
var theBullet : Rigidbody;
var theBullet2 : Rigidbody;
var Speed = 5000;
var shots : int = 200;
var Error : AudioClip;
function Update () {
if(Input.GetAxis(“QFireOrange”))
{
var clone = Instantiate(theBullet, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3(0,0,Speed));

		Destroy (clone.gameObject, 2);
	}if(Input.GetAxis("FFireGreen"))
	{
		var clone2 = Instantiate(theBullet2, transform.position, transform.rotation);
		clone2.velocity = transform.TransformDirection(Vector3(0,0,Speed));
		
		if(theBullet2 == null){
		audio.PlayOneShot(Error);
		}
		
		Destroy (clone.gameObject, 2);
	}
	
	
	
	
	//guiText.text = "Shots Left:" + shots;
}

This is my script . As you can see when I press the QFire or FFire buttons( Q and F) a rigidbody is spawned and sent forwards across the Z axis . But when it spawns it goes forward and then flies up for no apparent reason . How can I fix this .

It also gives me a warning which I don’t understand :
The referenced script on this Behaviour is missing!
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
PrefabShooting:Update() (at Assets/Tutorial 5/Tutorial 5/PrefabShooting.js:11)

Are these two problems linked ?

2 Answers

2

I would suggest checking to see if the projectile actually has a rigidbody component. You may also want to check your variable settings for “Speed” and “shots” in the Inspector menu.

One thing that has solved physics problems for me is enabling kinematics on my rigidbody, so you should give that a try. Lastly, there may be a collision occurring early-on in the projectile’s launch. You may want to reposition the spawn point a bit further from the character controller.

Best of luck!

For your error, I think the Object your trying to Instantiate have change name, or folder or that you have forget to link theBullet and theBullet2 to a gameobject.

For your unexpected behavior, as mention in the Documentation:

In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour.

You should use AddForce