怎样才能阻止我的2D角色在移动后滑行

时间:2016-03-28 01:11:14

标签: c# unity3d 2d

我目前正在制作2D自上而下的生存游戏。我已经对播放器控制器进行了编码,除了一个可以防止我丢失的问题之外,它还运行良好!每当我停止按下移动键时,我的角色不会停止移动(或减速非常缓慢)。我怎样才能立即停止,如果不是立即停止?任何提示都有助于谢谢 这是我正在使用的代码!

using UnityEngine;
using System.Collections;

public class CharacterMovement : MonoBehaviour{
public float speed;
private Rigidbody2D rb2d;

void Start () 
{
rb2d = GetComponent<Rigidbody2D> ();
}

void FixedUpdate()
{ 
float moveHorizontal = Input.GetAxisRaw ("Horizontal");
float moveVertical = Input.GetAxisRaw ("Vertical");

Vector2 movement = new Vector2 (moveHorizontal, moveVertical);

rb2d.AddForce (movement * speed);
}
}

1 个答案:

答案 0 :(得分:0)

我找到了一种增加减速度的方法。我废弃了我的代码并更改了机制,以便角色跟随鼠标并向前移动&#34; W&#34;键或向上箭头。这样可以平滑方向变化并使游戏更加愉快,对于减速,我通过使刚体2D检测器中的线性拖动不止一个并增加速度来弥补速度损失来解决问题。 这是我未来求职者的代码!

using UnityEngine;
using System.Collections;

public class CharacterMovement : MonoBehaviour{
public float speed;
private Rigidbody2D rbtd;

void Start () 
{
    rbtd = GetComponent<Rigidbody2D> ();
}

void FixedUpdate()
{
    var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition,
                                            Vector3.forward);

    transform.rotation = rot;
    transform.eulerAngles = new Vector3 (0, 0, transform.eulerAngles.z);
    rbtd.angularVelocity = 0;

    float input = Input.GetAxis ("Vertical");
    rbtd.AddForce (gameObject.transform.up * speed * input);
}
}