简单的第三人称人物和摄像机控制

首先,交代一下背景:人物只有一个idel和一个原地踏步walk的动画,要实现行走,摄像机跟随以及360度视角。

人物控制使用的CharacterController。

首先获取相机与人物的相对位置

 offset = transform.position - target.transform.position;
startEulerAngles = target.transform.localEulerAngles;
offsetForRotate = offset;

根据按键获取移动方向,并转化为欧拉角赋值给人物

direction = new Vector3(Input.GetAxis("Vertical"), 0, -Input.GetAxis("Horizontal")).normalized;
            target.transform.eulerAngles = new Vector3(0, Quaternion.FromToRotation(Vector3.forward, direction).eulerAngles.y, 0)
                + new Vector3(0, transform.eulerAngles.y, 0)
                - startEulerAngles;

旋转简单的写了水平360旋转,由于旋转会改变相对关系,所以需要实时回去当前的差值

if (Input.GetMouseButton(1))
        {
            transform.position = target.transform.position + offsetForRotate;
            transform.RotateAround(target.transform.position, target.transform.up, Input.GetAxis("Mouse X"));
            offsetForRotate = transform.position - target.transform.position;
        }

整体代码:

using UnityEngine;

public class CameraController: MonoBehaviour {

    public CharacterController target;//Player
    [HideInInspector]
    public bool isCanControl = true;
    [SerializeField]
    private float speed = 0.05f;

    private Vector3 direction;//按键方向
    private Vector3 startEulerAngles;//初始旋转
    private Vector3 offset;//初始相对位置
    private Vector3 offsetForRotate;//旋转后的相对位置

    private void Awake () 
	{
        offset = transform.position - target.transform.position;
	}

    private void OnEnable()
    {
        startEulerAngles = target.transform.localEulerAngles;
        offsetForRotate = offset;
    }

    private void FixedUpdate () 
	{
        if (!isCanControl)
            return;

        Move();
        CameraFollow();
    }

    /// <summary>
    /// 人物移动
    /// </summary>
    private void Move()
    {
        if (Mathf.Abs(Input.GetAxis("Vertical")) > 0 || Mathf.Abs(Input.GetAxis("Horizontal")) > 0)
        {
            direction = new Vector3(Input.GetAxis("Vertical"), 0, -Input.GetAxis("Horizontal")).normalized;
            target.transform.eulerAngles = new Vector3(0, Quaternion.FromToRotation(Vector3.forward, direction).eulerAngles.y, 0)
                + new Vector3(0, transform.eulerAngles.y, 0)
                - startEulerAngles;

            target.GetComponent<Animation>().Play("03_走");
            target.Move(target.transform.forward * speed);
        }
        else
            target.GetComponent<Animation>().Play("02_站立");
    }

    /// <summary>
    /// 摄像机旋转跟随
    /// </summary>
    private void CameraFollow()
    {
        if (Input.GetMouseButton(1))
        {
            transform.position = target.transform.position + offsetForRotate;
            transform.RotateAround(target.transform.position, target.transform.up, Input.GetAxis("Mouse X"));
            offsetForRotate = transform.position - target.transform.position;
        }

        transform.position = target.transform.position + offsetForRotate;
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

末零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值