Question by
dannywilliams · Mar 04, 2016 at 12:16 PM ·
movementcamera movement
I have a little issue with top down movement using the camera orientation.
Hello, my game currently uses controls based off of the direction that the camera is facing, I have no issues using this method during side-view and third person. But when I use the following code:
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.position -= Camera.main.transform.right * speed;
}
if (Input.GetKey (KeyCode.RightArrow))
{
transform.position += Camera.main.transform.right * speed;
}
if (Input.GetKey (KeyCode.UpArrow))
{
movement = transform.position += (Camera.main.transform.up);
hammy.transform.rotation = Quaternion.LookRotation (Camera.main.transform.up);
}
if (Input.GetKey (KeyCode.DownArrow))
{
movement = transform.position -= (Camera.main.transform.up);
hammy.transform.rotation = Quaternion.LookRotation (-(Camera.main.transform.up));
}
if (Input.GetAxis ("LeftStickHorizontal") > 0f)
{
movement = transform.position += Camera.main.transform.right * speed;
hammy.transform.rotation = Quaternion.LookRotation (Camera.main.transform.right);
}
if (Input.GetAxis ("LeftStickHorizontal") < 0f)
{
movement = transform.position -= Camera.main.transform.right * speed;
hammy.transform.rotation = Quaternion.LookRotation (-(Camera.main.transform.right));
}
if (Input.GetAxis ("LeftStickVertical") < 0f)
{
movement = transform.position += (Camera.main.transform.up);
hammy.transform.rotation = Quaternion.LookRotation (Camera.main.transform.up);
}
if (Input.GetAxis ("LeftStickVertical") > 0f)
{
movement = transform.position -= (Camera.main.transform.up);
hammy.transform.rotation = Quaternion.LookRotation (-(Camera.main.transform.up));
}
The top view seems to cause some issues.
The side-view uses this code:
if (Input.GetKey (KeyCode.RightArrow)) {
movement = transform.position += Camera.main.transform.right * speed;
hammy.transform.rotation = Quaternion.LookRotation (Camera.main.transform.right);
}
else if (Input.GetKey (KeyCode.LeftArrow)) {
movement = transform.position -= Camera.main.transform.right * speed;
hammy.transform.rotation = Quaternion.LookRotation (-(Camera.main.transform.right));
}
if (Input.GetKey (KeyCode.UpArrow)) {
movement = transform.position += Camera.main.transform.forward * speed;
hammy.transform.rotation = Quaternion.LookRotation (Camera.main.transform.forward);
}
else if (Input.GetKey (KeyCode.DownArrow)) {
movement = transform.position -= Camera.main.transform.forward * speed;
hammy.transform.rotation = Quaternion.LookRotation (-(Camera.main.transform.forward));
}
if (Input.GetAxis ("LeftStickHorizontal") > 0f) {
movement = transform.position += Camera.main.transform.right * speed;
hammy.transform.rotation = Quaternion.LookRotation (Camera.main.transform.right);
}
if (Input.GetAxis ("LeftStickHorizontal") < 0f) {
movement = transform.position -= Camera.main.transform.right * speed;
hammy.transform.rotation = Quaternion.LookRotation (-(Camera.main.transform.right));
}
if (Input.GetAxis ("LeftStickVertical") < 0f) {
movement = transform.position += Camera.main.transform.forward * speed;
hammy.transform.rotation = Quaternion.LookRotation (Camera.main.transform.forward);
}
if (Input.GetAxis ("LeftStickVertical") > 0f) {
movement = transform.position -= Camera.main.transform.forward * speed;
hammy.transform.rotation = Quaternion.LookRotation (-(Camera.main.transform.forward));
}
It seems to have the up and negative up as some incredible speed. I can fix this by dividing the transform by any integer but was wondering if there was any other fix which doesn't make me divide.
Thanks,
Danny.
Comment
Your answer
