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!
↧