所以基本上,我试图在RPG Maker中制作一个具有8个方向的玩家移动系统。我以某种方式成功,但仅部分成功。当我试图突然改变方向时。从上到左,字符断断续续,不想先移动所有键就移动。
重力比例已禁用或更像设置为0,主体类型是动态的。
这是一个代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveChar : MonoBehaviour {
Rigidbody2D rigid;
public float Speed;
// Use this for initialization
void Start () {
rigid = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
// float horiz = Input.GetAxis("Horizontal");
// float vert = Input.GetAxis("Vertical");
if(Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP
{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if(Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(0, 0);
}
if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(0, 0);
}
if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, 0);
}
if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, 0);
}
}
答案 0 :(得分:2)
使用Input.GetAxis(axisName)
来避免输入代码中的大小写冲突。另外,请使用AddForce与其他刚体完美搭配。
Vector2 oldV = rigid.velocity;
float horiz = Input.GetAxis("Horizontal");
float vert = Input.GetAxis("Vertical");
Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);
或者,按住键时,请跟踪自己的轴
public float horiz;
public float vert;
void Start() {
horiz = 0f;
vert = 0f;
if (Input.GetKey(KeyCode.A)) horiz -= 1f;
if (Input.GetKey(KeyCode.D)) horiz += 1f;
if (Input.GetKey(KeyCode.S)) vert -= 1f;
if (Input.GetKey(KeyCode.W)) vert += 1f;
}
void Update () {
Vector2 oldV = rigid.velocity;
if(Input.GetKeyDown(KeyCode.W)) vert += 1f;
else if(Input.GetKeyUp(KeyCode.W)) vert -= 1f;
if (Input.GetKeyDown(KeyCode.S)) vert -= 1f;
else if (Input.GetKeyUp(KeyCode.S)) vert += 1f;
if (Input.GetKeyDown(KeyCode.A)) horiz -= 1f;
else if (Input.GetKeyUp(KeyCode.A)) horiz += 1f;
if (Input.GetKeyDown(KeyCode.D)) horiz += 1f;
else if (Input.GetKeyUp(KeyCode.D)) horiz -= 1f;
Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);
}
答案 1 :(得分:0)
释放按钮只有一个小问题。在不受键影响的方向上施加原始力。整个脚本应如下所示:
void Update()
{
if (Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP
{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}
if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}
if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}
if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}
}
希望对您有帮助。