- Home /
 
 
               Question by 
               Joyble · Aug 24, 2016 at 03:51 PM · 
                rotationmovementdirectionquaternions  
              
 
              How to move relative to the orientation
I'm using a script that makes my character rotate in 8 directions using quaternions and a CameraOrbit camera script. After hours and hours of work, everything is finally working nicely, the character rotates just the way I want it to(and always keeping the Vector3 to wherever the camera is pointing) BUT I can't get a single, even the simplest movement script to work with it. Only forward works properly with most of them. If someone is willing to tell me how I could implement movement as simple as they can, I'd be super grateful.
Here's the 8-directional rotation script:
 using UnityEngine;
 using System.Collections;
 
 public class AxisMovement : MonoBehaviour {
 
     // Use this for initialization
 
     private Vector3 inputDir = Vector3.zero;
     private float lockRotation = 0.0f;
     private CharacterController controller;
     public Transform target;
     public float speed = 1f;
 
 
 
 
     void Start()
     {
 
         controller = GetComponent<CharacterController>();
     }
 
     void Update()
     {
 
         Vector3 inputDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         Vector3 v = inputDir.normalized;
         v.x = Mathf.Round(v.x);
         v.z = Mathf.Round(v.z);
         if (v.sqrMagnitude > 0.1f)
             inputDir = v.normalized;
 
 
         inputDir = Camera.main.transform.rotation * inputDir;
 
         if ( inputDir != Vector3.zero ) 
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputDir), 0.5f);
 
         }
 
 
 
     void LateUpdate()
 
     {
 
         controller.transform.rotation = Quaternion.Euler(lockRotation, transform.rotation.eulerAngles.y, lockRotation);
 
     }
 
 }
 
              
               Comment
              
 
               
              Answer by villevli · Aug 24, 2016 at 09:06 PM
Simple movement:
 public class AxisMovement : MonoBehaviour {
 
     public Transform target;
     public float walkSpeed = 5f;
     public float gravity = -10f;
     public float turnSpeed = 10f;
 
     private CharacterController controller;
 
     // Use this for initialization
     void Start()
     {
         controller = GetComponent<CharacterController>();
     }
 
     void Update()
     {
         Vector3 inputDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         Vector3 v = inputDir.normalized;
         v.x = Mathf.Round(v.x);
         v.z = Mathf.Round(v.z);
         if (v.sqrMagnitude > 0.1f)
             inputDir = v.normalized;
 
         inputDir = Camera.main.transform.rotation * inputDir;
         inputDir.y = 0;
 
         if (inputDir != Vector3.zero)
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputDir), turnSpeed * Time.deltaTime);
 
         // Movement
         inputDir *= walkSpeed;
         inputDir.y += gravity * 50 * Time.deltaTime;
         controller.Move(inputDir * Time.deltaTime);
     }
 
 }
 
              Your answer