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 wrathofjuju · Aug 24, 2015 at 11:21 AM · 3rd person cameracharacter controllingrelative rotationrelative movement

3rd person character movement relative to camera

I'm trying to build a simple 3rd person controller with a mouse-orbit camera where the character will move relative to the direction the camera is facing, so for example if you hit "left" he'll move to the camera's left, "forward" away from the camera, etc. I'm borrowing the camera script from here, seems to work fine: http://wiki.unity3d.com/index.php?title=MouseOrbitImproved.

Right now he'll MOVE in a direction relative to the camera, but I just can't figure out how to make him POINT in the direction that he's moving. He'll only face the "global" forward/back/left/right. I feel like the answer is right there, I just can't wrap my head around the math. I'm new at coding so it's all a bit foreign to me especially Quaternions and Euler angles etc. Any help would be appreciated! Here's my full character-movement script:

 using UnityEngine;
 using System.Collections;
 
 public class CharMovement : MonoBehaviour 
 {
     public float speed = 2f;
     public float runSpeed = 5f;
     public float turnSmoothing = 15f;
     
     private Vector3 movement;
     private Rigidbody playerRigidBody;
     
     void Awake()
     {
         playerRigidBody = GetComponent<Rigidbody> ();
     }
     
     void FixedUpdate()
     {
         float lh = Input.GetAxisRaw ("Horizontal");
         float lv = Input.GetAxisRaw ("Vertical");
         
         Move (lh, lv);
     }
     
     
     void Move (float lh, float lv)
     {
         movement.Set (lh, 0f, lv);
         movement = Camera.main.transform.TransformDirection(movement);
         
         
         if (Input.GetKey (KeyCode.LeftShift))
         {
             movement = movement.normalized * runSpeed * Time.deltaTime;
         } 
         else 
         {
             movement = movement.normalized * speed * Time.deltaTime;
         }
         
         playerRigidBody.MovePosition (transform.position + movement);
         
         
         if (lh != 0f || lv != 0f) 
         {
             Rotating(lh, lv);
         }
     }
     
     
     void Rotating (float lh, float lv)
     {
         Vector3 targetDirection = new Vector3 (lh, 0f, lv);
         
         Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);
         
         Quaternion newRotation = Quaternion.Lerp (GetComponent<Rigidbody> ().rotation, targetRotation, turnSmoothing * Time.deltaTime);
         
         GetComponent<Rigidbody>().MoveRotation(newRotation);
     }
 
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by christuart · Aug 24, 2015 at 02:37 PM

What is happening is that your targetDirection vector has been defined in world space, and never transformed to the camera space as you correctly achieved with the movement vector.

My advice would be, seeing as you have already calculated the movement vector, which is the direction you wish to face, and stored it in a variable accessible to the whole class, simply use that:

 Quaternion targetRotation = Quaternion.LookRotation (movement, Vector3.up);
Comment
Add comment · Show 2 · 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 wrathofjuju · Aug 28, 2015 at 11:15 AM 0
Share

Oh right, so simple! Thanks :)

avatar image Lienn · Aug 02, 2016 at 12:28 AM 0
Share

A follow-up to this. The fix works like a charm but the movement = camera.main.transformDirection (movement); is causing the rigidbody to bend toward camera's X rotation when moving forward and backward.

To fix it you need to manually reset movement's y position. Just adding movement.y = 0f; after the line above fixed it here. Now the camera will supposedly work fine with any pivot rotation or camera X rotation (my camera covers 0~60° X rotation, 360° Y position so i assume it covers all since it worked perfectly with any combination i tried)

I also did some $$anonymous$$or changes:

At $$anonymous$$ove () playerRigidBody.$$anonymous$$ovePosition (playerRigidBody.position + movement); and at Rotating () Quaternion targetRotation = Quaternion.LookRotation (movement, playerRigidBody.transform.up); Quaternion newRotation = Quaternion.Lerp (playerRigidBody.transform.rotation, targetRotation, turnSmoothing * Time.deltaTime); playerRigidBody.$$anonymous$$oveRotation (newRotation);

avatar image
0

Answer by Bigbossbro · Jan 16, 2017 at 03:02 PM

How about using Mouse Axis?

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Third Person Controller shuffling 0 Answers

transform.rotation and localRotation automatically reset after rotating. 1 Answer

How does TransformDirection(Vector) work? 1 Answer

Can anybody modify my script so that my player could move related to camera 1 Answer

AI strafe animations 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