Blaze
Blaze

Reputation: 1

Trying to create enter the gungeon type weapons however stuck on making weapons rotate to face mouse

Im making a game similar to Enter The Gungeon and am working in a similar space to them in unity. I'm using unity 3D and making a 2.5D game to give it the same perspective as gungeon. However I'm trying to make it so the weapon will face the mouse and its not working. Ive tried multiple tutorials and none of them have worked.

The issue is that it changes the rotation of all other axes to 0 when the game is in a top down perspective so I rotated the X of the quad to 90. From my experimenting I need the Y axis to follow the mouse. Unless there is a better way to go about this how can I do this with my current setup?

here's the code ive been using that somewhat worked but it resets all axes but Z.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class MouseFollow : MonoBehaviour
     {
         
     
     
     
         private void Start()
         {
             
         }
     
         private void Update()
         {
             
             var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
             var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         }
     
        
     
        
     }

Upvotes: 0

Views: 515

Answers (2)

Hunch
Hunch

Reputation: 25

I created this function once while trying to accomplish something similar to this.

The function takes in 2 Vector3 points and returns the Quaternion required for the object to 'Look At' the other point.

p1 is the origin point. p2 is the point you want to look at.

For your solution you could use the transform of the gun object as the origin point, and use your mouse position as the point you want to look at.


        public static Quaternion LookAt(Vector3 p1, Vector3 p2)
        {

            float angle = 0;
            float opp;
            float adj;

            if (p1.x == p2.x)
            {
                //Looking down
                if (p1.y > p2.y)
                {
                    angle = 0;
                }
                //Looking up
                else
                {
                    angle = 180;
                }
            }

            else if (p1.y == p2.y)
            {
                //Looking right
                if (p1.x < p2.x)
                {
                    angle = 90;
                }
                //Looking left
                else
                {
                    angle = 270;
                }
            }
            else
            {
                if (p1.x < p2.x)
                {
                    //First quadrant angle +0
                    if (p1.y > p2.y)
                    {
                        angle = 0;
                        opp = Mathf.Abs(p1.x - p2.x);
                        adj = Mathf.Abs(p1.y - p2.y);
                    }
                    //Second quadrant angle +90
                    else
                    {
                        angle = 90;
                        adj = Mathf.Abs(p1.x - p2.x);
                        opp = Mathf.Abs(p1.y - p2.y);
                    }
                }
                //if (p1.x > p2.x)
                else
                {
                    //Third quadrant angle +180
                    if (p1.y <= p2.y)
                    {
                        angle = 180;
                        opp = Mathf.Abs(p1.x - p2.x);
                        adj = Mathf.Abs(p1.y - p2.y);
                    }
                    //Forth quadrant angle +270
                    else
                    {
                        angle = 270;
                        adj = Mathf.Abs(p1.x - p2.x);
                        opp = Mathf.Abs(p1.y - p2.y);
                    }
                }

                float a = Mathf.Atan(opp / adj) * 180f / Mathf.PI;
                angle += a;
            }

            Quaternion rotation = Quaternion.Euler(0, 0, angle);
            return rotation;
        }

Upvotes: 0

Toll Gnoll
Toll Gnoll

Reputation: 198

The simplest solution I can think of is to create an empty object and make it the parent of the gun model. This will ensure that the gun (child GameObject) can keep its own rotation without resetting it while the parent GameObject can be rotated on the y axis using the method you described above.

If this is not an option, you could create another Quaternion for all of the rotations that you want to keep, which are independent of the mouse direction. Then, for each frame, multiply the direction you generate by it to receive the rotation you want.

transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward) * thePersistingQuaternion

Upvotes: 0

Related Questions