Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

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

$
0
0
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][1] =========================================================================== ![alt text][2] 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!!! [1]: /storage/temp/59054-good.jpg [2]: /storage/temp/59055-bad.jpg

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>