All,
I am attempting to create a camera that is controllable by the player in the following way(s):
- Up/Down arrow will move the camera forward and backwards along the X axis only (maintaining a static height over the ground)
- Left/right arrow will rotate the camera along the Y axis allowing the player to look at the ground (again, maintaining a static height over the ground)
The camera is placed over the game board at a height of 5(Y) and a rotation of 70(X).
Ideally by combining these two motions the player can hover and move smoothly over the board's surface.
At the moment the Forward and Back motion works fine. When I press either left or right the camera sweeps to the right and seems to "correct" itself. I believe this to be a fault with LocalRotation vs. World Rotation.
Here is my code thus far (note: I am new to both C# and Unity scripting in general). Any help would be greatly appreciated.
CODE:
using UnityEngine;
using System.Collections;
public class XookCam : MonoBehaviour
{
public float smooth = 1.5f; // The relative speed at which the camera will catch up.
private Transform ourcam; // Reference to the camera's transform.
private Quaternion newRot; // The rotation the camera is trying to reach.
private Vector3 newPos; // The position the camera is trying to reach.
void Awake ()
{
// Setting up the reference.
ourcam = GameObject.FindGameObjectWithTag("MainCamera").transform;
}
void Update ()
{
if(Input.GetKey(KeyCode.UpArrow))
{
// Lerp the camera's position between it's current position and it's new position.
newPos = transform.position + Vector3.forward;
transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
//transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.DownArrow))
{
// Lerp the camera's position between it's current position and it's new position.
newPos = transform.position + (-Vector3.forward);
transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
//transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.LeftArrow))
{
newRot = Quaternion.Euler(0, (transform.rotation.y + 10), 0);
Debug.Log(transform.rotation.y +10);
transform.rotation = Quaternion.Lerp(transform.rotation, newRot, Time.deltaTime * smooth);
//transform.rotation = Quaternion.Lerp(transform.rotation, newRot, smooth * Time.deltaTime);
//transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.RightArrow))
{
newRot = Quaternion.Euler(0, (transform.rotation.y -10), 0);
transform.rotation = Quaternion.Slerp(transform.rotation, newRot, Time.deltaTime * smooth);
}
// Lerp the camera's position between it's current position and it's new position.
//transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
}
}
Thank you!
Replies
You are mixing euler angles and quaternion components. transform.rotation.y is not an angle around the y axis but a component of the quaternion.
If you change this:
Quaternion.Euler(0, (transform.rotation.y + 10), 0);
to this:
Quaternion.Euler(0, (transform.rotation.eulerAngles.y + 10), 0);
it should work.
Thanks so much for the reply! I will try this as soon as I can. Much appreciated.
Cheers~
Thanks again!
Cheers~