3

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 .

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;
    }



}
Zissouu
  • 934
  • 2
  • 10
  • 17
Zid Lyx
  • 79
  • 1
  • 10

1 Answers1

1

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.

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. Altering zoomPos before you run the game allows you to effectively set a default starting height.

Matthew Loveday
  • 362
  • 3
  • 12