- Home /
 
How to move as well as rotate the object to face the direction it is moving.
Hello, I just began coding and wanted to get a little experience by moving a 3D object in a plane which i managed to accomplish.Then i decided i wanted to rotate the object to face the direction it would move but the issue is that it would'nt move forward but only face that direction.If i deactivated the turn function from the script then it would move but not turn.I want to be able to have my basic 3D car face the direction it is moving. I managed to use Quaternion.Slerp() to interpolate between the 2 states.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CarMovement : MonoBehaviour {
 
     Rigidbody car;
     Vector3 movement;
     public float speed = 5.0f;
     // Use this for initialization
     void Start () {
         car = GetComponent<Rigidbody>();
     
     }
 
     // Update is called once per frame
     private void FixedUpdate()
     {
         float h = Input.GetAxisRaw("Horizontal");
         float v = Input.GetAxisRaw("Vertical");
         turn(h, v);
         Move(h, v);
         
       
     }
 
     void Move(float h , float v)
     {
         movement.Set(h, 0.0f, v);
         movement = movement.normalized * speed * Time.deltaTime;
         car.MovePosition(transform.position + movement);
     }
 
     void turn(float h, float v)
      {
 
     car.transform.rotation = Quaternion.Slerp(car.transform.rotation, Quaternion.LookRotation(movement), 0.15f);
      }
 
 }
 
 
 
              
               Comment
              
 
               
              Your answer