亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

C#
holdtom 2023-09-16 17:24:18
我不知道為什么,但我的剛體上的 .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);        }    }}我希望箭頭在向前方向上添加力,但它只是打印出“單擊”(我添加的消息是為了確保鼠標單擊正常工作)。
查看完整描述

3 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超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()

    {


    }

}

并且測試本身無需旋轉,并且在主腳本上僅旋轉有效,因此我嘗試同時激活兩個腳本并且它開始工作,感謝有關此問題的所有幫助。


查看完整回答
反對 回復 2023-09-16
?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

盡管事實上這isn't working是一個相當薄弱的描述:

首先,您應該在 中執行此操作FixedUpdate,但在 中獲取輸入Update。

其次減少Find調用Update..效率非常低。如果可能的話,最好通過檢查器引用它們。否則,也許Start......我在這里展示的方式是延遲初始化的最后手段,假設您的腳本可能稍后在運行時生成

,您可能想要傳遞ForceMode.ImpulseAddForce,因為您只添加一次力,而不是連續添加力。

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;

? ? ? ? }

? ? }

}


查看完整回答
反對 回復 2023-09-16
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

你的代碼不做任何事情的原因不是因為它不起作用,而是因為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);

}


查看完整回答
反對 回復 2023-09-16
  • 3 回答
  • 0 關注
  • 225 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號