Unity第三人称控制器

1、挂载在摄像机上的脚本:MyCamera.cs

挂载在摄像机上

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

public class MyCamera : MonoBehaviour
{
    [Header("控制摄像机旋转的灵敏度")]
    public float mouseMoveSpeed = 2f;

    [Header("摄像机要跟随的物体")]
    public Transform playerTransfrom;

    [Header("被跟随物体与摄像机之间的距离")]
    public float direction = 5f;

    private float moveX;

    private float moveY;

    private void Update()
    {
        moveX += Input.GetAxis("Mouse X") * mouseMoveSpeed;

        moveY -= Input.GetAxis("Mouse Y") * mouseMoveSpeed;

        moveY = Mathf.Clamp(moveY, -90f, 90f);//限制旋转角度的范围,使其不超过一定的最大值和最小值

        transform.eulerAngles = new Vector3(moveY, moveX, 0);

        transform.position = playerTransfrom.position - transform.forward * direction;
    }
}

2、挂载在角色上的脚本:Player.cs

角色上的脚本

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

public class Player : MonoBehaviour
{
    public float smoothTime = 0.3f;

    public float walkSpeed = 2f;

    private float currentVelocity;

    private Transform cameraTransfrom;

    // Start is called before the first frame update
    void Start()
    {
        cameraTransfrom = Camera.main.transform;

        Cursor.lockState = CursorLockMode.Locked;  //将鼠标锁定在屏幕中心位置,鼠标的移动不再对游戏视图产生影响
    }

    // Update is called once per frame
    void Update()
    {
        Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

        Vector2 inputDir = input.normalized;

        float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraTransfrom.eulerAngles.y;

        //判断键盘是否按下
        if (inputDir != Vector2.zero)
        {
            transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref currentVelocity, smoothTime);
            transform.Translate(transform.forward * walkSpeed * Time.deltaTime, Space.World);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity第三人是指在Unity引擎中,通过设置相机的目标物体(通常是角色)并创建一个简单的动画状态机,实现对角色进行第三人视角的控制。这样可以让玩家在游戏中以第三人的视角观察和控制角色。你可以参考提供的demo和GitHub链接了解更多关于Unity第三人控制的实现方法。在第一人视角时,有些游戏物体需要进行隐藏,比如第三人手臂,为了避免在第一人视角下看到第三人手臂的问题,需要进行相应的设置。你可以按照这些设置来创建和添加物品种类并进行相应的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Unity第三人控制实现方式](https://blog.csdn.net/weixin_47260762/article/details/123903137)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Unity插件-第三人控制(ThirdPersonController)使用(一)](https://blog.csdn.net/wangjianxin97/article/details/87913131)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值