Advertisement
Guest User

Untitled

a guest
May 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ballMovement : MonoBehaviour {
  5.  
  6.     private Rigidbody ball_rb;
  7.  
  8.     public float _sensitivity;
  9.     private Vector3 _mouseReference;
  10.     private Vector3 _mouseOffset;
  11.     private Vector3 _rotation;
  12.     private bool _isRotating;
  13.  
  14.     // Use this for initialization
  15.     void Start () {
  16.         ball_rb = GetComponent<Rigidbody>();
  17.         _sensitivity = 0.4f;
  18.         _rotation = Vector3.zero;
  19.     }
  20.    
  21.     // Update is called once per frame
  22.     void Update () {
  23.         CameraMovement();
  24.     }
  25.  
  26.     void CameraMovement()
  27.     {
  28.         if (_isRotating)
  29.         {
  30.             // offset
  31.             _mouseOffset = (Input.mousePosition - _mouseReference);
  32.  
  33.             // apply rotation
  34.             _rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;
  35.  
  36.             // rotate
  37.             transform.Rotate(_rotation);
  38.  
  39.             // store mouse
  40.             _mouseReference = Input.mousePosition;
  41.         }
  42.  
  43.         if (Input.GetMouseButtonDown(1))
  44.         {
  45.             // rotating flag
  46.             _isRotating = true;
  47.  
  48.             // store mouse
  49.             _mouseReference = Input.mousePosition;
  50.         }
  51.         else if(Input.GetMouseButtonUp(1))
  52.         {
  53.             // rotating flag
  54.             _isRotating = false;
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement