【问题标题】:Movement script does not work (unity, C#)移动脚本不起作用(统一,C#)
【发布时间】:2021-05-25 16:23:53
【问题描述】:

我的目标是: Launching Ray> Ray found Collider> 添加这个对撞机变量> 创建Veсtor2.Lerp,其中的端点是一个transform the collider,找到了。 该脚本有效,但不是应有的。玩家不动 (对不起,愚蠢的错误,我是初学者)

public class move : MonoBehaviour
{
  private float speed = 150f;
  private Rigidbody2D rb2d;
  Transform EndPoint;
  private bool ready;

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

  void Update() {
    MoveInfoInsert();
  } 

  void FixedUpdate() {
    Moving();
  }

  void MoveInfoInsert() { 
    if(Input.GetAxisRaw("Horizontal") != 0) { 
      rb2d.constraints = RigidbodyConstraints2D.FreezePositionY;
      ready = true;
      if(Input.GetAxisRaw("Horizontal") > 0) {                     
        RaycastHit2D RightHit = Physics2D.Raycast(transform.position, transform.right * 1000f, LayerMask.GetMask("FinishForMove"));
        if(RightHit.collider != null) {
          EndPoint = RightHit.transform;
        }
      }
      else {                                                       
        RaycastHit2D LeftHit = Physics2D.Raycast(transform.position, -transform.right * 1000f, LayerMask.GetMask("FinishForMove"));
        if(LeftHit.collider != null) {
          EndPoint = LeftHit.transform;
        }
      }
    }
    if(Input.GetAxisRaw("Vertical") != 0) {                        
      rb2d.constraints = RigidbodyConstraints2D.FreezePositionX;
      ready = true;
      if(Input.GetAxisRaw("Vertical") > 0) {                       
        RaycastHit2D UpHit = Physics2D.Raycast(transform.position, transform.up * 1000f, LayerMask.GetMask("FinishForMove"));
        if(UpHit.collider != null) {
          EndPoint = UpHit.transform;
        }
      }
      else {                                                       
        RaycastHit2D DownHit = Physics2D.Raycast(transform.position, -transform.up * 1000f, LayerMask.GetMask("FinishForMove"));
        if(DownHit.collider != null) {
          EndPoint = DownHit.transform;
        }
      }                                                                                   
    }
  }

  void Moving() {
    if(ready == true) {
      Vector2.Lerp(transform.position, EndPoint.position, Time.fixedDeltaTime * speed);
      rb2d.constraints = RigidbodyConstraints2D.FreezeRotation;
    }
  }
}


    

    

【问题讨论】:

  • 一般来说,你应该简单地设置和重置(设置nullEndPoint,而不是ready,然后使用if(EndPoint)
  • @derHugo,我试过你写的。不工作。我还决定尝试进行“检查”(在控制台中),并注意到 RayCast 正在注册甚至不存在的对撞机。也许是他们?也许我以某种方式错误配置了它们?
  • 我找到了解决问题的方法。感谢 derHugo 和 Armin。首先,事实证明,在下一个参数中,您需要指定光线的长度,并且方向本身不需要乘以 1000。其次,我遇到了层的问题,我解决了。我也遇到了 ready 变量的问题,它根本不需要,只会干扰工作。 if也有一点问题

标签: c# unity3d


【解决方案1】:

试试这个,看看这是不是你想做的:

public class move : MonoBehaviour
{

    private float speed = 150f;
    private Rigidbody2D rb2d = null;
    private Transform EndPoint = null;
    private bool ready = false;
    private LayerMask layerMask = 0;

    private void Start()
    {
        layerMask = LayerMask.NameToLayer("FinishForMove");
        rb2d = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        MoveInfoInsert();
    }

    private void FixedUpdate()
    {
        Moving();
    }

    private void MoveInfoInsert()
    {
        if (Input.GetAxisRaw("Horizontal") != 0)
        {
            rb2d.constraints = RigidbodyConstraints2D.FreezePositionY;
            ready = true;
            if (Input.GetAxisRaw("Horizontal") > 0)
            {
                RaycastHit2D RightHit = Physics2D.Raycast(transform.position, transform.right * 1000f, layerMask);
                if (RightHit.collider != null)
                {
                    EndPoint = RightHit.transform;
                }
            }
            else
            {
                RaycastHit2D LeftHit = Physics2D.Raycast(transform.position, -transform.right * 1000f, layerMask);
                if (LeftHit.collider != null)
                {
                    EndPoint = LeftHit.transform;
                }
            }
        }
        if (Input.GetAxisRaw("Vertical") != 0)
        {
            rb2d.constraints = RigidbodyConstraints2D.FreezePositionX;
            ready = true;
            if (Input.GetAxisRaw("Vertical") > 0)
            {
                RaycastHit2D UpHit = Physics2D.Raycast(transform.position, transform.up * 1000f, layerMask);
                if (UpHit.collider != null)
                {
                    EndPoint = UpHit.transform;
                }
            }
            else
            {
                RaycastHit2D DownHit = Physics2D.Raycast(transform.position, -transform.up * 1000f, layerMask);
                if (DownHit.collider != null)
                {
                    EndPoint = DownHit.transform;
                }
            }
        }
    }

    private void Moving()
    {
        if(EndPoint == null)
        {
            return;
        }
        if (ready == true)
        {
            transform.position = Vector2.Lerp(transform.position, EndPoint.position, Time.fixedDeltaTime * speed);
            rb2d.constraints = RigidbodyConstraints2D.FreezeRotation;
        }
    }

}

【讨论】:

    【解决方案2】:

    我必须说,这是制作使用刚体的运动脚本的一种不同寻常的方式。你可以做的是在轴上增加力。例如,如果玩家按下 a,脚本会知道并从 z 轴添加一个力。对于跳跃,您可以进行地面检查。

    这里有几个动作脚本教程,解释了如何制作一个好的动作脚本:

    https://www.youtube.com/watch?v=dwcT-Dch0bA

    这个视频是关于如何使用物理来制作一个完整的 2D 玩家移动脚本。

    https://www.youtube.com/watch?v=wi-RL4sUayo

    这一篇是关于如何制作 2D 玩家移动脚本的不同方法,我保证,这会对你有很大帮助!

    https://www.youtube.com/watch?v=n4N9VEA2GFo

    最后但并非最不重要的一点是,这篇文章是关于如何使用玩家的变换制作 2D 玩家移动脚本。

    制作移动脚本主要有两种方法(使用物理和使用玩家的变换)。看完这些视频,我想你会找到制作最适合自己的动作脚本的最佳方法。

    希望这会有所帮助! :D

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 2022-07-13
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多