unity控制物体移动
player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
private float horizontalInput;
private Rigidbody rigidbodyComponent;
// Start is called before the first frame update
void Start()
{
rigidbodyComponent = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
}
private void FixedUpdate()
{
rigidbodyComponent.velocity = new Vector3(horizontalInput, rigidbodyComponent.velocity.y , 0);
}
}
这篇博客介绍了如何在Unity引擎中通过Rigidbody组件控制游戏对象沿水平轴移动。作者在`Update`方法中获取玩家输入,并在`FixedUpdate`中更新物体速度,实现了基于用户输入的实时移动效果。
4160

被折叠的 条评论
为什么被折叠?



