How do i rotate an object towards mouse direction?
So I'm making a game with stickmen. I added guns and i made a script so that when i press G i pick up stuff. However the arm that the picked up gun is attached to follows the mouse position. What i want the gun to do is when the arm goes right it rotates right as well so that i can shoot. The problem i have is it flips 180 and basically faces down which looks strange. If u could help it would be appreciated ! :)
This is what i found which didn't help my case.(the gun rotates on it's centre which doesn't help me)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RotateTo$$anonymous$$ouse : $$anonymous$$onoBehaviour
{
public float offset = 0.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotation_z = $$anonymous$$athf.Atan2(difference.y, difference.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotation_z + offset);
}
}
Your answer
Follow this Question
Related Questions
Rotating 3d person character 0 Answers
Rotate a vector based on a direction (transform.up) of a game object 1 Answer
Grabbing the Relative eulerAngles.y of a Rotation 1 Answer
Player Positioning on a Rotating Circle 0 Answers
Rotate object in 3d space along one axis in the direction of movement vector. 0 Answers