duck

unity 3d example rotation clamp script c#

Jul 24th, 2011
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class RotateClamp : MonoBehaviour {
  5.    
  6.     Quaternion originalRotation;
  7.     public float rotationLimit = 15;
  8.    
  9.     // Use this for initialization
  10.     void Start () {
  11.         originalRotation = rigidbody.rotation;
  12.     }
  13.    
  14.     // Update is called once per frame
  15.     void FixedUpdate () {
  16.        
  17.         rigidbody.AddRelativeTorque( Input.GetAxis("Vertical") * Vector3.right * 10 );
  18.         rigidbody.AddRelativeTorque( Input.GetAxis("Horizontal") * Vector3.up * 10 );
  19.    
  20.         if (Vector3.Angle(rigidbody.rotation * Vector3.up , Vector3.up) > rotationLimit)
  21.         {
  22.             Vector3 flatForwardVector = new Vector3( transform.forward.x, 0, transform.forward.z );
  23.             Quaternion flatRotation = Quaternion.LookRotation( flatForwardVector );
  24.             rigidbody.rotation = Quaternion.RotateTowards(flatRotation, rigidbody.rotation, rotationLimit);
  25.         }
  26.        
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment