0
\$\begingroup\$

This is my first character control script.
Where a cube is my player and the objective the i want to achieve with my script are
1.the cube moves in horizontal axis till the button is pressed once release the cube should stop(done)

2.the cube should has max vertical speed when the button is press the cube should move in vertical direction and try to reach the max-speed but when the button is released the cube should move at that speed continuous unless an opposite force is applied(done)

3.the cube should be able to jump (failed)

I want to make the cube jump but it just sticks to the ground. When gravity is turn on it jump but does not move forward and when gravity is turn off it moves forward but when pressed Jump it does not come down continuously move up. if anyone have played classic skyroads game am try to make that but a simple one without using raycast etc.

void FixedUpdate ()
{
    shipvelo = rigidbody.velocity;
    shipmagno = rigidbody.velocity.magnitude;


    Vector3 vel = rigidbody.velocity;
    vel.x = Input.GetAxisRaw("Horizontal") * MaxHorSpeed;

    if (Input.GetKeyDown(KeyCode.Space) && IsGrounded()){
        vel.y = jumpSpeed;
        print("I am on");
    }

    rigidbody.velocity = vel;


    rigidbody.AddForce (Vector3.forward * Input.GetAxis("Vertical") * forSpeedMul);
    rigidbody.velocity = new Vector3(Mathf.Clamp(rigidbody.velocity.x, -horizantalClamp, horizantalClamp),0,Mathf.Clamp(rigidbody.velocity.z, -VerticalClamp, VerticalClamp));
}
\$\endgroup\$
  • \$\begingroup\$ Please do not edit your post after it's been answered to substantially change the question. \$\endgroup\$ – MichaelHouse Dec 7 '14 at 15:46
  • \$\begingroup\$ You've edited the code and question to change what's being asked, I've reverted that. I suggest you accept the below answer as correct (because it apparently helped you remove that bit of code from your update), then ask a new question with the details of the problem you have now. Please ensure your new question contains all the information you want before it's answered. \$\endgroup\$ – MichaelHouse Dec 11 '14 at 7:48
  • \$\begingroup\$ My dear friend the answer didn't helped me and it did not worked all so I edited and removed to part of the code that was not necessary because the code that i want is not present consent. it is full of bug \$\endgroup\$ – Abdullah Sorathia Dec 11 '14 at 13:31
1
\$\begingroup\$

The second time you're setting the velocity of the rigidbody you pass in 0 for the y coordinate, so it will never move in the y axis.

You should really only be setting the velocity of your rigidbody when you jump (as Unity3D recommends), like so:

if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
{
    Vector3 jumpVel = rigidbody.velocity + Vector3.up * jumpspeed;
    rigidbody.velocity = jumpVel;
}

and the rest of the movement with forces, for more realistic looking movement.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.