Advertisement
Guest User

Untitled

a guest
May 13th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class cameracontroler : MonoBehaviour {
  6.  
  7.     //if nothing click
  8.     public Transform target;
  9.     public float smoothTime = 0.3F;
  10.     private Vector3 velocity = Vector3.zero;
  11.     public float cameraxx = 0f;
  12.     public float camerayy =2f;
  13.     public float camerazz = -4f;
  14.    
  15.     //CameraOrbit()
  16.  
  17.     private Vector3 _cameraOffset;
  18.     public Transform PlayerTransform;
  19.     [Range(0.01f, 1.0f)]
  20.     public float SmoothFactor = 0.5f;
  21.    public float RotationsSpeed = 5.0f;
  22.    
  23.  
  24.     void Start() {
  25.  
  26.  
  27.         _cameraOffset = transform.position - target.position;
  28.  
  29.  
  30.     }
  31.  
  32.     void LateUpdate()
  33.     {
  34.    
  35.         if (Input.GetMouseButton(1)) {
  36.             cameraorbit();
  37.         }
  38.         else {
  39.             camerasmooth();
  40.         }
  41.  
  42.     }
  43.  
  44.     void cameraorbit() {
  45.             Quaternion camTurnAngle =
  46.              Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);
  47.  
  48.             _cameraOffset = camTurnAngle * _cameraOffset;
  49.            
  50.         Vector3 newPos = target.position + _cameraOffset;
  51.         transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
  52.         transform.LookAt(target);
  53.        
  54.            
  55.  
  56.  
  57.     }
  58.     void camerasmooth() {
  59.         // Define a target position above and behind the target transform
  60.        
  61.         Vector3 targetPosition = target.TransformPoint(new Vector3(cameraxx, camerayy, camerazz));
  62.        
  63.  
  64.         // Smoothly move the camera towards that target position
  65.         transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
  66.         transform.LookAt(target);
  67.     }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement