Question by
peacemr3102003 · Jul 04, 2020 at 07:45 PM ·
movementrigidbodyvelocitydirection
i'm trying to make an fps with rigidbody but the player isn't going according to it's direction
i want to use the rigidbody for the player movement and i'm creating an fps but the rigidbody's velocity doesn't go according to the player direction
{
[SerializeField, Range(10f, 1000f)]
float maxAcceleration = 100f;
float speed = 10f;
Vector3 desiredVelocity;
Vector3 velocity;
Rigidbody rigid;
private void Start()
{
rigid = GetComponent<Rigidbody>();
}
void Update()
{
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
Vector3 direction = Vector3.ClampMagnitude(input, 1f);
desiredVelocity = new Vector3(direction.x, 0f, direction.z) * speed;
}
private void FixedUpdate()
{
velocity = rigid.velocity;
float maxSpeedChange = maxAcceleration * Time.deltaTime;
velocity.x = Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
velocity.z = Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);
}
}
Comment
Your answer
Follow this Question
Related Questions
Determine the direction an object is actually moving? 0 Answers
Counteracting rigidbody velocity using AddForce? 0 Answers
Move NPC on triggerEnter 0 Answers
How to properly move a rigidbody character? 0 Answers
Player stuck when jumping into a wall 0 Answers