- Home /
transformDirection for camera control
I'm attempting to make a game with a camera angled down about 70 degrees, and keep the camera bounded within it's parent object. I have it checking if it's within bounds and everything, but there are a few things that happen when I have the camera tilted and it's moving.
If I have it tilted anything other than 45 degrees down trying to move forward or backwards will move the camera along the Y axis as well, even though I'm using transform.TransformDirection to convert local space into world space, and if I use my vertical axis as the Z coordinate it moves my camera in the Y axis after putting it through the TransformDirection, as I have in my current script.
void FixedUpdate ()
{
float moveHorizontal = 0.0f;
float moveVertical = 0.0f;
// If camera is currently in bounds, calculates new position and applies movement
if (vector3Min("x") < transform.position.x && vector3Max("x") > transform.position.x)
moveHorizontal = Input.GetAxis ("Horizontal");
else if (vector3Min("x") >= transform.position.x && Input.GetAxis("Horizontal") > 0 || vector3Max("x") <= transform.position.x && Input.GetAxis("Horizontal") < 0)
moveHorizontal = Input.GetAxis ("Horizontal");
if (vector3Min("z") < transform.position.z && vector3Max("z") > transform.position.z)
moveVertical = Input.GetAxis ("Vertical");
else if (vector3Min("z") >= transform.position.z && Input.GetAxis("Vertical") > 0 || vector3Max("z") <= transform.position.z && Input.GetAxis("Vertical") < 0 )
moveVertical = Input.GetAxis ("Vertical");
if (moveHorizontal != 0.0f || moveVertical != 0.0f)
{
Vector3 movement = new Vector3 (moveHorizontal, moveVertical, 0.0f);
movement = transform.TransformDirection(movement);
transform.Translate(movement * speed);
}
}
float vector3Min(string I)
{
if (I == "x")
return (Vector3.Scale(transform.parent.localScale, transform.parent.parent.localScale) * -5.0f).x;
else if (I == "z")
return (Vector3.Scale(transform.parent.localScale, transform.parent.parent.localScale) * -5.0f).z;
else return 0.0f;
}
float vector3Max(string I)
{
if (I == "x")
return (Vector3.Scale(transform.parent.localScale, transform.parent.parent.localScale) * 5.0f).x;
else if (I == "z")
return (Vector3.Scale(transform.parent.localScale, transform.parent.parent.localScale) * 5.0f).z;
else return 0.0f;
}
If I swap the Y and Z around in the vector3 it moves along the Z axis primarily but still moves around the Y axis far too much.
Answer by Dwittles · Oct 30, 2017 at 06:08 PM
I solved this by creating a new parent object that wasn't rotated relative to the world space and attaching my script to that instead of directly to the camera, since the camera is now rotated relative to it's parent I can have it at whatever angle I want and it moves properly in the X and Z axis, no Y movement at all.
Your answer
Follow this Question
Related Questions
Lerp/Slerp making the Camera get stuck,Lerp / Slerp getting stuck 0 Answers
Distribute terrain in zones 3 Answers
Pinch zoom 1 Answer
Multiple Cars not working 1 Answer