0

Есть у кого скрипты поведения камеры? У меня на Unity, реализовано что то вреде плеера, который в зависимости от настроек проигрывает сцены. И мне нужны скрипты поведения камеры, такие которые ловят красивые виды.

Пару скриптов у меня уже есть(только их надо доработать, уж слишком некрасивый код получился):

  1. Который концентрируется на цели и крутиться во круг неё.
  2. У которого есть список объектов и он переключается от объекта к объекту.
  3. Камера движется по сплайну концентрируясь на цели.

Может ещё кто предложит сценарий поведения камеры?

1 и 2 скрипт объединён в один. Код следующий:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Timers;
using System.Threading;
using UnityEngine.SceneManagement;

public class RTCCamera : MonoBehaviour
{
    //  public Transform tank;
    //  public float distance = 15.0f;
    //  public float xSpeed = 50.0f;
    //  public float ySpeed = 50.0f;
    //  public float yMinLimit = -20.0f;
    //  public float yMaxLimit = 40.0f;
    //  
    //  private float x;
    //  private float y;
    //
    //  public float heightOffset = 3.5f;
    //(GameObject.Find ("M-109")) ? GameObject.Find ("M-109"): null ;

    public Transform target = null;
    public Vector3 offset = new Vector3(0f,10f,0f);
    public Quaternion angleoffset = new Quaternion(0.5f,0f,0f,5f) ;

    float sensitivity =0.2f; // чувствительность мышки
    float limit = 80; // ограничение вращения по Y
    float zoom = 0.25f; // чувствительность при увеличении, колесиком мышки

    public float zoomMin = 40f; // мин. увеличение
    public float zoomMax = 50f; // макс. увеличение

    public float Hmin = 5f;
    float tempHmin ;
    public float Anglemin = 0f;
    public float Hmax = 30f;
    float tempHmax ;
    public float Anglemax = 2f;

    public float HeightChangeRate = 0.1f;
    public float AngleChangeRate = 0.001f;

    private float X, Y;

    float index = 1f;
    bool zoombool = true;

    public bool onStartCamera = false;

    RTCTankController[] TanksList = null;

    public GameObject GameObjWithList = null;

    private int currentList = 0;
    Vector3 relPos;
    Quaternion newRot;

    public float speed = 0.65f;
    public float turn_speed = 1F;

    // Vector3 targetRotationCamera;
    string SceneName = "";

    bool sum = false;

    void Reset() {
        target = (GameObject.Find ("M-109")) ? GameObject.Find ("M-109").transform : null ;
        GameObjWithList = (GameObject.Find ("Vehicles")) ? GameObject.Find ("Vehicles"): null ;
    }

    void Start ()
    {

        //      Vector3 angles = transform.eulerAngles;
        //      x = angles.x;
        //      y = angles.y;
        //      
        //      if(GetComponent<Rigidbody>() != null)
        //      {
        //          GetComponent<Rigidbody>().freezeRotation = true;
        //      }

        if(SceneName == "Library") {
            target = GameObject.Find("Vehicles").GetComponent<Vehicles>().GetActivModel();
        }

        tempHmin = Hmin+target.position.y;
        tempHmax = Hmax+target.position.y;

        SceneName = SceneManager.GetActiveScene().name;

        limit = Mathf.Abs(limit);
        if(limit > 90) limit = 90;
        offset = new Vector3(offset.x, offset.y, -Mathf.Abs(zoomMax)/2);

        if (SceneName == "ShootingOne" || SceneName=="ShootingDefeat") {

        } else if (SceneName == "PathAll" || SceneName == "Distribution" || SceneName == "ShellingMovement" || SceneName == "СolumnMovement" || SceneName == "IntelligenceService" ) {
            transform.position = target.position + offset;
            TanksList = GameObjWithList.GetComponentsInChildren<RTCTankController>();
            if (SceneName == "СolumnMovement") {
                InvokeRepeating ("SwitchingGameObj_tow", 0, 5f);
            } else {
                InvokeRepeating("SwitchingGameObj", 0, 5f);
            }
        } else if (SceneName == "Library") {
            transform.position = target.position + offset;
        }

        if (SceneName == "IntelligenceService") {
            zoomMin = 410f;
            zoomMax = 450f;
        }

        //targetRotationCamera = target.position;

    }

