- Home /
Player won't move right or left
Im following a tutorial on youtube and the character was moving around fine with W,A,S,D until I had to change something in the script for jumping and I must have messed up something because now I can't go right or left anymore.
var walkAcceleration : float = 5;
var walkDeacceleration : float = 5;
@HideInInspector
var walkDeaccelerationVolx : float;
@HideInInspector
var walkDeaccelerationVolz : float;
var cameraObject : GameObject;
var maxWalkSpeed : float = 20;
@HideInInspector
var horizontalMovement : Vector2;
var jumpVelocity : float = 20;
@HideInInspector
var grounded : boolean = false;
var maxSlope : float = 60;
function Update ()
{
horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if (horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;
if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && grounded){
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);
}
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical")* walkAcceleration);
if (Input.GetButton("Jump") && grounded)
rigidbody.AddForce(0,jumpVelocity,0);
}
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
function OnCollisionExit ()
{
grounded = false;
}
Answer by Starwalker · Nov 10, 2013 at 09:00 AM
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * >>>Time.deltaTime<<<<, 0, Input.GetAxis("Vertical")* walkAcceleration);
Your multiplying your horizontal force by Time.deltaTime which is a small value, which makes your horizontal force close to zero.
Try this, replace:
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical")* walkAcceleration);
Your answer
![](https://koobas.hobune.stream/wayback/20220613121553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why does my fps controller's motion get disabled after it collides with an enemy cube? 0 Answers
How to create a path for the player in a terrain 0 Answers
Why Won't it jump? 1 Answer
Rigidbody(.addForce) doesn't work with Physic material 1 Answer
I'm a bit new but when my camera rotates with the player model to look around it kind of jitters 0 Answers