我不知道为什么,但我的附加力不起作用,我有一个igidbody2d

我不知道为什么,但我的刚体上的 .addforce 不起作用。


我尝试按照官方的 unity addforce 教程进行操作。


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class ArrowController : MonoBehaviour

{

    public Rigidbody2D rb;

    public float speed = 5.0f;

    public Vector2 pos;


    void Start()

    {

        rb = GetComponent<Rigidbody2D>();

    }

    // Update is called once per frame

    void Update()

    {

        faceMouse();

        testForClick();

    }


    void faceMouse()

    {

        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        Vector3 differance = GameObject.Find("gunArm").transform.position - mousePos;

        float gunAngle = Mathf.Atan2(differance.y, differance.x) * Mathf.Rad2Deg;

        GameObject.Find("gunArm").transform.rotation = Quaternion.Euler(0, 0, gunAngle);

    }


    void testForClick()

    {

        if (Input.GetMouseButtonDown(0))

        {

            print("click");

            rb.AddForce(transform.forward);

        }

    }

}

我希望箭头在向前方向上添加力,但它只是打印出“单击”(我添加的消息是为了确保鼠标单击正常工作)。


holdtom
浏览 154回答 3
3回答

白衣非少年

我不知道为什么,但我创建了一个测试脚本:using System.Collections;using System.Collections.Generic;using UnityEngine;public class Test : MonoBehaviour{&nbsp; &nbsp; public Rigidbody2D rb;&nbsp; &nbsp; public float speed = 20.0f;&nbsp; &nbsp; // Start is called before the first frame update&nbsp; &nbsp; void Start()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; rb = GetComponent<Rigidbody2D>();&nbsp; &nbsp; }&nbsp; &nbsp; // Update is called once per frame&nbsp; &nbsp; void Update()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Input.GetMouseButtonDown(0))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("click");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rb.AddForce(transform.right * speed, ForceMode2D.Impulse);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; rotate();&nbsp; &nbsp; }&nbsp; &nbsp; private void rotate()&nbsp; &nbsp; {&nbsp; &nbsp; }}我还将我的旧脚本编辑为:using System.Collections;using System.Collections.Generic;using UnityEngine;public class ArrowController : MonoBehaviour{&nbsp; &nbsp; public Rigidbody2D rb;&nbsp; &nbsp; public float speed = 50.0f;&nbsp; &nbsp; public Vector2 pos;&nbsp; &nbsp; private void Start()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; rb = GetComponent<Rigidbody2D>();&nbsp; &nbsp; }&nbsp; &nbsp; private void Update()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; faceMouse();&nbsp; &nbsp; &nbsp; &nbsp; testForClick();&nbsp; &nbsp; }&nbsp; &nbsp; void FixedUpdate()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (doForce == true)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doForce = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rb.AddForce(transform.forward * speed, ForceMode2D.Impulse);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private bool doForce;&nbsp; &nbsp; private GameObject gunArm;&nbsp; &nbsp; private Camera cam;&nbsp; &nbsp; private void faceMouse()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // try to reuse the reference&nbsp; &nbsp; &nbsp; &nbsp; if (!cam) cam = Camera.main;&nbsp; &nbsp; &nbsp; &nbsp; var mousePos = cam.ScreenToWorldPoint(Input.mousePosition);&nbsp; &nbsp; &nbsp; &nbsp; // try to re-use the reference&nbsp; &nbsp; &nbsp; &nbsp; if (!gunArm) gunArm = GameObject.Find("gunArm");&nbsp; &nbsp; &nbsp; &nbsp; var difference = rb.transform.position - mousePos;&nbsp; &nbsp; &nbsp; &nbsp; var gunAngle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;&nbsp; &nbsp; &nbsp; &nbsp; rb.transform.rotation = Quaternion.Euler(0, 0, gunAngle);&nbsp; &nbsp; }&nbsp; &nbsp; void testForClick()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Input.GetMouseButtonDown(0))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("click");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // only set the flag&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doForce = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; void place()&nbsp; &nbsp; {&nbsp; &nbsp; }}并且测试本身无需旋转,并且在主脚本上仅旋转有效,因此我尝试同时激活两个脚本并且它开始工作,感谢有关此问题的所有帮助。

MMMHUHU

尽管事实上这isn't working是一个相当薄弱的描述:首先,您应该在 中执行此操作FixedUpdate,但在 中获取输入Update。其次减少Find调用Update..效率非常低。如果可能的话,最好通过检查器引用它们。否则,也许Start......我在这里展示的方式是延迟初始化的最后手段,假设您的脚本可能稍后在运行时生成,您可能想要传递ForceMode.Impulse给AddForce,因为您只添加一次力,而不是连续添加力。public class ArrowController : MonoBehaviour{    public Rigidbody2D rb;    public float speed = 5.0f;    public Vector2 pos;    // store and re-use references!    // would be better to already reference them via drag&drop    // in the Inspector    [SerializeField] private GameObject gunArm;    [SerializeField] private Camera cam;    private void Start()    {        rb = GetComponent<Rigidbody2D>();    }    private void Update()    {        testForClick();    }    private void FixedUpdate()    {        // also do this here        faceMouse();        if (doForce)        {            doForce = false;            rb.AddForce(transform.forward, ForceMode.Impulse);        }    }    private bool doForce;    private void faceMouse()    {        // try to reuse the reference        if(!cam) cam = Camera.main;        var mousePos = cam.ScreenToWorldPoint(Input.mousePosition);        // try to re-use the reference        if (!gunArm) gunArm = GameObject.Find("gunArm");        var difference = gunArm.transform.position - mousePos;        var gunAngle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;        gunArm.rotation = Quaternion.Euler(0, 0, gunAngle);    }    private void testForClick()    {        if (Input.GetMouseButtonDown(0))        {            print("click");            // only set the flag            doForce = true;        }    }}

月关宝盒

你的代码不做任何事情的原因不是因为它不起作用,而是因为transform.forward是一个大小为1的向量。添加大小为1的力对大多数物体不会有太大作用,并且摩擦力可能会减慢再次放下物体。尝试添加更高强度的力和 ForceMode.Impulse:float strength = 50f;rb.AddForce(transform.forward * strength, ForceMode.Impulse);更新:看起来您希望枪面向您的鼠标位置,这可能就是您的问题所在:让我们尝试使用 Quaternion.LookRotation 来使其工作,而不是手动进行数学计算。或许:GameObject gunArm;void Awake(){&nbsp; &nbsp; gunArm = GameObject.Find("gunArm");}void faceMouse(){&nbsp; &nbsp; Vector3 difference = mousePos - gunArm.transform.position;&nbsp; &nbsp; difference.z = 0;&nbsp; &nbsp; gunArm.transform.rotation = Quaternion.LookRotation(difference);}
打开App,查看更多内容
随时随地看视频慕课网APP