帮我写一个unity3d的角色移动脚本
时间: 2024-02-13 13:03:42 浏览: 153
好的,以下是一个简单的Unity3D角色移动脚本,你可以根据需要进行修改和优化:
```csharp
using UnityEngine;
public class CharacterMovement : MonoBehaviour
{
public float moveSpeed = 5f; // 移动速度
public float jumpForce = 500f; // 跳跃力量
private Rigidbody2D rb;
private Animator anim;
private bool isGrounded = true;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
private void Update()
{
// 获取水平输入
float moveInput = Input.GetAxisRaw("Horizontal");
// 设置动画参数
anim.SetFloat("Speed", Mathf.Abs(moveInput));
// 移动角色
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
// 检测是否着地
isGrounded = Physics2D.Raycast(transform.position, Vector2.down, 0.1f);
// 处理跳跃
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(new Vector2(0f, jumpForce));
}
}
}
```
这个脚本实现了基本的角色移动和跳跃功能。你可以将它添加到角色对象上,并调整相应的参数来满足你的需求。
阅读全文
相关推荐

