    private void SwitchingGameObj() {
        onStartCamera =  false;
        currentList++;
    }

    private void SwitchingGameObj_tow() {
        onStartCamera =  true;
        currentList++;
    }

    private void SwitchingFunctionCamera() {
        if (onStartCamera == true)
            onStartCamera = false;
        else {
            onStartCamera = true;
            Invoke ("SwitchingFunctionCamera", 5f);
        }
    }

    private void SwitchBetweenGameObjCamera() {
        if (TanksList != null) {
            if (currentList < TanksList.Length) {

                target = TanksList [currentList].transform;

                if (transform.position.x > target.position.x) {
                    transform.position = Vector3.MoveTowards(transform.position, target.position+offset, speed);
                    relPos =  target.position - transform.position;
                    newRot = Quaternion.LookRotation (relPos);
                    transform.rotation = Quaternion.RotateTowards (transform.rotation,newRot,Time.time*speed);
                } else {
                    Vector3 tumpVector3 = new Vector3 (offset.x,offset.y * -1f,offset.z);
                    transform.position = Vector3.MoveTowards(transform.position, target.position - tumpVector3, speed);
                    relPos =  target.position - transform.position;
                    newRot = Quaternion.LookRotation (relPos);
                    transform.rotation = Quaternion.RotateTowards (transform.rotation ,newRot,Time.time*speed);
                }

            } else {
                currentList = 0;
                target = TanksList[currentList].transform;
                transform.position = Vector3.MoveTowards(transform.position, target.position+offset, speed);
                relPos = target.position - transform.position;
                newRot = Quaternion.LookRotation (relPos);
                transform.rotation = Quaternion.RotateTowards (transform.rotation,newRot,Time.time*speed);
                //CancelInvoke ("SwitchingGameObj");
                onStartCamera = true;
            }

        }
    }

    void Face_target(Vector3 relativePos)
    {
        Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turn_speed);  // slerp ( from.rotation, to.rotation, speed)
    }

    void LateUpdate()
    {
        // SceneName = SceneManager.GetActiveScene().name;
        if (SceneName=="ShootingOne" || SceneName=="ShootingDefeat") { 

        } else if (SceneName=="IntelligenceService") {

            SwitchBetweenGameObjCamera();

        } else {
            if (SceneName == "Library") {
                onStartCamera = true;
                if(SceneName == "Library") {
                    target = GameObject.Find("Vehicles").GetComponent<Vehicles>().GetActivModel();
                }
            }
            if (onStartCamera) {
                if (tempHmin != Hmin+target.position.y) { tempHmin = Hmin + target.position.y; } ;
                if (tempHmax != Hmax+target.position.y) { tempHmax = Hmax + target.position.y; } ;
                RotationRoundCamera ();
            } else {
                SwitchBetweenGameObjCamera();
            }
        }
        //      if(tank != null)
        //      {
        //
        //          x += (float)(Input.GetAxis("Mouse X") * xSpeed * 0.02f);
        //          y -= (float)(Input.GetAxis("Mouse Y") * ySpeed * 0.02f);
        //          
        //          y = ClampAngle(y, yMinLimit, yMaxLimit);
        //          
        //          Quaternion rotation = Quaternion.Euler(y, x, 0);
        //          Vector3 position = rotation * (new Vector3(0.0f, heightOffset, -distance)) + tank.position;
        //          
        //          transform.rotation = rotation;
        //          transform.position = position;
        //
        //      }
    }

