0
\$\begingroup\$

The script is attached to a Camera. A bit messed but it's working.

For changing the camera height at run time I'm using a variable name cameraHeight at this line number 88:

Vector3 negDistance = new Vector3(0.0f, cameraHeight, -distanceFromTarget);

The original line was :

Vector3 negDistance = new Vector3(0.0f, 0.0f, -distanceFromTarget);

but if instead, I want to create a public method something like :

public void ChangeCameraAngle()
{

}

What should I do inside this method and then how to use it for things like changing the camera(transform) height or angle in run time?

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

public class CameraController : MonoBehaviour
{
    public Transform targetToRotateAround;

    public bool saveDuringPlay = false;

    public float distanceFromTarget = 5.0f;
    private float oldDistanceFromTarget;

    public float xSpeed = 120.0f;
    public float ySpeed = 120.0f;
    private float oldxSpeed, oldySpeed;

    public float yMinLimit = -20f;
    public float yMaxLimit = 80f;
    private float oldyMinLimit, oldyMaxLimit;

    public float distanceMin = .5f;
    public float distanceMax = 15f;
    private float oldDistanceMin, oldDistanceMax;

    public float cameraHeight;
    private float oldCameraHeight;

    public bool makeTransition = false;

    private Rigidbody rigidbody;
    private float x = 0.0f;
    private float y = 0.0f;

    // Use this for initialization
    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;

        rigidbody = GetComponent<Rigidbody>();

        // Make the rigid body not change rotation
        if (rigidbody != null)
        {
            rigidbody.freezeRotation = true;
        }

        LoadSettings();
    }

    private void Update()
    {
        if (saveDuringPlay)
        {
            SaveDuringPlay();

            oldDistanceFromTarget = distanceFromTarget;
            oldxSpeed = xSpeed;
            oldySpeed = ySpeed;
            oldyMinLimit = yMinLimit;
            oldyMaxLimit = yMaxLimit;
            oldDistanceMin = distanceMin;
            oldDistanceMax = distanceMax;
            oldCameraHeight = cameraHeight;
        }
    }

    void LateUpdate()
    {
        if (targetToRotateAround)
        {
            x += Input.GetAxis("Mouse X") * xSpeed * distanceFromTarget * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

            y = ClampAngle(y, yMinLimit, yMaxLimit);

            Quaternion rotation = Quaternion.Euler(y, x, 0);

            distanceFromTarget = Mathf.Clamp(distanceFromTarget - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);

            RaycastHit hit;
            if (Physics.Linecast(targetToRotateAround.position, transform.position, out hit))
            {
                distanceFromTarget -= hit.distance;
            }
            Vector3 negDistance = new Vector3(0.0f, cameraHeight, -distanceFromTarget);
            Vector3 position = rotation * negDistance + targetToRotateAround.position;

            transform.rotation = rotation;
            transform.position = position;
        }
    }

    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }

    public void SaveDuringPlay()
    {
        PlayerPrefs.SetFloat("DistanceFromTarget", distanceFromTarget);
        PlayerPrefs.SetFloat("xSpeed", xSpeed);
        PlayerPrefs.SetFloat("ySpeed", ySpeed);
        PlayerPrefs.SetFloat("yMinLimit", yMinLimit);
        PlayerPrefs.SetFloat("yMaxLimit", yMaxLimit);
        PlayerPrefs.SetFloat("DistanceMin", distanceMin);
        PlayerPrefs.SetFloat("DistanceMax", distanceMax);
        PlayerPrefs.SetFloat("CameraHeight", cameraHeight);
        PlayerPrefs.SetInt("MakeTransition", boolToInt(makeTransition));
    }

    public void LoadSettings()
    {
        distanceFromTarget = PlayerPrefs.GetFloat("DistanceFromTarget");
        oldDistanceFromTarget = distanceFromTarget;

        xSpeed = PlayerPrefs.GetFloat("xSpeed");
        oldxSpeed = xSpeed;
        ySpeed = PlayerPrefs.GetFloat("ySpeed");
        oldySpeed = xSpeed;

        yMinLimit = PlayerPrefs.GetFloat("yMinLimit");
        oldyMinLimit = yMinLimit;
        yMaxLimit = PlayerPrefs.GetFloat("yMaxLimit");
        oldyMaxLimit = yMaxLimit;

        distanceMin = PlayerPrefs.GetFloat("DistanceMin");
        oldDistanceMin = distanceMin;
        distanceMax = PlayerPrefs.GetFloat("DistanceMax");
        oldDistanceMax = distanceMax;

        cameraHeight = PlayerPrefs.GetFloat("CameraHeight");
        oldCameraHeight = cameraHeight;

        makeTransition = intToBool(PlayerPrefs.GetInt("MakeTransition", 0));
    }

    int boolToInt(bool val)
    {
        if (val)
            return 1;
        else
            return 0;
    }

    bool intToBool(int val)
    {
        if (val != 0)
            return true;
        else
            return false;
    }
}
\$\endgroup\$
1
\$\begingroup\$

To lift any object up or down you can simply add Vector3.up to your position:

transform.position = transform.position + Vector3.up*myDistance;   //if myDistance is negative it will go down
// or even more simpler:
transform.Translate(Vector3.up*myDistance); // does exactly the same job

(In general: to move object to some direction just find that direction vector and add to your position. In this case direction vector is Vector3.up).

To rotate object around by some angle you can simply use Rotate:

transform.Rotate(Vector3.up,myAngle);

This will instantly lift/rotate object to desired height/angle.

\$\endgroup\$
3
  • \$\begingroup\$ what about the method ChangeCameraAngle()? Should I create and use that one so I can make changes from other scripts? \$\endgroup\$ Jun 1 '21 at 15:42
  • \$\begingroup\$ I see no wrong in doing that \$\endgroup\$
    – ShoulO
    Jun 1 '21 at 15:44
  • \$\begingroup\$ If answer is correct/helpful you can accept it \$\endgroup\$
    – ShoulO
    Jun 1 '21 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .