Unity3D之第三人称视角相机跟随

前言

这次实现类似绝地求生这个游戏中的 第三人称视角相机跟随

角色层级

直接把相机挂载到角色模型作为子级

调整好角度与位置,初步实现第三人称视角

挂载脚本

  • PlayerController.cs 挂载到Player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    
    
  public float rotationSpeed = 200f;
  private float mouseX;
  private float mouseY;

  void Start()
  {
    
    
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
  }

  void Update()
  {
    
    
  	mouseX = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
    transform.Rotate(new Vector3(0f, mouseX, 0f), Space.Self);

    mouseY -= Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
    mouseY = Mathf.Clamp(mouseY, -40f, 40f);
    Camera.main.transform.localEulerAngles = new Vector3(mouseY, 0, 0);
  }
}

实现效果

后话

对不同的需求有不同的相机跟随的实现方法,灵活运用才是正解

Enjoy~

猜你喜欢

转载自blog.csdn.net/a924282761/article/details/130836210