- Home /
why is my player deceleration script not working?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playermove2 : MonoBehaviour { [SerializeField] private int jumpsleft; [SerializeField] private int jumpsmax; [SerializeField] private bool canjump; [SerializeField] private float jumpheight; [SerializeField] public float movementspeed; [SerializeField] private AnimationCurve acceleration; [SerializeField] private float deceleration; Vector3 vect1; Vector3 vect2; public Rigidbody Playercontroller; private Vector3 velocityplaceholder = Vector3.zero; private float speed;
public void Awake()
{
Playercontroller = gameObject.GetComponent<Rigidbody>();
}
private void Update()
{
float inputVertical = Input.GetAxisRaw("Vertical");
float inputHorizontal = Input.GetAxisRaw("Horizontal");
if (inputHorizontal == 0)
{
velocityplaceholder = transform.InverseTransformDirection(Playercontroller.velocity);
vect1.x = velocityplaceholder.x;
vect2.y = velocityplaceholder.y;
Playercontroller.AddForce(Playercontroller.transform.TransformVector(deceleration * vect1));
}
if (inputHorizontal == 0 && inputVertical == 0)
{
return;
}
Vector3 verticalForceToAdd = Playercontroller.transform.forward * inputVertical * movementspeed * Time.deltaTime;
Vector3 horizontalForceToAdd = Playercontroller.transform.right * inputHorizontal * movementspeed * Time.deltaTime;
Playercontroller.AddForce(verticalForceToAdd + horizontalForceToAdd);
}
}
Your answer

Follow this Question
Related Questions
Player not moving due to static instance : is dynamic instancing a thing ? 1 Answer
Best way to move a character in a 2d platformer? 1 Answer
How do I create a rubber band effect on a camera forward movement? 1 Answer
How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers
Transform position of an object at particular angle/direction 3 Answers