Reputation: 37
Problem
I have a GameObject in my scene and I want that GameObject to rotate so it faces my mouse when I click it. After the rotation it's supposed to move in the direction it is now facing. However it almost works.
Example
For instance lets say the hypotenuse is: 4.592912 and the adjacent is: 3.042814, then i take MathF.Cos(adjacent/hypotenuse) = 0.6625021, now i take MathF.Acos(0.6625021) * MathF.Rad2Deg = 37.95857
GIF from the scene:
Code
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);
}
Upvotes: 1
Views: 1351
Reputation: 16652
Here's a robust method using Mathf.Atan
, Rigidibody2D
and 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;
}
}
}
Here's the scene setup:
(scale of the parent object is 10)
Result:
Notes:
You might be off by +/- 90 degrees, in my example I didn't bother, I just moved the red tip to the proper place, I'll leave that to you as an exercise !
Another thing you can do is to destroy objects that are not visible anymore, should be easy with the methods I used in Camera
.
Upvotes: 1