Orbit Camera with Track Unity

269
25 августа 2017, 22:15

Есть такой скрипт. Пытаюсь к нему приделать траекторию. Допустим у меня есть здание буквой L. Мне нужно чтобы камера крутилась не по кругу как сейчас, а по траектории которая бы зависела от расстояния камеры до стен здания. Например, расстояние до стены или потолка должно быть не меньше 50f.

Кто-то поможет?)

 using System;
 using UnityEngine;
 using System.Collections;
[AddComponentMenu("Camera-Control/OrbitCam")]
[RequireComponent(typeof(RoomOrbitCamMouse))]
[RequireComponent(typeof(RoomOrbitCamKeys))]
public class RoomOrbitCam : MonoBehaviour
{
public Transform Target;
public float Distance;          // Desired distance (units, ie Meters)
public float Rotation;          // Desired rotation (degrees)
public float Tilt;              // Desired tilt (degrees)
public bool Smoothing;          
public float ZoomDampening;     
public float RotationDampening; 
public float TiltDampening;     
public float MinDistance;       // Minimum distance of camera from target
public float MaxDistance;       // Maximum distance of camera from target
public float MinRotation;       // Minimum rotation (degrees)
public float MaxRotation;       // Maximum rotation (degrees)
public float MinTilt;           // Minimum tilt (degrees)
public float MaxTilt;           // Maximum tilt (degrees)
public bool CollisionDetection;              
public float CameraRadius = 0.5f;
public bool AutoSetIgnoreLayers = false;
public LayerMask CollisionIgnoreLayerMask;   
public bool FollowBehind;                           
public float FollowRotationOffset;                  
//
// PRIVATE VARIABLES
//
private float _currDistance;    // actual distance
private float _currRotation;    // actual rotation
private float _currTilt;        // actual tilt
//
// Unity METHODS
//
#region UNITY_METHODS
public void Reset()
{
    Smoothing = true;
    CollisionDetection = true;
    CameraRadius = 0.5f;
    CollisionIgnoreLayerMask = 0;    // "Nothing" by default!
    AutoSetIgnoreLayers = false;
    Distance = 16f;
    MinDistance = 2f;
    MaxDistance = 32f;
    Rotation = 0f;
    MinRotation = -180f;
    MaxRotation = 180f;
    Tilt = 45f;
    MinTilt = -15f;
    MaxTilt = 85f;
    TiltDampening = 5f;
    ZoomDampening = 5f;
    RotationDampening = 5f;
    FollowBehind = false;
    FollowRotationOffset = 0;
}
protected void Start()
{
    if (GetComponent<Rigidbody>())
    {
        // don't allow camera to rotate
        GetComponent<Rigidbody>().freezeRotation = true;
    }
    //
    // set our current values to the desired values so that we don't "slide in"
    //
    _currDistance = Distance;
    _currRotation = Rotation;
    _currTilt = Tilt;
}
protected void Update()
{
}
protected void LateUpdate()
{
    if (Target == null)
        return;
    //
    // wrap and clamp values
    //
    Rotation = WrapAngle(Rotation);
    Tilt = WrapAngle(Tilt);
    Tilt = Mathf.Clamp(Tilt, MinTilt, MaxTilt);
    Distance = Mathf.Clamp(Distance, MinDistance, MaxDistance);
    Rotation = Mathf.Clamp(Rotation, MinRotation, MaxRotation);
    //
    // move from "desired" to "target" values
    //
    if (Smoothing)
    {
        _currRotation = Mathf.LerpAngle(_currRotation, Rotation, Time.deltaTime * RotationDampening);
        _currDistance = Mathf.Lerp(_currDistance, Distance, Time.deltaTime * ZoomDampening);
        _currTilt = Mathf.LerpAngle(_currTilt, Tilt, Time.deltaTime * TiltDampening);
    }
    else
    {
        _currRotation = Rotation;
        _currDistance = Distance;
        _currTilt = Tilt;
    }
    //
    // if we're following AND forcing behind, override the rotation to point to target (with offset)
    //
    if (FollowBehind)
    {
        ForceFollowBehind();
    }
    //
    // optionally, we'll check to make sure the target is visible
    //
    if (CollisionDetection)
    {
        if (AutoSetIgnoreLayers && CollisionIgnoreLayerMask == 0)
        {
            CollisionIgnoreLayerMask = 1 << Target.gameObject.layer;
        }
        CheckCollisions();
    }
    //
    // recalculate the actual position of the camera based on the above
    //
    UpdateCamera();
}
#endregion
//
// PUBLIC METHODS
//
#region PUBLIC_METHODS
/// <summary>
/// Snaps the current and desired camera position to the specified values
/// </summary>
/// <param name="distance"></param>
/// <param name="rotation"></param>
/// <param name="tilt"></param>
public void SnapTo(float distance, float rotation, float tilt)
{
    Distance = _currDistance = distance;
    Rotation = _currRotation = rotation;
    Tilt = _currTilt = tilt;
}
#endregion
//
// PRIVATE METHODS
//
#region PRIVATE_METHODS
/// <summary>
/// Update the camera position and rotation based on calculated values
/// </summary>
private void UpdateCamera()
{
    if (Target == null)
        return;
    var rotation = Quaternion.Euler(_currTilt, _currRotation, 0);
    var v = new Vector3(0.0f, 0.0f, -_currDistance);
    var position = rotation * v + Target.transform.position;
    if (GetComponent<Camera>().orthographic)
    {
        GetComponent<Camera>().orthographicSize = _currDistance;
    }
    // update position and rotation of camera
    transform.rotation = rotation;
    transform.position = position;
}
private RaycastHit[] _prevHits;
private void CheckCollisions()
{
    var direction = (transform.position - Target.transform.position);
    direction.Normalize();
    var distance = Distance;
    RaycastHit hitInfo;
    if (Physics.SphereCast(Target.transform.position, CameraRadius, direction, out hitInfo, distance, ~CollisionIgnoreLayerMask))
    {
        if (hitInfo.transform != Target)   // don't collide with ourself! ... hopefully, our target's layer is in the CollisionIgnoreLayerMask
        {
            _currDistance = hitInfo.distance - 0.05f;
        }
    }
}
private void ForceFollowBehind()
{
    var v = Target.transform.forward * -1;
    var angle = Vector3.Angle(Vector3.forward, v);
    var sign = (Vector3.Dot(v, Vector3.right) > 0.0f) ? 1.0f : -1.0f;
    _currRotation = Rotation = 180f + (sign * angle) + FollowRotationOffset;
}
private float WrapAngle(float angle)
{
    while (angle < -180)
    {
        angle += 360;
    }
    while (angle > 180)
    {
        angle -= 360;
    }
    return angle;
}
#endregion

}

READ ALSO
Закрыть доступ к дампу dll

Закрыть доступ к дампу dll

Подскажите как возможно защитить свою игры онNET DUMP

224
Запуск класса перед формой WPF

Запуск класса перед формой WPF

Мне нужно перед запуском mainwindowxaml запустить класс start

230
unity3d не работает OnCollisionEnter2D

unity3d не работает OnCollisionEnter2D

объект отмечен как триггер - при прохождении через этот коллайдер-триггер на котором висит этот скрипт - другие коллайдеры должны уничтожатьсяНа...

274
Как подключить?

Как подключить?

Как подключить свою системную библиотеку? Допустим нужно mscorib - через ссылки не удается - удалил старый вопрос

230