我不知道为什么,但是我有一个body2d,但我的加力没有作用,代码看起来正确,但仍然无法正常工作?

时间:2019-08-22 16:35:34

标签: c# unity3d

我不确定为什么,但是我在刚体上的.addforce无法正常工作。

我已经尝试过官方统一的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);
        }
    }
}

我希望箭头在向前的方向上被加力,但是它只是打印出“喀哒”声(我添加的消息是为了确保鼠标单击有效)。

3 个答案:

答案 0 :(得分:0)

尽管isn't working的描述很不充分:

首先,您应该在FixedUpdate中进行操作,但要在Update中进行输入。

第二个减少Find中的Update调用..效率非常低。如果可能的话,最好通过检查器来引用它们。否则,也许在Start中..我在这里显示的方法是延迟初始化的最后一招,假设您的脚本可能稍后在运行时生成。

另外(由于EricOverflow),您可能希望将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;
        }
    }
}

答案 1 :(得分:0)

您的代码不执行任何操作的原因不是因为它不起作用,而是因为transform.forward是一个幅度为1的向量。添加幅度为1的力不会对大多数对象产生太大的作用,并且不会产生摩擦可能会再次降低物体的速度。

尝试添加具有更高强度和ForceMode.Impulse的力:

float strength = 50f;
rb.AddForce(transform.forward * strength, ForceMode.Impulse);

更新:

您似乎想让枪对准您的鼠标位置,这可能是您的问题所在: 让我们尝试使用Quaternion.LookRotation代替手动进行数学运算。

也许:

GameObject gunArm;

void Awake()
{
    gunArm = GameObject.Find("gunArm");
}

void faceMouse()
{
    Vector3 difference = mousePos - gunArm.transform.position;
    difference.z = 0;
    gunArm.transform.rotation = Quaternion.LookRotation(difference);
}

答案 2 :(得分:0)

我不确定为什么,但是我创建了一个测试脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    public Rigidbody2D rb;
    public float speed = 20.0f;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            print("click");

            rb.AddForce(transform.right * speed, ForceMode2D.Impulse);
        }

        rotate();
    }

    private void rotate()
    {

    }
}

我也将旧脚本编辑为此:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArrowController : MonoBehaviour
{
    public Rigidbody2D rb;
    public float speed = 50.0f;
    public Vector2 pos;

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

    private void Update()
    {
        faceMouse();
        testForClick();
    }

    void FixedUpdate()
    {
        if (doForce == true)
        {
            doForce = false;
            rb.AddForce(transform.forward * speed, ForceMode2D.Impulse);
        }
    }

    private bool doForce;

    private GameObject gunArm;
    private Camera cam;

    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 = rb.transform.position - mousePos;
        var gunAngle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        rb.transform.rotation = Quaternion.Euler(0, 0, gunAngle);
    }

    void testForClick()
    {
        if (Input.GetMouseButtonDown(0))
        {
            print("click");

            // only set the flag
            doForce = true;
        }
    }

    void place()
    {

    }
}

并且测试本身没有轮换工作,并且仅在主脚本上轮换工作,因此我尝试同时激活两个脚本并开始工作,感谢在此问题上的所有帮助。