• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);
     }
 
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_987gLU_QOEEZWg · Oct 26, 2020 at 11:21 AM 0
Share

absolute hero

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a movement system that changes with your viewpoint but also lets you hit multiple keys at once to go diagonally? 0 Answers

Look rotation viewing is zero warning 0 Answers

Understanding Space.World and Space.Self: Character changing movement direction 1 Answer

How to incorporate a rotation towards mouse position in this script? I tried 0 Answers

Rotate player in movement direction 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges