transform.localPosition help

Hello,

I would liek to start out by explaining that i am New to Scripting.

I am trying to make a ‘Character controller’, I know there is one in unity already, But it is a challenge to create my own one, and it wil help me pick things up along the way,

I have done this code and hoped it would work, But it just teleports me to X:0 Y:0 Z:5e-05

I thought that this code would move the box along the local Z axis at 5 unity per second, Apparently i was wrong.

Please help me figure this out, Thanks!

var normSpeed : float = Time.deltaTime * 5;
var sboostSpeed : float = Time.deltaTime * 8;


function Update () {

		if(Input.GetKeyDown ("w")){
			transform.localPosition = Vector3(0, 0, normSpeed);
		}

}
  1. If you are using a CharacterController, use CharacterController.Move or CharacterController.SimpleMove
  2. If you are not using a CharacterController, use transform.Translate.
  3. Use Input.GetAxis rather than GetKeyDown
  4. Time.deltaTime*5 = 5e-05. That is why it is moving you to that position.
  5. Multiply your movement speed by Time.deltaTime every frame. Not when you declare the variable. It is constantly changing.

Couple of mistakes:

var normSpeed : float = Time.deltaTime * 5;
var sboostSpeed : float = Time.deltaTime * 8;

Should be:

var normSpeed : float = 5;
var sboostSpeed : float = 8;

Time.deltaTime changes every frame - you can’t simply call it once and save it (which is what you are doing here).

transform.localPosition = Vector3(0, 0, normSpeed);

Should be:

transform.localPosition += Vector3(0, 0, normSpeed) * Time.deltaTime;

or

transform.localPosition += transform.forward * normSpeed * Time.deltaTime;

If you want to move, you have to add the speed you moved, not simply move to the speed you moved (which doesn’t make much sense!). I also showed an appropriate usage of Time.deltaTime.

Thanks!, But

transform.localPosition += Vector3(0, 0, normSpeed) * Time.deltaTime; moves me on the Worlds Z axis, doesn’t localPosition mean the the Vector3 in relation to the Object, and not World?

Nope, it is in relation to the parent transform.

Try this:

transform.position += transform.forward * Input.GetAxis("Vertical") * normSpeed * Time.DeltaTime;

(Prob. got spell errors).

What does the += mean?

Nope, it is in relation to the parent transform.

Try this:

var normSpeed : float = 5;

function Update () {
transform.position += transform.forward * Input.GetAxis("Vertical") * normSpeed * Time.deltaTime;
}

i tried Input.GetKeyDown, but then i get the error “Operator ‘*’ cannot be used with a left hand side of type ‘UnityEngine.Vector3’ and a right hand side of type ‘boolean’” ?

Think about it simply in 1 dimention.

I am at 10.

I move 1.

position += distance traveled

(new) position = position + distance traveled

(new) position = 10 + 1

My position is now 11.

Exactly what it says. You cannot times a Vector by a boolean. Use the script reference and take a look at the result of Input.GetAxis and Input.GetAxisDown.

There is no GetAxisDown because it is a Axis, Not a button?, i want to use W to move, not “Vertical” Axis?

I’m probably completly wrong, so apologies

This is a simple example showing GetAxis.

Try the W S (Up Arrow) and (Down Arrow) keys.

If you still want to do it on just w:

var normspeed = 15;

function Update(){
if (Input.GetKey("w")){
transform.position += Vector3(0, 0, normSpeed) * Time.deltaTime;
}
}

Thanks, But when i use

transform.position += transform.forward * Input.GetAxis(“Vertical”) * normSpeed * Time.deltaTime; it moves once when i press it, How would i make it keep moving while i hold it?

It already keeps moving while you hold it, see the demo.

Not in my game, Here’s the code

 var normSpeed : float =  5;
var boostSpeed : float =  8;


function Update () {

		if(Input.GetKeyDown ("w")){
			transform.position += transform.forward * Input.GetAxis("Vertical") * normSpeed * Time.deltaTime;

		}

}

In that scenario get rid of Input.GetAxis.

Why are you checking for Input twice?

Futhermore research the difference between GetKey() and GetKeyDown().

Sorry i Pasted the wrong code, i was Undoing changes i made and i didn’t go back all the way so you got Half of one code and Half of another there

The problem was i left another Code attached to the object that seemed to be conflicting, Silly me