launched projectile not accepting speed variable

Hello (First post on this forum! Woot!) I am having an issue with launching a projectile after just reading through the FPS shooter walk through. The projectile launches just fine, except it won’t accept the speed setting if it’s a variable. It will accept me entering in the number raw, but that’s all. I will post the different codes I’ve tried. The “* -1” at the end is because the model I’m using was backwards, so I have to reverse the directions on everything.

var projectile:Rigidbody;
var tspeed = 125;


function Update()
{
	if (Input.GetButtonDown("Fire1"))
	{
		var instantiatedProjectile:Rigidbody = Instantiate(projectile,transform.position,transform.rotation);
		instantiatedProjectile.velocity = transform.forward * tspeed * -1;
		Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
	}

}
var projectile:Rigidbody;
var tspeed = 125;
var vspeed = new Vector3(0,0,0);


function Update()
{
	if (Input.GetButtonDown("Fire1"))
	{
		var instantiatedProjectile:Rigidbody = Instantiate(projectile,transform.position,transform.rotation);
		vspeed = Vector3(0,0,tspeed);
		instantiatedProjectile.velocity = transform.TransformDirection(vspeed);
		Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
	}

}
var projectile:Rigidbody;
var tspeed = 200;


function Update()
{
	if (Input.GetButtonDown("Fire1"))
	{
		var instantiatedProjectile:Rigidbody = Instantiate(projectile,transform.position,transform.rotation);
		instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0,0,tspeed));
		Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
	}

}

the only one that’s worked is the following.

var projectile:Rigidbody;


function Update()
{
	if (Input.GetButtonDown("Fire1"))
	{
		var instantiatedProjectile:Rigidbody = Instantiate(projectile,transform.position,transform.rotation);
		instantiatedProjectile.velocity = transform.forward * 125 * -1;
		Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
	}

}

If someone could help explain why the projectile won’t accept the variable so I can fix the error, that would be great, thank you.

I don’t know if this is the problem but one thing to keep in mind when using a command like

var tspeed = 125;

is its going to give an int

which for something like projectile calculation I assume you would probably want a float

which you would get by going

var tspeed = 125.0;

or better yet

var tspeed : float = 125.0;

thanks for the information. I left out some information by accident because it happened so many tries ago that I forgot about it.

When using a float, it will at least move forward, but it disregards what the variable is and goes at a set speed.

so without a float as my var, it sits still.
with a float, it moves forward, but tspeed = 1 and tspeed = 100 gives the same speed for the projectile. It seems to be using 400 as it’s speed based on the speed of the launcher.

Those are the exact same thing actually. The second one is just longer. :wink: In any case, the int will be converted to a float, which takes a tiny bit of extra time, but will work fine. One thing I can think of is to try using much larger values; small values for the force don’t have much effect with a mass of 1.

–Eric

Do you see the value in the inspector? , try changing it there if so. I sometimes has “problem” that the inspector saves the value on a variable and how much i change it in the script it takes the “old” value from the inspector.

try :
private var tspeed = 125.0;

or

@System.NonSerialized
var tspeed = 125.0;