x


Problem with transform.position

Hello everyone ;)

I have this script:

var Speed        : float = 0;
var MaxSpeed     : float = 10;
var Acceleration : float = 10;
var Deceleration : float = 10;
var rotateSpeed = 25.0;
function Update () 
{
if(Input.GetKey("a"))
{
transform.eulerAngles.y -= rotateSpeed * Time.deltaTime;
}
if(Input.GetKey("d"))
{
transform.eulerAngles.y += rotateSpeed * Time.deltaTime;
}
if ((Input.GetKey ("s"))&&(Speed < MaxSpeed))
{
    Speed = Speed - Acceleration * Time.deltaTime;  
}
else if ((Input.GetKey ("w"))&&(Speed > -MaxSpeed))
{
    Speed = Speed + Acceleration * Time.deltaTime;
}
else
{
    if(Speed > Deceleration * Time.deltaTime)
{
        Speed = Speed - Deceleration * Time.deltaTime;
}
else if(Speed < -Deceleration * Time.deltaTime)
{
        Speed = Speed + Deceleration * Time.deltaTime;
}
else
{
    Speed = 0;
}
}

transform.position.z = transform.position.z + Speed * Time.deltaTime;
}

Simple yet quiet effectivt...

The problem is that I want my character to turn, wich I have done using transform.eulerAngles

The problem is that, my character moves along the "World axes" no matter how he turns he will still move in the world z-axes if I press "w" If he faces backwards ind a press w(forward key) he still moves along the world z direction.

So my question is is there a way to get my charcter move along his "Local axes" instead of the "World/global axes"

more ▼

asked Feb 26 '11 at 01:03 PM

OrangeLightning gravatar image

OrangeLightning
5.4k 47 57 112

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

First: Don't increment or decrement eulerAngles. If they are out of bounds [0..360] you get an error. Use Transform.Rotate instead.

And for the position use localPosition or Translate

more ▼

answered Feb 26 '11 at 01:13 PM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

Thank you got it to work now. Thank you...again ;)

Feb 26 '11 at 01:30 PM OrangeLightning
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1367
x885
x134
x134
x30

asked: Feb 26 '11 at 01:03 PM

Seen: 1151 times

Last Updated: Feb 26 '11 at 01:16 PM