Movement Flipped in Game View?
I am making a 3D, top-down shooter, and have run into a bit of an issue with my camera. The camera is above the player, pointed straight down, and is Orthographic, Size 20, with default clipping planes and Viewport Rect. It has a script attached:
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public double height = 20.0;
public double distance = 15.0;
public double maxSeparation = 60.0;
public float speed = 0.5f;
public Transform trgt;
void Awake()
{
transform.position = trgt.position;
transform.position += new Vector3(0, -(float)distance, (float)height);
transform.LookAt(trgt);
}
void Update()
{
Renderer renderer = trgt.GetComponent<Renderer>();
if ((Vector3.Distance(transform.position, trgt.position) >= maxSeparation) || !(renderer.isVisible) || (Vector3.Distance(transform.position, trgt.position) == 0))
{
transform.position = Vector3.Lerp(transform.position, trgt.position, speed);
transform.position += new Vector3(0, -(float)distance, (float)height);
transform.LookAt(trgt);
}
}
}
Currently, the variables are set to:
Height = 20
Distance = 0
Max Separation = 27
Speed = 1
Trgt = Player (Transform)
Movement is handled in a separate script, which uses AddRelativeForce and AddTorque.
This first is inside the script's FixedUpdate() function (only relevant code):
// If player can't move or game is paused
if (!playerCanMove || Time.timeScale == 0f)
{
return;
}
// Get input axis and process input
MovePlayer();
Vector3 movement = new Vector3(hAxis, 0.0f, vAxis);
if (movement.magnitude < _deadZone)
{
// Debug.Log("No movement");
movement = Vector3.zero;
}
else
{
// Debug.Log("Moving...");
movement = movement.normalized * ((movement.magnitude - _deadZone) / (1 - _deadZone));
}
if (movement != Vector3.zero)
{
var accelMultiplier = 1 - (_rigidbody.velocity.magnitude / _topSpeed);
// Debug.Log("accelMultiplier: " + accelMultiplier);
_rigidbody.AddRelativeForce(movement * _thrust * accelMultiplier * Time.deltaTime, ForceMode.VelocityChange);
}
if (rotateDir != 0.0f)
{
var turnMultiplier = 1 - (_rigidbody.angularVelocity.magnitude / _topAngVelocity);
_rigidbody.AddTorque(transform.up * rotateDir * _turnSpeed * turnMultiplier * Time.deltaTime, ForceMode.VelocityChange);
}
And here's and the MovePlayer() function:
private void MovePlayer()
{
hAxis = CrossPlatformInputManager.GetAxis("Horizontal");
vAxis = CrossPlatformInputManager.GetAxis("Vertical");
float r = CrossPlatformInputManager.GetAxis("Rotate");
if (Mathf.Abs(r) > _deadZone)
rotateDir = r * ((r - _deadZone) / (1 - _deadZone));
if ((r < 0) && (rotateDir > 0)) rotateDir = rotateDir * -1;
}
All variables are properly initialized in the Awake() function. Despite this, my horizontal input is shown to be reversed - pressing the right button appears to move the character left, and left right. This even happens with the XBOX 360 controller, so it's not that the axes are flipped. Further, this is only how it appears in the camera - the Scene View shows the character moving as expected.
Note that movement occurs along the X-Z plane only.
I suspect it's a bug in the CameraFollow class, but I'm not sure.
Thank you for any help you can give me with this problem. It's probably something easy I've overlooked, but I'm pretty new at this.
Your answer
Follow this Question
Related Questions
Can someone help me with this camera orbiting script? 0 Answers
How to script a random asset generator> 0 Answers
How can I get all cameras enabled true false states ? 0 Answers
Boolean variable is never true, even if declared true or ticked in the inspector 1 Answer
Sprite mouse movement Unity2D 0 Answers