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 关注
- 141 浏览
添加回答
举报