Question by
mrlafle · Nov 30, 2015 at 12:28 AM ·
javascript3rd person controller3rd person camera
Help With Camera-Dependent 3rd Person Control
Hello everyone, I'm working on my first 3D platformer in Unity and I could use some help with my custom 3rd person controller. And sorry BTW if this has been asked and answered already!
So here's my situation. I have a 3rd person control system where I can rotate the camera around the hero (with the right stick) without making him turn as well. When I tilt the left stick upwards, the hero should turn and walk AWAY from the camera, no matter what direction it's pointed. But I cannot figure out how to program this. In my program the hero walks in the same global direction, even if it doesn't match up with the view. Here is my java 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. This is the camera's parent that rotates on the y-axis
//declaring speed & state variables
internal var runSpd:float = 0.5;
internal var facing:Vector3;
internal var grounded:boolean;
//declaring controller variables
internal var moveY:float; //move hero forward/back
internal var moveX:float; //move hero left/right
internal var jump:boolean; //make hero jump
function Update ()
{
print(moveY);
//defining controller variables
moveY = controller.GetComponent(ControllerScript).leftStickY;
moveX = controller.GetComponent(ControllerScript).leftStickX;
jump = controller.GetComponent(ControllerScript).butA;
//Determine hero facing with left stick
if(moveX || moveY){
facing = new Vector3(moveX, 0, -moveY);
heroMesh.transform.forward = facing;
}
//Move player in forward direction
hero.transform.position.z += -moveY * runSpd;
hero.transform.position.x += moveX * runSpd;
}
Sorry if my code is a little redundant, I'm a little new with Javascript
And thanks for the help!!!
Comment