Home Unity

Camera Controls

Novian
polycounter lvl 12
Offline / Send Message
Novian polycounter lvl 12
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

  • Novian
    Options
    Offline / Send Message
    Novian polycounter lvl 12
    Update: When I apply this code to an empty Game Object that I then parent the camera to the rotation works, mostly, however it seems to approach a maximum and slow down on a curve. The effect is holding the left arrow for example, the camera rotates and then slows and eventually stops at 10.7892 degrees and holds. You can see the Debug.Log statement in order to read the value at run time. After it reaches this value it stops. Effectively I cannot rotate past 10 degrees to the right or left. I now this must be somehow tied to the fact that I have it coded to turn 10 degrees each time the key is pressed but I cannot see it. Any help would be greatly appreciated. Thanks!
  • Parkar
    Options
    Offline / Send Message
    Parkar polycounter lvl 18
    http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.html

    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.
  • Novian
    Options
    Offline / Send Message
    Novian polycounter lvl 12
    Parkar,

    Thanks so much for the reply! I will try this as soon as I can. Much appreciated. :)

    Cheers~
  • Novian
    Options
    Offline / Send Message
    Novian polycounter lvl 12
    That did it! I also had to reverse my -/+ 10 for the rotations to work correctly. Much appreciated! Next up is to make objects on the game board react when clicked.

    Thanks again!

    Cheers~
Sign In or Register to comment.