- Home /
Player does not Jump Issue(Unity3D)
This is my first character control script.
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.
void FixedUpdate ()
{
Vector3 vel = rigidbody.velocity;
vel.x = Input.GetAxisRaw("Horizontal") * MaxHorSpeed;
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded()){
vel.y = jumpSpeed;
}
}
Answer by Lunatix · Dec 05, 2014 at 03:16 PM
Your player might not move because of surface friction.
Try to use the following code snippet to move your character:
var movement = Vector3.forward * movementFactor * Time.deltaTime;
var desiredPosition = gameObject.position + movement;
rigidBody.MovePosition(desiredPosition);
I also would suggest to use some lerping to smooth the start end end of movement motion.
Brief description from docs: "Use $$anonymous$$ovePosition to prevent rigidBodies from teleporting". This method will "step" your rigidBody to the desired position and takes care of collisions.
http://docs.unity3d.com/ScriptReference/Rigidbody.$$anonymous$$ovePosition.html
thanks all lot for the documentation but it does have a problem with colliders its works well when speed of player is slow but when the speed is increased the player passes through the collider ignoring it and also my player is type of a car and using $$anonymous$$ovePosition gives a sudden movement in forward direction where as addforce slowly and gradually increases the speed of the vehicle.
Why didn't you mention that the player drives car before? $$anonymous$$aybe you should update your question.
$$anonymous$$aybe the air friction is set much to high, also, it is not always recommended to use velocitiy directly (in your code, the velocity is taken before addforce is called!) and why the clamping?
Ins$$anonymous$$d of clamping, just stop adding force when the velocity is big enough. One more hint: velocity is applied in the direction you are heading, setting "y" directly can lead to wrong movement.
For example, if you want to push another character, one can calculate the "push" direction by using: character.transform.rotation * Vector3.forward * throwBackAmount;
Your answer
Follow this Question
Related Questions
Adding force to forward direction 1 Answer
(C#) How would I go about making physics like F-Zero GX's? 1 Answer
fast forward physic vehicles 0 Answers
Tearing Polygon as if Paper? 1 Answer
2D 360 degress platformer example needed 0 Answers