我们准备学习Rotate() ,Lookat() ,RoateAround() 三个方法,请你给我设计一个简单的游戏,只用到这三个方法即可,简简单单,并且单独告诉我场景的搭建需求!
当然可以!这里有一个简单的游戏案例,可以用来练习Unity中的Rotate(), LookAt()和RotateAround()方法:
游戏名称:太空漫游
**游戏目标:**控制太空飞船在太空中漫游,观察不同的星球。
游戏玩法:
场景搭建需求:
这个游戏案例简单易懂,可以让学生练习使用Unity中的Rotate(), LookAt()和RotateAround()方法。希望对你有所帮助!
在这个游戏案例中,我们可能需要两个脚本:一个用于控制太空飞船的移动,另一个用于控制摄像机的行为。下面是这两个脚本的示例:
- 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);
- }
- }
- }
!这是带有详细注释的摄像机跟随游戏主角的脚本:
- 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.LerpAngle 和 Mathf.Lerp 方法来平滑地过渡摄像机的旋转角度和高度。这两个方法都接受三个参数:当前值、目标值和过渡速度。它们会返回一个新的值,这个值是当前值向目标值过渡的结果。
然后,我们创建了一个新的旋转四元数,并使用这个四元数来设置摄像机的位置。
最后,我们使用 transform.LookAt 方法让摄像机始终看向观察目标。
希望这个解释和注释能帮助你理解这个脚本!如果你有其他问题或者需要更多的帮助,随时告诉我!
这是一个简单的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 方法来平滑地移动摄像机到新的位置。
你可以根据需要修改这个脚本,以实现更复杂的跟踪效果。