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).
===========================================================================
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!!!
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;
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.
Your answer
Follow this Question
Related Questions
Help With Camera-Dependent 3rd Person Control 0 Answers
How can I make the character turn to face the camera? 0 Answers
Object with fixed position but follows camera rotation 0 Answers
3rd person character controller rotation keeps resetting to 0 0 Answers
How to fix "the type or namespace ".." could not be found" in unity 0 Answers