Unity3D制作3dRPG游戏——镜头控制

Unity3D制作3dRPG游戏——镜头控制

镜头控制

  • Unity3D制作3dRPG游戏——镜头控制
  • 直接控制镜头围绕玩家角色旋转
  • 控制镜头跟随鼠标上下左右滑动围绕玩家角色旋转
  • 控制镜头跟随鼠标中键滚轮拉动视角
  • 控制镜头上下拉动的角度上限

直接控制镜头围绕玩家角色旋转

利用this.gameObject获得相机;
利用tag找到并获取给变量Player(需要在unity中给角色添加tag);
利用Came.transform.LookAt(Player.transform.position)使镜头对准玩家角色;
利用Came.transform.RotateAround(Player.transform.position, Player.transform.up, Input.0.1f )让摄像机围绕角色为中心旋转;

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

public class CameraPlayer : MonoBehaviour
{
    GameObject Came;
    GameObject Player;
    Vector3 off;
    public float Speed = 1// Start is called before the first frame update
    void Start()
    {
        Came = this.gameObject;
        Player = GameObject.FindGameObjectWithTag("Kara");
        off = Player.transform.position - Came.transform.position;
        Came.transform.LookAt(Player.transform.position);
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(1))
        {  
            Came.transform.RotateAround(Player.transform.position, Player.transform.up, Input.0.1f );  
        }
    }
}

控制镜头跟随鼠标上下左右滑动围绕玩家角色旋转

在unity的edit->project setting->input->Axis中提供了各种控制选项
Input.GetAxis(“Mouse Y”)即为获得鼠标纵向移动的移动量,横向移动同理。

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

public class CameraPlayer : MonoBehaviour
{
    GameObject Came;
    GameObject Player;

    Vector3 off;
    public float Speed = 1;
    
    // Start is called before the first frame update
    void Start()
    {
        Came = this.gameObject;
        Player = GameObject.FindGameObjectWithTag("Kara");
        off = Player.transform.position - Came.transform.position;
        Came.transform.LookAt(Player.transform.position);
    }
    // Update is called once per frame
    void Update()
    { 
        if (Input.GetMouseButton(1))
        {
            Came.transform.RotateAround(Player.transform.position, Player.transform.up, Input.GetAxis("Mouse X")*Speed );
            Came.transform.RotateAround(Player.transform.position, Came.transform.right, Input.GetAxis("Mouse Y") * Speed*-1);

        }
    }
}

控制镜头跟随鼠标中键滚轮拉动视角

同样在unity的edit->project setting->input->Axis中提供了滚轮操作;
视角远近拉动原理需要利用magnitude和normalized
magnitude的作用为返回向量的长度
normalized则是返回长度为1 的向量
而控制长度的数值从Input.GetAxis(“Mouse ScrollWheel”)即鼠标滚轮滚动的多少来赋值。
另外,还需要设置镜头拉近和镜头旋转的关系逻辑。若不设置,会出现混乱,即拉动镜头远近后,再调整镜头视角旋转,会无法再以角色为视角中心旋转。
因此添加一个bool变量来进行判断。

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

public class CameraPlayer : MonoBehaviour
{
    GameObject Came;
    GameObject Player;

    Vector3 off;
    public float Speed = 1;
    
    // Start is called before the first frame update
    void Start()
    {
        Came = this.gameObject;

        Player = GameObject.FindGameObjectWithTag("Kara");

        off = Player.transform.position - Came.transform.position;


        Came.transform.LookAt(Player.transform.position);
    }

    float distance;
    /// 
    /// 是否在旋转中
    /// 
    bool isRote = false; 

    // Update is called once per frame
    void Update()
    {
        if (!isRote)
        {
            Came.transform.position = Player.transform.position - off;
            Came.transform.position = Player.transform.position - off;
            distance = off.magnitude;  //返回向量的长度
            distance += Input.GetAxis("Mouse ScrollWheel");
            off = off.normalized * distance;  //返回长度为1的向量
        }
        
        
        if (Input.GetMouseButton(1))
        {
            isRote = true;
            Came.transform.RotateAround(Player.transform.position, Came.transform.right, Input.GetAxis("Mouse Y") * Speed*-1);
            Came.transform.RotateAround(Player.transform.position, Player.transform.up, Input.GetAxis("Mouse X")*Speed );
            //Came.transform.RotateAround(Player.transform.position, Player.transform.up, Input.GetAxis("Mouse X") * Speed);
            off = Player.transform.position - Came.transform.position;
        }
        else
        {
            isRote = false;
        }

        
    }
}

控制镜头上下拉动的角度上限

用欧拉角确定镜头的上下角度变化。
设定一个变量为镜头旋转角度作为中间量,将相机欧拉角赋值给它,利用数学公式mathf函数控制该变量的x值得范围,最后再把该变量回传给镜头的欧拉角,使其一直保持在规定范围之内。

//相机的旋转角度
    Vector3 RotorCame;
…………………………………………
			RotorCame = Came.transform.eulerAngles;
            RotorCame.x =  Mathf.Clamp(RotorCame.x,15, 40);
            Came.transform.eulerAngles = RotorCame;

你可能感兴趣的:(unity3d,游戏开发,c#)