//  void RotationCamera () {
//      if (targetRotationCamera != null) {
//          Quaternion targetRotation = Quaternion.LookRotation(targetRotationCamera - transform.position);
//          transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 4f);
//      }
//  }

    void RotationRoundCamera() {

        if(transform.position.y >= tempHmin && transform.position.y <= tempHmax && sum==false ) {
            //transform.position = new Vector3(transform.position.x,transform.position.y-2,transform.position.z);
            offset = new Vector3 (offset.x,offset.y-HeightChangeRate,offset.z);
            if (angleoffset.x - AngleChangeRate >= Anglemin) {
                angleoffset = new Quaternion (angleoffset.x-AngleChangeRate,angleoffset.y,angleoffset.z,angleoffset.w);

            } else {
                angleoffset = new Quaternion (angleoffset.x,angleoffset.y,angleoffset.z,angleoffset.w);
            }
        } else if(transform.position.y > tempHmax) {
            sum = false;
            offset = new Vector3 (offset.x,offset.y-HeightChangeRate,offset.z);
            if (angleoffset.x - AngleChangeRate >= Anglemin) {
                angleoffset = new Quaternion (angleoffset.x-AngleChangeRate,angleoffset.y,angleoffset.z,angleoffset.w);
            } else {
                angleoffset = new Quaternion (angleoffset.x,angleoffset.y,angleoffset.z,angleoffset.w);
            }
            //transform.position = new Vector3(transform.position.x,transform.position.y-2,transform.position.z);
        } else if (transform.position.y >= tempHmin && transform.position.y <= tempHmax && sum==true ) {
            //transform.position = new Vector3(transform.position.x,transform.position.y+2,transform.position.z);
            offset = new Vector3 (offset.x,offset.y+HeightChangeRate,offset.z);
            if (angleoffset.x + AngleChangeRate <= Anglemax) {
                angleoffset = new Quaternion (angleoffset.x+AngleChangeRate,angleoffset.y,angleoffset.z,angleoffset.w);
            } else {
                angleoffset = new Quaternion (angleoffset.x,angleoffset.y,angleoffset.z,angleoffset.w);
            }
        } else if (transform.position.y < tempHmin) {
            sum = true;
            offset = new Vector3 (offset.x,offset.y+HeightChangeRate,offset.z);
            if (angleoffset.x + AngleChangeRate <= Anglemax) {
                angleoffset = new Quaternion (angleoffset.x+AngleChangeRate,angleoffset.y,angleoffset.z,angleoffset.w);
            } else {
                angleoffset = new Quaternion (angleoffset.x,angleoffset.y,angleoffset.z,angleoffset.w);
            }
            //transform.position = new Vector3(transform.position.x,transform.position.y+2,transform.position.z);
        }

        if (zoombool == true) {
            offset.z += zoom/10f;
            zoombool = true;
            index=index + 0.25f;
            if (index==zoomMax){zoombool = false;}
        } else if (zoombool == false) {
            offset.z -= zoom/10f;
            zoombool = false;
            index=index - 0.25f;
            if (index == -zoomMin){zoombool = true;}
        }

        offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));

        if(SceneName == "Library") {
            X = transform.localEulerAngles.y + 0.1f * sensitivity;
        } else {
            X = transform.localEulerAngles.y + 0.5f * sensitivity;
        }

        Y += 0.00000001f * sensitivity;
        Y = Mathf.Clamp (Y, -limit, limit);
        transform.localEulerAngles = new Vector3(-Y, X, 0);
        transform.position = transform.localRotation * offset + target.position;

        if (target.position.y == transform.position.y) {
            transform.position = new Vector3 (transform.position.x, transform.position.y + target.position.y, transform.position.z);
        }

        if(angleoffset.w != 0f) {
            transform.rotation = transform.rotation * angleoffset;
        }
    }

    void OnCollisionEnter (Collision other) {
        tempHmin = Terrain.activeTerrain.SampleHeight (transform.position);
        print (other.gameObject.name);
    }

}

по сути я хотел повторить путь на рисунке(во время кругового движения): введите сюда описание изображения Настройки у скрипта получились не очевидные и не всегда работающие: введите сюда описание изображения

За любые улучшения кода буду благодарен.

7
  • А в чем вопрос то? Какой-то непонятный скрипт с какими-то танками, какие-то сплайны, хотя на них в скрипте и намека нет.
    – user302909
    16 авг 2019 в 13:25
  • @RiotBr3aker 1 Как улучшить мой код? 2 Если есть идеи сценария для камеры поделиться ими, если будет ещё и скрипт сценария поведения камеры, то совсем шикарно будет. 16 авг 2019 в 14:04
  • Ваш код - каша, как его улучшить? Переписать с нуля, камера для RTC? Какие-то танки.
    – user302909
    16 авг 2019 в 14:08
  • @RiotBr3aker Вопросом я хотел поднять тему. Сценарий поведения камеры, не привязанной к персонажу. Думаю он актуален не только для меня. Вдруг у кого то есть уже такие скрипты 16 авг 2019 в 14:09
  • К сожалению, данный сайт предназначен для конкретных вопросов и конкретных ответов. "Дайте пожалуйста код камеры" - это не вопрос.
    – user302909
    16 авг 2019 в 14:09

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.