unity3d驾驶游戏
时间: 2024-01-31 12:10:01 浏览: 33
以下是一个简单的Unity3D驾驶游戏的示例代码:
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
public float speed = 10f;
public float rotationSpeed = 100f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(0f, 0f, moveVertical) * speed * Time.deltaTime;
Quaternion rotation = Quaternion.Euler(0f, moveHorizontal * rotationSpeed * Time.deltaTime, 0f);
rb.MovePosition(rb.position + transform.TransformDirection(movement));
rb.MoveRotation(rb.rotation * rotation);
}
}
```
这个示例代码实现了一个简单的驾驶游戏,玩家可以使用键盘的方向键来控制汽车的移动和转向。通过获取玩家的输入,计算出汽车的移动和旋转的变化量,然后使用Rigidbody组件来实现汽车的运动。