@nyahiito (土肥 也汰)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

unityにて、カメラアングルをもっと上まで上げたいです

解決したいこと

unityでゲームを作っております。
ネットのunity講座で教えてもらったカメラスクリプトを使っているのですが、視野が上まで上がらなくて困っています。改造しようにも数学の知識が要るようで、手に余ってます。
足下は見れるのですが...

スクリプトを公開しますので、視点を上げる改造を教えていただけると幸いです。

発生している問題・エラー

視野が水平までしか上がらない。

ここまでが視点の上限です。
スクリーンショット 2021-03-13 21.02.30.png

該当するソースコード

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

public struct SphericalVector
{
    public float Lenght;
    public float Zenith;
    public float Azimuth;

    public SphericalVector(float azimuth, float zenith, float lenght)
    {
        Lenght = lenght;
        Zenith = zenith;
        Azimuth = azimuth;
    }

    public Vector3 Direction
    {
        get
        {
            Vector3 dir;

            float vertical_Angle = Zenith * Mathf.PI / 2f;
            dir.y = Mathf.Sin(vertical_Angle);
            float h = Mathf.Cos(vertical_Angle);

            float horizontal_Angle = Azimuth * Mathf.PI;
            dir.x = h * Mathf.Sin(horizontal_Angle);
            dir.z = h * Mathf.Cos(horizontal_Angle);

            return dir;
        }
    }

    public Vector3 Position
    {
        get
        {
            return Lenght * Direction;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Orbit : MonoBehaviour {

    public SphericalVector spherical_Vector_Data = new SphericalVector(0, 0, 1);

    protected virtual void Update() {
        transform.position = spherical_Vector_Data.Position;
    }

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

public class CameraOrbit : Orbit {

    public Vector3 target_Offset = new Vector3(0, 2, 0);
    public Vector3 camera_Position_Zoom = new Vector3(-0.5f, 0, 0);
    public float camera_Lenght = -10f;
    public float camera_Lenght_Zoom = -5f;
    public Vector2 orbit_Speed = new Vector2(0.01f, 0.01f);
    public Vector2 orbit_Offset = new Vector2(0, -0.8f);
    public Vector2 angle_Offset = new Vector2 (0, -0.25f);

    private float zoomValue;
    private Vector3 camera_Position_Temp;
    private Vector3 camera_Position;

    private Transform playerTarget;
    private Camera mainCamera;

    void Start () {
        playerTarget = GameObject.FindGameObjectWithTag ("Player").transform;

        spherical_Vector_Data.Lenght = camera_Lenght;
        spherical_Vector_Data.Azimuth = angle_Offset.x;
        spherical_Vector_Data.Zenith = angle_Offset.y;

        mainCamera = Camera.main;

        camera_Position_Temp = mainCamera.transform.localPosition;
        camera_Position = camera_Position_Temp;

        MouseLock.MouseLocked = true;

    }

    // Update is called once per frame
    void Update () {
        if (playerTarget) {
            HandleCamera ();
            HandleMouseLocking ();
        }
    }

    void HandleCamera() {
        if (MouseLock.MouseLocked) {
            spherical_Vector_Data.Azimuth += Input.GetAxis ("Mouse X") * orbit_Speed.x;
            spherical_Vector_Data.Zenith += Input.GetAxis ("Mouse Y") * orbit_Speed.y;
        }

        spherical_Vector_Data.Zenith = Mathf.Clamp (spherical_Vector_Data.Zenith + orbit_Offset.x,
            orbit_Offset.y, 0f);

        float distance_ToObject = zoomValue;
        float delta_Distance = Mathf.Clamp (zoomValue, distance_ToObject, -distance_ToObject);
        spherical_Vector_Data.Lenght += (delta_Distance - spherical_Vector_Data.Lenght);

        Vector3 lookAt = target_Offset;

        lookAt += playerTarget.position;

        base.Update ();

        transform.position += lookAt;
        transform.LookAt (lookAt);

        if (zoomValue == camera_Lenght_Zoom) {
            Quaternion targetRotation = transform.rotation;
            targetRotation.x = 0f;
            targetRotation.z = 0f;
            playerTarget.rotation = targetRotation;
        }

        camera_Position = camera_Position_Temp;
        zoomValue = camera_Lenght;
    }

    void HandleMouseLocking() {
        if (Input.GetKeyDown (KeyCode.Tab)) {
            if (MouseLock.MouseLocked) {
                MouseLock.MouseLocked = false;
            } else {
                MouseLock.MouseLocked = true;
            }
        }
    }

} // class

自分で試したこと

ここに問題・エラーに対して試したことを記載してください。

スクリプトとにらめっこするも数学がわからずダウン。
zenithとazimuthの意味だけ分かりました。

0 likes

1Answer

この行ですね

        spherical_Vector_Data.Zenith = Mathf.Clamp (spherical_Vector_Data.Zenith + orbit_Offset.x,
            orbit_Offset.y, 0f);

ClampでZenithの値が制限されているので、これをまるっと消せば際限なく視野を上げることができます。

0Like

Your answer might help someone💌