- Home /
 
               Question by 
               Bogdan003 · Oct 24, 2019 at 04:42 PM · 
                c#movementplayermovement script  
              
 
              How can i smooth out the rotation?
I really hope u will help me as im struggling for 2 days
sing System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;
public class CharController : MonoBehaviour { [SerializeField] float moveSpeed = 4f; Vector3 forward, right; void Start() { forward = Camera.main.transform.forward; forward.y = 0; forward = Vector3.Normalize(forward); right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward ; }
 void Update()
 {
     if (Input.anyKey)
         Move();
     //if (Input.GetKeyDown(key:Space))
     
 }
 public void Move()
 {
     Vector3 directiom = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
     Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
     Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("Vertical");
     Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
     transform.forward = heading;
     transform.position += rightMovement;
     transform.position += upMovement;
 }     
}
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Lawlets · Oct 25, 2019 at 10:35 AM
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CharController : MonoBehaviour
 {
   [SerializeField] float moveSpeed = 4f;
   [SerializeField] float rotationSpeed = 4f; // use to manage the speed of your rotation
   Vector3 forward, right;
   void Start()
   {
     forward = Camera.main.transform.forward;
     forward.y = 0;
     forward = Vector3.Normalize(forward);
     right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
   }
   void Update()
   {
     if (Input.anyKey)
       Move();
 
   }
   public void Move()
   {
     
     Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 
     // Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
     Vector3 rightMovement = right * moveSpeed * Time.deltaTime * direction.x;
     // Don't use Input.GetAxis("Horizontal"), you already have the value store in the direction vector.
     // It's cost you more to call the Input.GetAxis function that use data from your direction vector.
 
     // Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("Vertical");
     Vector3 upMovement = forward * moveSpeed * Time.deltaTime * direction.z;
     // Same thing here
     
     
     Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
 
     transform.forward = Vector3.Lerp(transform.forward, heading, Time.deltaTime * rotationSpeed);
     // If you want to smooth thing, you generaly use the Lerp function
     // The lerp function do an linear interpolation between 2 vectors/quaternions/floats/... 
     // According to a float
     // float result = Lerp(float A, float B, float t);
     // here: A represent the "origin"
     // B is the "target"
     // and t is the delta of interpolation (between 0 and 1), for example:
     // float result = Mathf.Lerp(10f, 20f, 0f) -> here result will be equal to 10
     // float result = Mathf.Lerp(10f, 20f, 1f) -> here result will be equal to 20
     // float result = Mathf.Lerp(10f, 20f, 0.5f) -> here result will be equal to 15
     // If you want to know more about the Lerp function, checkout the Unity documentation
 
     transform.position += rightMovement;
     transform.position += upMovement;
   }
 }
Bruh THAN$$anonymous$$ U SO $$anonymous$$UCH, I really appreciate u
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                