using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
//速度
public int speed;
private Rigidbody2D _rigidbody;
private float _posX;
private float _posY;
private Vector2 _currentVector;
// Start is called before the first frame update
void Start()
{
//获得刚体组件
_rigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Movement();
}
private void FixedUpdate()
{
Vector2 _playerPos = _rigidbody.position;
_playerPos += _currentVector * speed * Time.deltaTime;
_rigidbody.MovePosition(_playerPos);
}
void Movement()
{
//获得方向输入
_posX = Input.GetAxis("Horizontal");
_posY = Input.GetAxis("Vertical");
Vector2 _inputVector = new Vector2(_posX, _posY);
_currentVector = _inputVector;
}
}
基础向——使用Rigidbody.MovePosition()方法实现物体移动
最新推荐文章于 2024-11-02 10:26:54 发布