实现效果
汽车漫游
1.创建汽车模型
导入汽车模型(FBX格式或其他3D格式),确保模型包含车轮、车身等部件。
为汽车添加碰撞体(如 Box Collider 或 Mesh Collider),避免穿透场景物体。
添加 Rigidbody 组件,启用重力并调整质量(Mass)以模拟物理效果。
2.编写汽车控制脚本
using UnityEngine;
using UnityEngine.UI; // 引入UI命名空间
public class CarController : MonoBehaviour
{
public float maxSpeed = 20f; // 最大速度
public float acceleration = 5f; // 加速度
public float turnSpeed = 10f; // 转向速度
public bool isRoamingEnabled = true; // 漫游开关状态
public Button roamToggleButton; // 绑定的UI按钮
public Text buttonText; // 按钮上的文字组件
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
// 绑定按钮点击事件
if (roamToggleButton != null)
{
roamToggleButton.onClick.AddListener(ToggleRoaming);
UpdateButtonText(); // 初始化按钮文字
}
}
void FixedUpdate()
{
if (isRoamingEnabled)
{
// 获取输入(仅在漫游启用时响应)
float moveInput = Input.GetAxis("Vertical"); // W/S 或 上下箭头
float turnInput = Input.GetAxis("Horizontal"); // A/D 或 左右箭头
// 加速/减速
Vector3 force = -transform.forward * moveInput * acceleration;
rb.AddForce(force, ForceMode.Acceleration);
// 限制最大速度
if (rb.velocity.magnitude > maxSpeed)
rb.velocity = rb.velocity.normalized * maxSpeed;
// 转向
float turn = turnInput * turnSpeed * Time.fixedDeltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
else
{
// 漫游禁用时自动减速
rb.velocity = Vector3.Lerp(rb.velocity, Vector3.zero, Time.fixedDeltaTime * 2f);
rb.angularVelocity = Vector3.Lerp(rb.angularVelocity, Vector3.zero, Time.fixedDeltaTime * 2f);
}
}
// 切换漫游开关状态
private void ToggleRoaming()
{
isRoamingEnabled = !isRoamingEnabled;
UpdateButtonText();
}
// 更新按钮文字显示
private void UpdateButtonText()
{
if (buttonText != null)
buttonText.text = isRoamingEnabled ? "停止漫游" : "开始漫游";
}
}
- 配置输入
打开 Edit > Project Settings > Input Manager,确保 Vertical 和 Horizontal 轴已正确绑定到键盘输入。
4.摄像机跟随
添加跟随脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCamera : MonoBehaviour
{
public Transform target;
public Vector3 offset = new Vector3(-60, 20, 0);
public float smoothTime = 0.3f;
public float rotationSmoothSpeed = 5f; // 旋转平滑系数
private Vector3 _velocity = Vector3.zero;
void LateUpdate()
{
Vector3 desiredPosition = target.position + offset;
transform.position = Vector3.SmoothDamp(
transform.position,
desiredPosition,
ref _velocity,
smoothTime
);
// 平滑旋转
Quaternion targetRotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(
transform.rotation,
targetRotation,
rotationSmoothSpeed * Time.deltaTime
);
}
}
步骤 1:创建 UI 按钮
在 Hierarchy 面板右键 > UI > Button,创建一个按钮。
调整按钮位置和文字:
选中按钮下的 Text 子对象,修改文字为 开始漫游。
在 Inspector 中调整按钮的 RectTransform 和颜色样式。
步骤 2:修改汽车控制脚本
在原有代码中添加 漫游状态开关 和 按钮点击事件响应逻辑:
using UnityEngine;
using UnityEngine.UI; // 引入UI命名空间
public class CarController : MonoBehaviour
{
public float maxSpeed = 20f; // 最大速度
public float acceleration = 5f; // 加速度
public float turnSpeed = 10f; // 转向速度
public bool isRoamingEnabled = true; // 漫游开关状态
public Button roamToggleButton; // 绑定的UI按钮
public Text buttonText; // 按钮上的文字组件
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
// 绑定按钮点击事件
if (roamToggleButton != null)
{
roamToggleButton.onClick.AddListener(ToggleRoaming);
UpdateButtonText(); // 初始化按钮文字
}
}
void FixedUpdate()
{
if (isRoamingEnabled)
{
// 获取输入(仅在漫游启用时响应)
float moveInput = Input.GetAxis("Vertical"); // W/S 或 上下箭头
float turnInput = Input.GetAxis("Horizontal"); // A/D 或 左右箭头
// 加速/减速
Vector3 force = -transform.forward * moveInput * acceleration;
rb.AddForce(force, ForceMode.Acceleration);
// 限制最大速度
if (rb.velocity.magnitude > maxSpeed)
rb.velocity = rb.velocity.normalized * maxSpeed;
// 转向
float turn = turnInput * turnSpeed * Time.fixedDeltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
else
{
// 漫游禁用时自动减速
rb.velocity = Vector3.Lerp(rb.velocity, Vector3.zero, Time.fixedDeltaTime * 2f);
rb.angularVelocity = Vector3.Lerp(rb.angularVelocity, Vector3.zero, Time.fixedDeltaTime * 2f);
}
}
// 切换漫游开关状态
private void ToggleRoaming()
{
isRoamingEnabled = !isRoamingEnabled;
UpdateButtonText();
}
// 更新按钮文字显示
private void UpdateButtonText()
{
if (buttonText != null)
buttonText.text = isRoamingEnabled ? "停止漫游" : "开始漫游";
}
}
步骤 3:绑定 UI 组件
选中汽车对象,找到 CarController 脚本组件。
将 Hierarchy 中的按钮拖拽到 Toggle Roam Button 字段。
展开按钮对象,将其子对象 Text 拖拽到 Button Text 字段。