简体   繁体   English

unity 相机起始位置

[英]Unity camera starting position

Hello so I created a camera for a RTS game I'm making but on start the camera is on the minHight position and I cant figure out how to change it hope someone helps the code is a bit of a mess its just for a test here is the code I want it to start from the maxHight position that is given and not on the minHight .你好,所以我为我正在制作的 RTS 游戏创建了一个摄像头,但在开始时,摄像头位于 minHight 位置,我无法弄清楚如何更改它希望有人帮助代码有点混乱,只是为了在这里进行测试是我希望它从给出的 maxHight 位置而不是 minHight 开始的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RtsCamera : MonoBehaviour {


    public float zoomSensitivy = 15;
    private Transform m_Transform;
    public float screenEdgeBorder = 25f;
    public bool useScreenEdgeInput = true;
    public float screenEdgeMovementSpeed = 3f;

    // zoom stuff
    public float heightDampening = 5f;
    public float scrollWheelZoomingSensitivity = 25f;
    public float maxHeight = 10f; //maximal height
    public float minHeight = 15f; //minimnal height
    public LayerMask groundMask = -1;
    public bool autoHeight = true;
    public bool limitMap = true;
    public float limitX = 50f; //x limit of map
    public float limitZ = 50f; //z limit of map
    private float zoomPos = 0; //value in range (0, 1) used as t in Matf.Lerp
    public bool useScrollwheelZooming = true;

    Vector3 tempPos;


    public string zoomingAxis = "Mouse ScrollWheel";

    private float ScrollWheel
    {
        get { return Input.GetAxis(zoomingAxis); }
    }

    private Vector2 MouseAxis
    {
        get { return new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); }
    }
    private Vector2 MouseInput
    {
        get { return Input.mousePosition; }
    }


    private void Start()
    {

        m_Transform = transform;

    }
    // Update is called once per frame
    void Update () {
        Move();
        LimitPosition();
        HeightCalculation();







    }
    private void Move()
    {


        if (useScreenEdgeInput)
        {
            Vector3 desiredMove = new Vector3();

            Rect leftRect = new Rect(0, 0, screenEdgeBorder, Screen.height);
            Rect rightRect = new Rect(Screen.width - screenEdgeBorder, 0, screenEdgeBorder, Screen.height);
            Rect upRect = new Rect(0, Screen.height - screenEdgeBorder, Screen.width, screenEdgeBorder);
            Rect downRect = new Rect(0, 0, Screen.width, screenEdgeBorder);

            desiredMove.x = leftRect.Contains(MouseInput) ? -1 : rightRect.Contains(MouseInput) ? 1 : 0;
            desiredMove.z = upRect.Contains(MouseInput) ? 1 : downRect.Contains(MouseInput) ? -1 : 0;

            desiredMove *= screenEdgeMovementSpeed;
            desiredMove *= Time.deltaTime;
            desiredMove = Quaternion.Euler(new Vector3(0f, transform.eulerAngles.y, 0f)) * desiredMove;
            desiredMove = m_Transform.InverseTransformDirection(desiredMove);

            m_Transform.Translate(desiredMove, Space.Self);
        }

    }
    private void LimitPosition()
    {
        if (!limitMap)
            return;

        m_Transform.position = new Vector3(Mathf.Clamp(m_Transform.position.x, -limitX, limitX),
            m_Transform.position.y,
            Mathf.Clamp(m_Transform.position.z, -limitZ, limitZ));
    }

    private void HeightCalculation()
    {
        float distanceToGround = DistanceToGround();
        if (useScrollwheelZooming)
            zoomPos += ScrollWheel * Time.deltaTime * scrollWheelZoomingSensitivity;


        zoomPos = Mathf.Clamp01(zoomPos);

        float targetHeight = Mathf.Lerp(minHeight, maxHeight, zoomPos);
        float difference = 0;

        if (distanceToGround != targetHeight)
            difference = targetHeight - distanceToGround;

       m_Transform.position = Vector3.Lerp(m_Transform.position,
            new Vector3(m_Transform.position.x, targetHeight + difference, m_Transform.position.z), Time.deltaTime * heightDampening);
    }
    private float DistanceToGround()
    {
        Ray ray = new Ray(m_Transform.position, Vector3.down);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, groundMask.value))
            return (hit.point - m_Transform.position).magnitude;

        return 0f;
    }



}

Set the value of the variable zoomPos on line 25 to either 1 or 0 depending on whether you want it at the min height or the max height, you could do this either in the Start() or just at the declaration of the variable.将第 25 行的变量zoomPos的值设置为 1 或 0,具体取决于您希望它处于最小高度还是最大高度,您可以在Start()或仅在变量声明中执行此操作。

This is because in the HeightCalculation function it sets the height to somewhere between the min and max based off the value of zoomPos , as you use the scrollwheel this value changes and so does the zoom.这是因为在HeightCalculation函数中,它根据HeightCalculation值将高度设置为介于最小值和最大值之间的某个zoomPos ,当您使用zoomPos ,该值会发生变化,缩放也会发生变化。 Altering zoomPos before you run the game allows you to effectively set a default starting height.在运行游戏之前更改zoomPos可以让您有效地设置默认起始高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM