Home>

Now we are creating a system like TPS shooting with unity.
I am currently operating the camera with the mouse, but when the camera moves the mouse moves with it, so I want to stop it at a certain point.

The environment is unity 2017.4.3f1 C #.
The reason why it is not up-to-date is that I am a professional student, but the school does not keep it up to date for some reason.

Currently, the camera moves around the player's Y axis only by moving the mouse.
I want to set it so that the camera doesn't move further around 30 degrees above, 30 degrees below, 50 degrees right, and 50 degrees left.

Problems i am experiencing
Because I am a C # beginner, I don't know how to do it.
The source code that has been written has been made through trial and error and a lot of research.
Applicable source code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cameras: MonoBehaviour {
    GameObject targetObj;
    Vector3 targetPos;
    void Start ()
    {
        targetObj = GameObject.Find ("Player");
        targetPos = targetObj.transform.position;
    }
    void Update ()
    {
        // Move yourself (camera) by the amount of target movement
        transform.position + = targetObj.transform.position-targetPos;
        targetPos = targetObj.transform.position;
            // amount of mouse movement
            float mouseInputX = Input.GetAxis ("Mouse X");
            float mouseInputY = Input.GetAxis ("Mouse Y");
            // Rotate (revolve) around the Y axis at the target position
            transform.RotateAround (targetPos, Vector3.up, mouseInputX * Time.deltaTime * 50f);
// Move the camera vertically
        transform.RotateAround (targetPos, transform.right, -mouseInputY * Time.deltaTime * 50f);
}
}
It becomes a sentence like this.
I'd like to know what you can write in this sentence to limit the camera angle.
Thank you.

if (mouseInputY<= MaxUp) // When facing up
{
this.transform.rotation = Quaternion.Euler (startX-30, 0, 0);
mouseInputY = -30;
}

I tried this, but first I can't get inside the IF statement.

  • Answer # 1

    How about adding the horizontal and vertical rotation angles so that you can see how much you have rotated from the initial angle?
    I think that the rotation amount given to RotateAround should be adjusted so that this accumulated rotation angle does not exceed -50 ° to + 50 ° horizontal and -30 ° to + 30 ° vertical.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Cameras: MonoBehaviour
    {
        GameObject targetObj;
        Vector3 targetPos;
        // Add a field so that you can remember the accumulated rotation angle separately
        float angleH;
        float angleV;
        void Start ()
        {
            targetObj = GameObject.Find ("Player");
            targetPos = targetObj.transform.position;
        }
        void Update ()
        {
            // Move yourself (camera) by the amount of target movement
            transform.position + = targetObj.transform.position-targetPos;
            targetPos = targetObj.transform.position;
            // amount of mouse movement
            float mouseInputX = Input.GetAxis ("Mouse X");
            float mouseInputY = Input.GetAxis ("Mouse Y");
            // Horizontal/vertical rotation angle calculated from the amount of mouse movement (same as the third argument of RotateAround in the questioner's code)
            float deltaAngleH = mouseInputX * Time.deltaTime * 50f;
            float deltaAngleV = -mouseInputY * Time.deltaTime * 50f;
            // Accumulate the angle
            angleH + = deltaAngleH;
            angleV + = deltaAngleV;
            // Clamp the total angle within limits
            float clampedAngleH = Mathf.Clamp (angleH, -50, 50);
            float clampedAngleV = Mathf.Clamp (angleV, -30, 30);
            // Subtract the accumulated angle after clamping from the accumulated angle before clamping to find out how much the limit range was exceeded
            // If it is within the limit, the difference will be 0, but if it exceeds the minus side, it will be negative, if it is positive, a positive angle difference will be obtained
            float overshootH = angleH-clampedAngleH;
            float overshootV = angleV-clampedAngleV;
            // Adjust the amount of rotation by the angle difference so that the limit is not exceeded
            // Overwrite the integrated angle with the adjusted value
            deltaAngleH-= overshootH;
            deltaAngleV-= overshootV;
            angleH = clampedAngleH;
            angleV = clampedAngleV;
            // Rotate (revolve) around the Y axis at the target position
            transform.RotateAround (targetPos, Vector3.up, deltaAngleH);
            // Move the camera vertically
            transform.RotateAround (targetPos, transform.right, deltaAngleV);
        }
    }