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 mrlafle · Dec 01, 2015 at 04:44 AM · javascriptlocal3rd person controller3rd person camera

How to move 3rd person controller in camera's local space?

Hi, I have a dilemma which may be harder to explain than it is to solve. I've asked this in the help room twice already with no luck, and anyone I found who asked about this in the past just left the community confused (and this is something I'd expect a lot of people to know about). This final attempt at explaining may be a little long, so please bear with me!

What I'm trying to do is set up a custom 3rd person controller exactly like what most 3rd person games use (Xenoblade, GTA 5, 3D Zelda's, Skyrim, 3D Mario's, Conker, etc all do this). I'm using an Xbox 360 controller, with the left stick set to control the player, and the right stick for controlling the free-look camera. (I've got most of this figured out BTW there's just one thing I'm struggling with. This is not a "make this game for me" post!)

In all the games I mentioned above, the camera can be rotated around the character while he/she just stands there -- the character does not turn with the camera the way he would in Gears of War or Resident Evil 4.

Also in all those games, when you tilt the left control stick upwards, the character ALWAYS turns and walks away from the camera. And if you try rotating the camera while walking in this direction, the character simply turns and keeps walking away from the camera.

This is because, when you move the character with the left stick, you're moving him in the camera's local space, not the world space. This is what I'm having trouble with. So far, my character is moving in the world space, so the stick's axes move him in the same direction no matter which way the camera is facing. This makes controlling him feel very weird in all but one camera angle. If I can just figure out how to move the character, with the left stick, in the camera's local space, I should be gravy.

Here is my script:

 #pragma strict
  
  //declaring object variables
  var controller:GameObject;        //gameObject with ControllerScript attatched
  var hero:GameObject;            //"Player" gameObject -- moves hero AND camera when running (parent of all other gameObjects here)
  var heroMesh:GameObject;        //moves hero (and not camera) when jumping (it has this script attached)
  var cam:GameObject;                //this is the camera's parent, an empty gameObject in the same location as the character. I rotate this on it's Y axis with the right stick
                                  
  internal var facing:Vector3;      //This Vector3 will be used to set character's rotation
  
  //declaring controller variables (which represent floats from Input.GetAxis and the Xbox controller axes)
  internal var moveY:float;        //move hero forward/back (controller's Y-axis)
  internal var moveX:float;        //move hero left/right (controller's X-axis)
  
  function Update () 
  {
      //I'm setting these with variables from my ControllerScript, which uses Input.GetAxis with the left stick
      moveY = controller.GetComponent(ControllerScript).leftStickY;
      moveX = controller.GetComponent(ControllerScript).leftStickX;        
  }
  
  function FixedUpdate()
  {
      //Determine hero facing with left stick 
      if(moveX || moveY){       //only adjust character rotation when left stick is in use
          facing = new Vector3(moveX, 0, -moveY);   //build "facing" Vector3 with stick input
          facing = cam.transform.InverseTransformDirection(facing);  //something I tried which almost worked but not quite
          heroMesh.transform.forward = facing; //finally, turn the character in the direction of 'facing"
      }
  }

If you have any questions or are confused about what I just went over please ask! I'd much rather go over it again than be sent off to do more googling. And, as always, thank you for the help!

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

0 Replies

· Add your reply
  • Sort: 

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

36 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

Related Questions

How do I make rigidbody.velocity in local space? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

How to make the character move and rotate where the camera is facing (3rd person) 0 Answers

Instantiate cloned prefab to local position of an empty object 1 Answer

Unity 3d Networking Local Search 0 Answers


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