Unity中控制摄像机跟踪游戏角色(插值柔和追踪+旋转)

开发背景

我们准备学习Rotate() ,Lookat() ,RoateAround() 三个方法,请你给我设计一个简单的游戏,只用到这三个方法即可,简简单单,并且单独告诉我场景的搭建需求!

当然可以!这里有一个简单的游戏案例,可以用来练习Unity中的Rotate(), LookAt()和RotateAround()方法:

游戏名称:太空漫游

**游戏目标:**控制太空飞船在太空中漫游,观察不同的星球。

游戏玩法:

  1. 在游戏场景中创建一个太空飞船和若干个星球。
  2. 玩家可以使用键盘上的方向键控制太空飞船的移动方向。
  3. 使用Rotate()方法来旋转太空飞船,使其朝向移动方向。
  4. 当太空飞船靠近一个星球时,摄像机自动切换到观察模式。
  5. 在观察模式下,摄像机使用LookAt()方法始终看向星球,使用RotateAround()方法绕着星球旋转。

场景搭建需求:

  • 一个太空飞船模型,可以使用Unity Asset Store中的免费资源。
  • 若干个星球模型,可以使用Unity Asset Store中的免费资源。
  • 一个摄像机,用于观察场景。

这个游戏案例简单易懂,可以让学生练习使用Unity中的Rotate(), LookAt()和RotateAround()方法。希望对你有所帮助!

在这个游戏案例中,我们可能需要两个脚本:一个用于控制太空飞船的移动,另一个用于控制摄像机的行为。下面是这两个脚本的示例:

1.太空飞船控制脚本:

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

public class SpaceshipController : MonoBehaviour
{
    public float speed = 10.0f; // 飞船的移动速度

    void Update()
    {
        // 获取玩家的输入
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        // 计算飞船的新位置
        Vector3 newPosition = new Vector3(horizontalInput, 0, verticalInput) * speed * Time.deltaTime;

        // 移动飞船
        transform.Translate(newPosition);

        // 旋转飞船以朝向移动方向
        if (newPosition != Vector3.zero)
        {
            Quaternion toRotation = Quaternion.LookRotation(newPosition, Vector3.up);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, speed * Time.deltaTime);
        }
    }
}

2.摄像机控制脚本:

!这是带有详细注释的摄像机跟随游戏主角的脚本:

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

public class CameraController : MonoBehaviour
{
    public Transform target; // 观察目标
    public float distance = 10.0f; // 观察距离
    public float height = 5.0f; // 观察高度
    public float heightDamping = 2.0f; // 高度阻尼
    public float rotationDamping = 3.0f; // 旋转阻尼

    void LateUpdate()
    {
        if (!target)
            return;

        // 计算摄像机的位置和旋转角度
        float wantedRotationAngle = target.eulerAngles.y; // 目标的y轴旋转角度
        float wantedHeight = target.position.y + height; // 目标的高度加上预设高度
        
        float currentRotationAngle = transform.eulerAngles.y; // 当前摄像机的y轴旋转角度
        float currentHeight = transform.position.y; // 当前摄像机的高度
        
        // 使用Lerp函数平滑过渡角度和高度
        currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
        
        currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
        
        // 创建一个旋转四元数
        Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
        
        // 设置摄像机的位置
        transform.position = target.position;
        transform.position -= currentRotation * Vector3.forward * distance;
        
        transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
        
        // 让摄像机始终看向目标
        transform.LookAt(target);
    }
}

这个脚本中,LateUpdate 方法在每一帧更新时被调用。在这个方法中,我们首先检查是否设置了观察目标。如果没有设置观察目标,就直接返回。

然后,我们计算了摄像机应该有的旋转角度和高度。这些值是根据观察目标的位置和旋转角度计算出来的。

接下来,我们使用 Mathf.LerpAngleMathf.Lerp 方法来平滑地过渡摄像机的旋转角度和高度。这两个方法都接受三个参数:当前值、目标值和过渡速度。它们会返回一个新的值,这个值是当前值向目标值过渡的结果。

然后,我们创建了一个新的旋转四元数,并使用这个四元数来设置摄像机的位置。

最后,我们使用 transform.LookAt 方法让摄像机始终看向观察目标。

希望这个解释和注释能帮助你理解这个脚本!如果你有其他问题或者需要更多的帮助,随时告诉我!

3.第二种摄像机跟踪脚本

这是一个简单的Unity脚本,脚本中用到的target 变量是需要外部赋值,这个游戏物体是作为主角的子物体,并且放在主角背后上方,是将来摄像机跟踪到的位置!

可以让摄像机平滑地跟踪玩家角色:

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

public class SmoothFollow : MonoBehaviour
{
    public Transform target; // 跟踪目标
    public float smoothTime = 0.3f; // 平滑时间

    private Vector3 velocity = Vector3.zero;

    void Update()
    {
        // 计算新的位置
        Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10));
        // 平滑移动到新的位置
        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
      transform.LookAt(target);
    }
}

这个脚本定义了一个 SmoothFollow 类,继承自 MonoBehaviour。它有两个公共变量:target 表示跟踪目标,smoothTime 表示平滑时间。

Update 方法中,我们首先计算了摄像机的新位置。这个位置是跟踪目标的位置加上一个偏移量。然后,我们使用 Vector3.SmoothDamp 方法来平滑地移动摄像机到新的位置。

你可以根据需要修改这个脚本,以实现更复杂的跟踪效果。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Unity3d青子

难题的解决使成本节约,求打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值