adding this script to a camera
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public int Sensitivity;
public float MaxDistance;
private float MouseY;
private float MouseYSet;
private float CameraAngleX;
private float CameraY;
private float CameraZ;
void Start () {
}
void Update () {
// Look controls
MouseY -= Input.GetAxisRaw ("Mouse Y");
CameraY = Mathf.Clamp (CameraY + (MouseY - MouseYSet) * Sensitivity * 10, transform.parent.lossyScale.y / 2, transform.parent.lossyScale.y / 2 + MaxDistance);
CameraZ = Mathf.Clamp (CameraZ + (MouseY - MouseYSet) * Sensitivity * 10, -MaxDistance, -MaxDistance / 4 * 3);
if (MouseYSet != MouseY)
{
transform.localPosition = new Vector3 (0, CameraY, CameraZ);
transform.LookAt (transform.parent.position);
MouseYSet = MouseY;
}
}
}
and this to a player
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public int Speed;
public int RunSpeed;
public int Sensitivity;
public int JumpHeight;
public int Force;
private float TerrainHeight;
private float TerrainHeightSet;
private float PlayerFoot;
private float MouseX;
private float MouseXSet;
private bool Jumping;
private bool Running;
void Start () {
Jumping = false;
Running = false;
}
void Update () {
PlayerFoot = transform.position.y - transform.lossyScale.y / 2;
TerrainHeight = Terrain.activeTerrain.SampleHeight (transform.position);
// Translation controls
if (Input.GetKey (KeyCode.W) && Running == false)
{
transform.position += transform.forward * Speed * 20 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.S) && Running == false)
{
transform.position -= transform.forward * Speed * 20 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.D) && Running == false)
{
transform.position += transform.right * Speed * 20 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.A) && Running == false)
{
transform.position -= transform.right * Speed * 20 * Time.deltaTime;
}
// Run controls
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.Q))
{
Running = true;
}
if (Input.GetKey (KeyCode.W) && Running == true)
{
transform.position += transform.forward * RunSpeed * 20 * Time.deltaTime;
}
if (Input.GetKeyUp (KeyCode.Q) && Input.GetKeyUp (KeyCode.W))
{
Running = false;
}
// Rotation controls
MouseX += Input.GetAxisRaw ("Mouse X");
if (MouseXSet != MouseX)
{
transform.Rotate (new Vector3 (0,(MouseX - MouseXSet) * Sensitivity * 10, 0));
MouseXSet = MouseX;
}
// Jump controls
if (Input.GetKey (KeyCode.Space) && PlayerFoot < TerrainHeight + 1 && PlayerFoot < TerrainHeight + JumpHeight * 10)
{
TerrainHeightSet = TerrainHeight;
StartCoroutine (JumpDelay ());
}
if (Jumping == true)
{
rigidbody.velocity = new Vector3 (0, (Force * 10), 0);
}
if (Input.GetKeyUp (KeyCode.Space))
{
Jumping = false;
}
if (PlayerFoot >= TerrainHeightSet + JumpHeight * 10)
{
Jumping = false;
}
if (Jumping == false)
{
rigidbody.velocity = new Vector3 (0, -(Force * 20), 0);
}
// Keep player upright
if (PlayerFoot < TerrainHeight + 1)
{
transform.position += Vector3.up * (TerrainHeight - PlayerFoot);
}
}
// Check if player really wants to jump
IEnumerator JumpDelay () {
yield return new WaitForSeconds (0.05f);
if (Input.GetKey (KeyCode.Space))
{
Jumping = true;
}
StopCoroutine (JumpDelay ());
}
}
it works fine with the looking left and right (not snappy) but vertical camera rotation is strange. it has about 20 different positions for some reason, almost like the positions are ints, but theyre not. this isnt a HUGE issue, but id like to eventually get it fixed. ive already had a look but theres nothing i can spot right now. if anyone could have a look, id appreciate it. sensitivity i set to 9 on both the camera and player, and i set maxdistance to 50.