3 回答

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
我不知道為什么,但我創(chuàng)建了一個(gè)測(cè)試腳本:
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()
{
}
}
并且測(cè)試本身無(wú)需旋轉(zhuǎn),并且在主腳本上僅旋轉(zhuǎn)有效,因此我嘗試同時(shí)激活兩個(gè)腳本并且它開(kāi)始工作,感謝有關(guān)此問(wèn)題的所有幫助。

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊
盡管事實(shí)上這isn't working
是一個(gè)相當(dāng)薄弱的描述:
首先,您應(yīng)該在 中執(zhí)行此操作FixedUpdate
,但在 中獲取輸入Update
。
其次減少Find
調(diào)用Update
..效率非常低。如果可能的話,最好通過(guò)檢查器引用它們。否則,也許Start
......我在這里展示的方式是延遲初始化的最后手段,假設(shè)您的腳本可能稍后在運(yùn)行時(shí)生成
,您可能想要傳遞ForceMode.Impulse
給AddForce
,因?yàn)槟惶砑右淮瘟?,而不是連續(xù)添加力。
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貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
你的代碼不做任何事情的原因不是因?yàn)樗黄鹱饔茫且驗(yàn)閠ransform.forward是一個(gè)大小為1的向量。添加大小為1的力對(duì)大多數(shù)物體不會(huì)有太大作用,并且摩擦力可能會(huì)減慢再次放下物體。
嘗試添加更高強(qiáng)度的力和 ForceMode.Impulse:
float strength = 50f;
rb.AddForce(transform.forward * strength, ForceMode.Impulse);
更新:
看起來(lái)您希望槍面向您的鼠標(biāo)位置,這可能就是您的問(wèn)題所在:讓我們嘗試使用 Quaternion.LookRotation 來(lái)使其工作,而不是手動(dòng)進(jìn)行數(shù)學(xué)計(jì)算。
或許:
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 關(guān)注
- 252 瀏覽
添加回答
舉報(bào)