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()
{
}
}
并且測試本身無需旋轉,并且在主腳本上僅旋轉有效,因此我嘗試同時激活兩個腳本并且它開始工作,感謝有關此問題的所有幫助。

TA貢獻1834條經驗 獲得超8個贊
盡管事實上這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;
? ? ? ? }
? ? }
}

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);
}
- 3 回答
- 0 關注
- 225 瀏覽
添加回答
舉報