• 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 /
  • Help Room /
avatar image
Question by mrlafle · Nov 30, 2015 at 07:14 PM · javascript3rd person controller3rd person cameralocalspace

Transform.InverseTransformDirection ALMOST worked with character-facing direction!!!!

Hello, I'm working on my first 3d platformer and while I've been able to solve a surprising number of problems on my own, there's one thing that has had me stumped for some time. 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
 var heroMesh:GameObject;        //"Mesh" gameObject -- moves hero (and not camera) when jumping
 var cam:GameObject;                //"CamRotY" gameObject. I rotate this on it's Y axis with the right stick
                                 
 internal var facing:Vector3;
 
 //declaring controller variables
 internal var moveY:float;        //move hero forward/back 
 internal var moveX:float;        //move hero left/right
 
 function Update () 
 {
     print(cam.transform.forward);

     //defining controller variables
     moveY = controller.GetComponent(ControllerScript).leftStickY;
     moveX = controller.GetComponent(ControllerScript).leftStickX;        
 }
 
 function FixedUpdate()
 {
     //Determine hero facing with left stick 
     if(moveX || moveY){
         facing = new Vector3(moveX, 0, -moveY);
         facing = cam.transform.InverseTransformDirection(facing);
         heroMesh.transform.forward = facing;
     }
 }
 

This script is attatched to my hero. I have another script attached to the camera which allows me to rotate the camera around the hero without making him turn as well (Just like in GTA 5). There's another script (ControllerScript) that handles input from my Xbox controller.

Just like in GTA 5, when I tilt the left stick upwards, the hero SHOULD turn and walk away from the camera, no matter what direction said camera is facing. My problem is turning the hero in accordance to the camera's direction.

I tried transforming my hero's "facing" Vector3 from world space to my camera's local space with cam.transform.InverseTransformDirection(facing). This almost worked, but not quite.

Now, when my camera is pointed north or south (cam.transform.forward.x = 0), the hero turns exactly where he should when I use the left stick. This is good. However, when my camera is facing east or west (cam.transform.forward.x = 1 or -1), the hero turns exactly OPPOSITE of where he should when I use the left stick. This is NOT good.

Here are some pictures that may help show what my problem is. Take note the direction my hero is facing, my left stick input (arrows), and cam.transform.facing (printed).

alt text

===========================================================================

alt text

This has been a real pain in the butt for me. I will build a shrine and dedicate it to whoever can help me with this!!!

good.jpg (363.3 kB)
bad.jpg (363.1 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by JonathanCzeck · Nov 30, 2015 at 08:17 PM

 facing = cam.transform.forward; // Shortcut for cam.transform.TransformDirection(Vector3.forward)
 facing.y = 0;
 transform.forward = facing;
Comment

People who like this

0 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 mrlafle · Nov 30, 2015 at 09:05 PM 0
Share

Thank you first of all for replying!

I replaced what I had in my if(moveX || moveY) statement with your code, and it didn't work. The hero's turning lined up with the camera direction, but no matter how I tilted my left stick, the hero would always face away from the camera.

Then I removed the if statement and left my FixedUpdate() with nothing but your code and it worked... but not the way I want. It let me rotate the camera AND turn the character with the right stick, causing the camera to always stay behind the hero (much like in Gears of War).

What I'm trying to do is rotate the camera with the right stick, and turn/move the hero with the left stick. The character should not move or rotate when I use the right stick (GTA 5 and Zelda: Wind Waker do the same thing).

That's why I'm using the if(moveX || moveY) statement -- to only update the hero rotation when he gets input from the left stick.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

39 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

Related Questions

Help With Camera-Dependent 3rd Person Control 0 Answers

How to fix "the type or namespace ".." could not be found" in unity 0 Answers

3rd Person Movement / Camera Control Help or Advice Needed 1 Answer

3rd Person Camera Controller, Character should follow the Camera Movement 0 Answers

Need help with my 3RD person Script being buggy!! 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