- Home /
Directional Movement - Input position calculation?
Hey sorry if it's hard to explain but basically I want to have an empty target at the player position and have it move according to the directional buttons, but also according to the camera. The thirdpersoncontroller tries to do this but the system is really bad and doesn't account for sideways movement.
Here's a picture of what I'm trying to achieve:
edit: down below
Basically a movement system according to the camera. I tried using this script, but localPosition on both objects are starting to confuse me:
var p : Transform;
var cam : GameObject;
function Awake () {
if (!cam)
cam = gameObject.FindWithTag("MainCamera");
}
function Update () {
transform.rotation = cam.transform.rotation;
transform.position = p.transform.position;
if (Input.GetAxis("Horizontal") > 0)
{
if (Input.GetAxis("Vertical") > 0)
{
transform.localPosition = Vector3(p.transform.localPosition.x + 1, p.transform.localPosition.y, p.transform.localPosition.z + 1);
}
}
//etc
}
I'm not sure if that's what I'm supposed to be doing, but that's the target script... that doesn't work. Is there an easier way to get directional keys according to the camera with the center point being the player?
Answer by SomeGuy22 · Nov 19, 2012 at 10:19 PM
Solved it.
You need to have a center point on the player with the camera's rotation, and have a child of that object that's local position = Input.GetAxis.
Then you have the player always move forward and have him point towards the local object.
Answer by diegzumillo · Nov 19, 2012 at 03:41 AM
IF I understood your question, here's how I would do this:
Create a dummy object, let's call it 'root'. As a child of this root I would assign a camera and, maybe, a model for the player, but let's keep it simple and just work as if root is the player model already.
The camera isn't at the same position of the root, but flying above it. Assign a script to the camera that makes it always looking at the root. At the root assign a script that rotates it around the y axis according to mouse movement. Also at the root assign (with the same script, if you like) movement using the regular fps movement. Since the root is actually rotating, when you tell it to go forward in its local coordinate system it will be coherent with the camera as well.
If you don't want the player model to always have his back at the camera, make it a child of root and tell it to follow the root.
Sorry that's not what I meant, I wanted to make it so the player could move backwards and sideways on the camera and still have movement based on the camera. I think I might have figured this out, if so I'll post an answer later.
Sorry for not being able to help you. If your solution ends up not working try to elaborate your question a little better, maybe I or someone else can help you.