- Home /
Question by
Bincredible · Oct 30, 2013 at 07:42 PM ·
rotationvector3booleangetkey
Camera rotation
Hey, I wanted to know how I could make an option so if you don't have the boolean moveWithPlayer as true, you need to move the camera left and right with the mouse. But if you have it as true, when you press the arrow keys to turn left and right the camera will rotate with the player instead of using the mouse. Here is the script :
using UnityEngine;
using System.Collections;
public class MouseOrbit : MonoBehaviour
{
public Transform target;
public float distance = 10.0f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20;
public float yMaxLimit = 80;
private float x = 0.0f;
private float y = 0.0f;
public int min = 4;
public int max = 19;
public float minLess = 4.1f;
public float maxLess = 18.9f;
public bool moveWithPlayer;
void Start ()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
void Update ()
{
if (target)
{
if(distance < min){
distance = minLess;
}
if(distance > max){
distance = maxLess;
}
if(distance >= min && distance <= max){
distance -= Input.GetAxis("Mouse ScrollWheel") * 6;
}
if(Input.GetMouseButton(1)){
x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
}
y = ClampAngle(y, yMinLimit, yMaxLimit);
if(!moveWithPlayer){
transform.rotation = Quaternion.Euler(y, x, 0);
}
if(moveWithPlayer){
//This is where the code for rotating the camera with the player goes.
}
transform.position = (Quaternion.Euler(y, x, 0)) * new Vector3(0.0f, 0.0f, -distance) + target.position;
}
}
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
{
angle += 360;
}
if (angle > 360)
{
angle -= 360;
}
return Mathf.Clamp(angle, min, max);
}
}
Comment