• Страница 1 из 1
  • 1
Форум » Программирование » Java script » Движение камеры при хотьбе и беге
Движение камеры при хотьбе и беге
Администратор
Уровень 999
Сообщений: 2104
Дата: Среда, 11.04.2012, 01:54:59 | Сообщение # 1
Offline
Вот скрипт для движение камеры при хотьбе или беге

Code
using UnityEngine;   

   public class Camera_FPS : MonoBehaviour   
   {   
   public Vector2 rotationLimit; //Limit for the vertical rotation of the camera. X is higher limit, Y is lower limit.   
   public float rotationSpeed = 600.0f; //Vertical rotation speed of the camera   
   public bool invertY = true; //Invert vertical rotation   
   public bool headBobEnabled = true; //Enables/disables head bobbing   
   public Vector2 headBobDistance = new Vector2(0.2f, 0.3f); //The distance the camera travels in the y-axis for the headBobbing effect   
   public float headBobSpeed = 5.0f; //Speed at which the player's head bobs   

   private Vector3 initialLocalPos; //Starting local y-position of camera   
   private float rot; //Stores the rotation value incremented by mouse movement in the y-axis   
   private Transform t; //Reference to the transform component of the game object   

   public void Awake()   
   {   
   t = transform; //Cache transform component   
   initialLocalPos = t.localPosition; //Store initial local position of the camera   
   }   

   public void Update ()   
   {   
   //Update rotation   
   rot += Input.GetAxis("Mouse Y") * rotationSpeed * ((invertY) ? -1 : 1) * Time.deltaTime;   
   if(rot < rotationLimit.y) //Check for lower limit   
   rot = rotationLimit.y;   
   if(rot > rotationLimit.x) //Check for upper limi   
   rot = rotationLimit.x;   
   t.localRotation = Quaternion.AngleAxis (rot, Vector3.right); //Apply rotation to transform   

   //Head bobbing   
   if(headBobEnabled)   
   {   
   //Calculate percentage of blend between stationary and bobbing camera positions   
   float percentage = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));   

   //Calculate desired x position   
   float desiredPosX = initialLocalPos.x + headBobDistance.x * Mathf.Sin(Time.time * headBobSpeed + Mathf.PI/2);   
   //Blend between stationary and desired x position   
   float newX = (initialLocalPos.x * (1 - percentage)) + (desiredPosX * (percentage));   

   //Calculate desired y position   
   float desiredPosY = initialLocalPos.y + headBobDistance.y * Mathf.Sin(Time.time * 2 * headBobSpeed);   
   //Blend between stationary and desired y position   
   float newY = (initialLocalPos.y * (1 - percentage)) + (desiredPosY * (percentage));   

   t.localPosition = new Vector3( newX, newY, t.localPosition.z);   
   }   
   }   
   }
Подпись пользователя
Форум » Программирование » Java script » Движение камеры при хотьбе и беге
  • Страница 1 из 1
  • 1
Поиск: