KeyboardOrbit

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(New page: Category: Camera Category: MonoBehaviour Category: MouseLook Author: [http://nihilocrat.tumblr.com nihilocrat] ==Description== A slightly tweaked version of the original Mous...)
 
(added zoom keys)
Line 11: Line 11:
  
 
<csharp>
 
<csharp>
 +
//WASD to orbit, left Ctrl/Alt to zoom
 
using UnityEngine;
 
using UnityEngine;
  
Line 16: Line 17:
  
 
public class KeyboardOrbit : MonoBehaviour {
 
public class KeyboardOrbit : MonoBehaviour {
public Transform target;
+
    public Transform target;
public float distance = 10.0f;
+
    public float distance = 20.0f;
 +
    public float zoomSpd = 2.0f;
  
public float xSpeed = 240.0f;
+
    public float xSpeed = 240.0f;
public float ySpeed = 60.0f;
+
    public float ySpeed = 123.0f;
  
public int yMinLimit = -20;
+
    public int yMinLimit = -723;
public int yMaxLimit = 80;
+
    public int yMaxLimit = 877;
  
private float x = 0.0f;
+
    private float x = 0.0f;
private float y = 0.0f;
+
    private float y = 0.0f;
  
public void Start () {
+
    public void Start () {
Vector3 angles = transform.eulerAngles;
+
        Vector3 angles = transform.eulerAngles;
x = angles.y;
+
        x = angles.y;
y = angles.x;
+
        y = angles.x;
  
// Make the rigid body not change rotation
+
        // Make the rigid body not change rotation
if (rigidbody)
+
        if (rigidbody)
rigidbody.freezeRotation = true;
+
            rigidbody.freezeRotation = true;
}
+
    }
  
public void LateUpdate () {
+
    public void LateUpdate () {
if (target) {
+
        if (target) {
x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
+
            x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
+
            y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
 +
           
 +
            y = ClampAngle(y, yMinLimit, yMaxLimit);
 
 
y = ClampAngle(y, yMinLimit, yMaxLimit);
+
    distance -= Input.GetAxis("Fire1") *zoomSpd* 0.02f;
 
+
            distance += Input.GetAxis("Fire2") *zoomSpd* 0.02f;
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
+
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
+
 
 
transform.rotation = rotation;
+
            Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
transform.position = position;
+
            Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
}
+
           
}
+
            transform.rotation = rotation;
 +
            transform.position = position;
 +
        }
 +
    }
  
public static float ClampAngle (float angle, float min, float max) {
+
    public static float ClampAngle (float angle, float min, float max) {
if (angle < -360.0f)
+
        if (angle < -360.0f)
angle += 360.0f;
+
            angle += 360.0f;
if (angle > 360.0f)
+
        if (angle > 360.0f)
angle -= 360.0f;
+
            angle -= 360.0f;
return Mathf.Clamp (angle, min, max);
+
        return Mathf.Clamp (angle, min, max);
}
+
    }
 
}
 
}
 +
  
 
</csharp>
 
</csharp>

Revision as of 11:18, 13 August 2011


Author: nihilocrat

Description

A slightly tweaked version of the original MouseOrbit script to make it work with keyboard, or whatever you happen to bind to the Horizontal and Vertical axes.

Code C#

<csharp> //WASD to orbit, left Ctrl/Alt to zoom using UnityEngine;

[AddComponentMenu("Camera-Control/Keyboard Orbit")]

public class KeyboardOrbit : MonoBehaviour {

   public Transform target;
   public float distance = 20.0f;
   public float zoomSpd = 2.0f;
   public float xSpeed = 240.0f;
   public float ySpeed = 123.0f;
   public int yMinLimit = -723;
   public int yMaxLimit = 877;
   private float x = 0.0f;
   private float y = 0.0f;
   public void Start () {
       Vector3 angles = transform.eulerAngles;
       x = angles.y;
       y = angles.x;
       // Make the rigid body not change rotation
       if (rigidbody)
           rigidbody.freezeRotation = true;
   }
   public void LateUpdate () {
       if (target) {
           x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
           y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
           
           y = ClampAngle(y, yMinLimit, yMaxLimit);

distance -= Input.GetAxis("Fire1") *zoomSpd* 0.02f;

           distance += Input.GetAxis("Fire2") *zoomSpd* 0.02f;
           Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
           Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
           
           transform.rotation = rotation;
           transform.position = position;
       }
   }
   public static float ClampAngle (float angle, float min, float max) {
       if (angle < -360.0f)
           angle += 360.0f;
       if (angle > 360.0f)
           angle -= 360.0f;
       return Mathf.Clamp (angle, min, max);
   }

}


</csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox