unity3D爬楼梯代码
时间: 2023-10-22 10:03:41 浏览: 109
unity3d 代码
5星 · 资源好评率100%
以下是一个简单的 Unity3D 爬楼梯的代码示例:
```csharp
using UnityEngine;
public class StairClimber : MonoBehaviour
{
public float stepHeight = 0.3f;
public float stepDuration = 0.2f;
public float stepDistance = 1.0f;
public float climbSpeed = 2.0f;
private Vector3 targetPosition;
private bool isClimbing = false;
private float climbStartTime;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && !isClimbing)
{
StartClimbing();
}
if (isClimbing)
{
float t = (Time.time - climbStartTime) / stepDuration;
transform.position = Vector3.Lerp(transform.position, targetPosition, t * climbSpeed);
if (t >= 1.0f)
{
isClimbing = false;
}
}
}
private void StartClimbing()
{
targetPosition = transform.position + (transform.up * stepHeight) + (transform.forward * stepDistance);
climbStartTime = Time.time;
isClimbing = true;
}
}
```
这个脚本可以附加到一个角色或其他对象上,使它能够爬上楼梯。当按下空格键时,对象将开始爬行,每个步骤将持续一定的时间,直到到达目标位置。你可以通过调整公共变量的值来改变步骤的高度、速度和距离。
阅读全文