按AWSD来移动,当然方向键也可以
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class run : MonoBehaviour
{
public float speed=1;
public Rigidbody2D rigidbody2D;
private float x, y;
// Start is called before the first frame update
void Start()
{
rigidbody2D = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
playerRun();
}
void playerRun() {
x = Input.GetAxisRaw("Horizontal");
y = Input.GetAxisRaw("Vertical");
Vector2 pos = new Vector2(x, y);
rigidbody2D.velocity = pos * speed;
}
}