unity3d 划船交互的脚本
时间: 2023-07-29 15:07:16 浏览: 135
以下是一个基本的 Unity3D 划船交互脚本:
```csharp
using UnityEngine;
public class BoatController : MonoBehaviour
{
public float speed = 10f;
public float turnSpeed = 50f;
private float movementInputValue;
private float turnInputValue;
private Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
movementInputValue = Input.GetAxis("Vertical");
turnInputValue = Input.GetAxis("Horizontal");
}
private void FixedUpdate()
{
Move();
Turn();
}
private void Move()
{
Vector3 movement = transform.forward * movementInputValue * speed * Time.deltaTime;
rb.MovePosition(rb.position + movement);
}
private void Turn()
{
float turn = turnInputValue * turnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
rb.MoveRotation(rb.rotation * turnRotation);
}
}
```
这个脚本将允许玩家使用键盘控制划船的前后移动和转向。您可以将此脚本附加到船的 GameObject 上,并调整速度和转向速度参数以达到所需的效果。
阅读全文