C# 向鼠标位置旋转一个对象并发射一些子弹
问题 我的场景中有一个游戏对象,我希望该游戏对象旋转,使其在我单击时面向我的鼠标。在旋转之后,它应该朝着它现在所面对的方向移动。然而,它几乎可以工作 示例C# 向鼠标位置旋转一个对象并发射一些子弹,c#,unity3d,rotation,mouse,C#,Unity3d,Rotation,Mouse,问题 我的场景中有一个游戏对象,我希望该游戏对象旋转,使其在我单击时面向我的鼠标。在旋转之后,它应该朝着它现在所面对的方向移动。然而,它几乎可以工作 示例 float mouseX, mouseY; float playerX, playerY; float squaredDeltaX, squaredDeltaY; float hypotenuse; int dirX, dirY; float rotation; bool mouseClicked = false; void Upd
float mouseX, mouseY;
float playerX, playerY;
float squaredDeltaX, squaredDeltaY;
float hypotenuse;
int dirX, dirY;
float rotation;
bool mouseClicked = false;
void Update () {
UpdateInputs();
if (mouseClicked)
{
CalcDistance(playerX,playerY,mouseX,mouseY);
SetRotation(squaredDeltaX, squaredDeltaY, hypotenuse);
}
}
void UpdateInputs()
{
Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
mouseX = pos.x;
mouseY = pos.y;
playerX = transform.position.x;
playerY = transform.position.y;
mouseClicked = Input.GetMouseButtonDown(0);
}
void CalcDistance(float x, float y, float x1, float y1)
{
dirX = (x - x1) >= 0 ? 1 : -1;
dirY = (y - y1) >= 0 ? 1 : -1;
squaredDeltaX = (x - x1) * (x - x1);
squaredDeltaY = (y - y1) * (y - y1);
hypotenuse = Mathf.Sqrt(squaredDeltaX + squaredDeltaY);
print("squaredDelta: " + squaredDeltaX + ", " + squaredDeltaY);
print("Hypotenuse: " + hypotenuse);
print("delta: " + Mathf.Sqrt(squaredDeltaX)*dirX + ", " + Mathf.Sqrt(squaredDeltaY)*dirY);
}
void SetRotation(float opposite, float adjacent, float hypotenuse)
{
float tempOpposite = Mathf.Sqrt(opposite);
float tempAdjacent = Mathf.Sqrt(adjacent);
rotation = Mathf.Acos(Mathf.Cos(tempOpposite / hypotenuse));
print("MathF.Cos: " + rotation);
rotation *= Mathf.Rad2Deg * dirX;
print("MathF.Acos: " + rotation);
rotation = dirY != -1 ? rotation + 180 : rotation;
print("angle: " + rotation + "*");
transform.eulerAngles = new Vector3(1,1,rotation);
}
例如,假设斜边是:4.592912,相邻的是:3.042814,那么我取MathF.Cos(相邻/斜边)=0.6625021,现在取MathF.Acos(0.6625021)*MathF.Rad2Deg=37.95857
来自场景的GIF:
代码
float mouseX, mouseY;
float playerX, playerY;
float squaredDeltaX, squaredDeltaY;
float hypotenuse;
int dirX, dirY;
float rotation;
bool mouseClicked = false;
void Update () {
UpdateInputs();
if (mouseClicked)
{
CalcDistance(playerX,playerY,mouseX,mouseY);
SetRotation(squaredDeltaX, squaredDeltaY, hypotenuse);
}
}
void UpdateInputs()
{
Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
mouseX = pos.x;
mouseY = pos.y;
playerX = transform.position.x;
playerY = transform.position.y;
mouseClicked = Input.GetMouseButtonDown(0);
}
void CalcDistance(float x, float y, float x1, float y1)
{
dirX = (x - x1) >= 0 ? 1 : -1;
dirY = (y - y1) >= 0 ? 1 : -1;
squaredDeltaX = (x - x1) * (x - x1);
squaredDeltaY = (y - y1) * (y - y1);
hypotenuse = Mathf.Sqrt(squaredDeltaX + squaredDeltaY);
print("squaredDelta: " + squaredDeltaX + ", " + squaredDeltaY);
print("Hypotenuse: " + hypotenuse);
print("delta: " + Mathf.Sqrt(squaredDeltaX)*dirX + ", " + Mathf.Sqrt(squaredDeltaY)*dirY);
}
void SetRotation(float opposite, float adjacent, float hypotenuse)
{
float tempOpposite = Mathf.Sqrt(opposite);
float tempAdjacent = Mathf.Sqrt(adjacent);
rotation = Mathf.Acos(Mathf.Cos(tempOpposite / hypotenuse));
print("MathF.Cos: " + rotation);
rotation *= Mathf.Rad2Deg * dirX;
print("MathF.Acos: " + rotation);
rotation = dirY != -1 ? rotation + 180 : rotation;
print("angle: " + rotation + "*");
transform.eulerAngles = new Vector3(1,1,rotation);
}
这是一个使用
Mathf.Atan
、Rigidibody2D
和ConstantForce2D
的健壮方法:
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
private Camera _camera;
private GameObject _find;
private void OnEnable()
{
_find = GameObject.Find("New Sprite");
_camera = Camera.main;
}
private void Update()
{
// find the vector between cannon and mouse position
var p1 = _camera.ScreenToViewportPoint(Input.mousePosition);
var p2 = _camera.WorldToViewportPoint(_find.transform.position);
var p3 = p2 - p1;
// rotate cannon to mouse position
var angle = Mathf.Atan2(p3.y, p3.x) * Mathf.Rad2Deg;
_find.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
// throw a projectile on mouse down
if (Input.GetMouseButtonDown(0))
{
var clone = Instantiate(_find);
var rb = clone.AddComponent<Rigidbody2D>();
rb.gravityScale = 0;
var force = clone.AddComponent<ConstantForce2D>();
force.relativeForce = Vector2.left * 5.0f;
}
}
}
使用UnityEngine;
公共类NewBehaviourScript:MonoBehavior
{
私人摄像机;
私人游戏对象(find);;
私有void OnEnable()
{
_find=GameObject.find(“新精灵”);
_camera=camera.main;
}
私有void更新()
{
//找到加农炮和鼠标位置之间的矢量
var p1=_camera.ScreenToViewportPoint(Input.mousePosition);
var p2=_camera.WorldToViewportPoint(_find.transform.position);
变量p3=p2-p1;
//将大炮旋转到鼠标位置
变量角度=数学Atan2(p3.y,p3.x)*数学Rad2Deg;
_find.transform.rotation=四元数.AngleAxis(角度,矢量3.向前);
//把弹丸扔到老鼠身上
if(Input.GetMouseButtonDown(0))
{
var clone=实例化(_find);
var rb=clone.AddComponent();
rb.重力标度=0;
var force=clone.AddComponent();
force.relativeForce=矢量2.left*5.0f;
}
}
}
这是场景设置:
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
private Camera _camera;
private GameObject _find;
private void OnEnable()
{
_find = GameObject.Find("New Sprite");
_camera = Camera.main;
}
private void Update()
{
// find the vector between cannon and mouse position
var p1 = _camera.ScreenToViewportPoint(Input.mousePosition);
var p2 = _camera.WorldToViewportPoint(_find.transform.position);
var p3 = p2 - p1;
// rotate cannon to mouse position
var angle = Mathf.Atan2(p3.y, p3.x) * Mathf.Rad2Deg;
_find.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
// throw a projectile on mouse down
if (Input.GetMouseButtonDown(0))
{
var clone = Instantiate(_find);
var rb = clone.AddComponent<Rigidbody2D>();
rb.gravityScale = 0;
var force = clone.AddComponent<ConstantForce2D>();
force.relativeForce = Vector2.left * 5.0f;
}
}
}
(父对象的比例为10)
结果:
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
private Camera _camera;
private GameObject _find;
private void OnEnable()
{
_find = GameObject.Find("New Sprite");
_camera = Camera.main;
}
private void Update()
{
// find the vector between cannon and mouse position
var p1 = _camera.ScreenToViewportPoint(Input.mousePosition);
var p2 = _camera.WorldToViewportPoint(_find.transform.position);
var p3 = p2 - p1;
// rotate cannon to mouse position
var angle = Mathf.Atan2(p3.y, p3.x) * Mathf.Rad2Deg;
_find.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
// throw a projectile on mouse down
if (Input.GetMouseButtonDown(0))
{
var clone = Instantiate(_find);
var rb = clone.AddComponent<Rigidbody2D>();
rb.gravityScale = 0;
var force = clone.AddComponent<ConstantForce2D>();
force.relativeForce = Vector2.left * 5.0f;
}
}
}
注意事项:
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
private Camera _camera;
private GameObject _find;
private void OnEnable()
{
_find = GameObject.Find("New Sprite");
_camera = Camera.main;
}
private void Update()
{
// find the vector between cannon and mouse position
var p1 = _camera.ScreenToViewportPoint(Input.mousePosition);
var p2 = _camera.WorldToViewportPoint(_find.transform.position);
var p3 = p2 - p1;
// rotate cannon to mouse position
var angle = Mathf.Atan2(p3.y, p3.x) * Mathf.Rad2Deg;
_find.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
// throw a projectile on mouse down
if (Input.GetMouseButtonDown(0))
{
var clone = Instantiate(_find);
var rb = clone.AddComponent<Rigidbody2D>();
rb.gravityScale = 0;
var force = clone.AddComponent<ConstantForce2D>();
force.relativeForce = Vector2.left * 5.0f;
}
}
}
你可能会偏离+/-90度,在我的例子中,我没有打扰你,我只是将红色尖端移动到适当的位置,我将把它留给你作为练习
您可以做的另一件事是销毁不再可见的对象,使用我在
相机中使用的方法应该很容易
非常感谢,我将尝试一下!)欢迎:-)顺便说一句,用来问游戏相关的问题,这是一个更好的地方!