Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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 an object go the direction it is facing? 6 Answers

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

How do I keep my character facing the direction of travel after movement stops? 1 Answer

Help with my Character Controller 1 Answer

Move into direction of another object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges