- Home /
Location and Rotation not Working
I'm simply trying to move an object on the local Z axis with the W and S keys, so I used transform.localPosition instead of transform.position which gave me the same result :/. The other problem is the rotation which only rotates for like 3 seconds then stops why doesn't this move my object on the local axis and what is wrong with the rotation?
var speed : float = 0.1;
function Update(){ //forward if(Input.GetKey("w")){ transform.localPosition.z += speed; } //backward if(Input.GetKey("s")){ transform.localPosition.z += -speed; } //left if(Input.GetKey("a")){ transform.rotation.y += -speed; } //right if(Input.GetKey("d")){ transform.rotation.y += speed; } }
Thanks
Answer by tool55 · Jan 30, 2011 at 11:39 PM
transform.localPosition.z is meant to be read only or for specific placement of an object, not for translation. You're going to get very inconsistent results doing it the way you've shown above.
What you want is transform.Translate (0,0, speed*Time.deltaTime) Just change your speed variable from + to - to change the direction. There are good examples in the scripting reference.
Same thing for rotation: transform.Rotate (X,Y,Z);
The following is directly from the scripting reference:
// A very simplistic car driving on the x-z plane.
var speed : float = 10.0; var rotationSpeed : float = 100.0;
function Update () { // Get the horizontal and vertical axis. // By default they are mapped to the arrow keys. // The value is in the range -1 to 1 var translation : float = Input.GetAxis ("Vertical") speed; var rotation : float = Input.GetAxis ("Horizontal") rotationSpeed;
// Make it move 10 meters per second instead of 10 meters per frame...
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
// Move translation along the object's z-axis
transform.Translate (0, 0, translation);
// Rotate around our y-axis
transform.Rotate (0, rotation, 0);
}
Actually that's not true, if position is meant to be read only they would have made it read only. The Translate function just do exactly the same thing as transform.position += transform.forward speed Time.deltaTime.
You can even set the eulerAngles manually without any problems but here you have to keep the angle within the right range or it will fail. It's all explained in the scripting reference.
Answer by Bunny83 · Jan 31, 2011 at 12:18 AM
Two things:
First rotation or localRotation are Quaternions but you used them like eulerAngles.
As tool55 mentioned don't increase eulerAngles because setting the angles to wrong values will fail. To rotate a transform use a Quaternion or what will be easier in your case (also like tool55 said) the Rotate function
transform.Rotate(0,0,rotationSpeed * Time.deltaTime);
rotationSpeed is in degrees per sec.
If your object isn't a child localPosition equals position. That means it's the position in world space and increasing z will move it on the global z axis. If it is a child position.z will represent the position in the local space of your parent but not in your own local space.
To move along the own local-z-axis you can use transform.forward which is a vector that points in the z axis of your transform. Just use that vector and add it to your position like:
transform.position += transform.forward * moveSpeed + Time.deltaTime;
moveSpeed is in Units per sec. Which should be meters.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to smoothly rotate an object through just one axis 1 Answer
My Zombie Model is coming at me sideways with the AI 1 Answer
Rotate an instantiated GameObject towards another GameObjects pivot point 1 Answer
Calc angle. 1 Answer