- Home /
How to make a jump and a rotation using javascript?
Hello, I am a rookie in unity i was trying to make a moving car, I am using the following command in javascript to make it "jump n rotate" but the movement is irregular and sometimes it even ends up piercing right through my terrain please help!Am i writing something wrong ??
**if (Input.GetKey (KeyCode.Space) ) { transform. Rotate ( rotation_speed*Time.deltaTime, 0.00 , rotation_speed*Time.deltaTime);
transform.Translate (Vector3 (0.00, 3.00,0.00) Time.deltaTime*5.00); }*
Answer by robertbu · Mar 18, 2013 at 06:20 AM
You've not explained much about your project, but since you write "piercing right through my terrain," I'm guessing you have a Rigidbody. Typically Rigidbodies need to be moved through force, not transform.Translate() and transform.Rotate(). For exmaple:
rigidbody.AddForce(Vector3.up*upForce*Time.deltaTime);
rigidbody.AddTorque(Vector3.up*angularForce*Time.deltaTime);
Note how much force you need will depend on the mass of the object. If your car does not seem to move, greatly increase upForce and angularForce.
If instead you have a character controller, take a look at CharacterController.Move() and CharacterController.MoveSimple().
Answer by Saraki · Mar 18, 2013 at 05:49 PM
Thank you for your answer robertbu i tried using your commands and it really helped !! I used rigidbody for gravity. What i don't get is why in the video that i used as tutorial the source code was the same as mine and its car was moving perfectly while mine is trembling and if i increese my speed it pierces the terrain. My project is not much just a cube with four cylinders attached under it moving on a larger cube and i followed all the steps in the video in the end i only had to change a bit the numbers in my source code to fix the movement a bit. This is my source code
pragma strict
public var forward_speed : int = 5;
public var backward_speed : int = 5;
public var rotation_speed : int = 80;
function Start () {
}
static var speed : int = 5;
function Update () {
if (Input.GetKey (KeyCode.UpArrow)) transform.Translate (Vector3(1,-0.5,-0.55) * Time.deltaTime*forward_speed);
if (Input.GetKey (KeyCode.DownArrow)) transform.Translate (Vector3(-1,-0.005,0.5) * Time.deltaTime*backward_speed);
if (Input.GetKey (KeyCode.LeftArrow)) transform.Rotate (0.0 , -rotation_speed * Time.deltaTime , 0.0 );
if (Input.GetKey (KeyCode.RightArrow)) transform.Rotate (0.0 , rotation_speed * Time.deltaTime , 0.0 );
if (Input.GetKey (KeyCode.Space) ) { rigidbody.AddTorque (Vector3.up* angularForce* Time.deltaTime);
rigidbody.AddForce ( Vector3.up * upForce *Time.deltaTime); }
}
[1]: http://www.youtube.com/watch?v=kE8nZL5FdQc
Your answer
Follow this Question
Related Questions
Slerp Problem?: Enemy in Constant Rotation! 1 Answer
using a custom rotation in "Instantiate" 4 Answers
Smooth rotation in 90° increments 0 Answers
How To Check If X Rotation Is Between Two Numbers?? 1 Answer
space flying system 3 Answers