KeyboardOrbit

From Unify Community Wiki
Revision as of 19:13, 24 March 2010 by Nihilocrat (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


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> using UnityEngine;

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

public class KeyboardOrbit : MonoBehaviour { public Transform target; public float distance = 10.0f;

public float xSpeed = 240.0f; public float ySpeed = 60.0f;

public int yMinLimit = -20; public int yMaxLimit = 80;

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);

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