# How to make a 2D character points his gun to the mouse position?

 0 Hi all, I'm trying to make a 2D Run'n'gun game, like Metal Slug. The player controls the character using W, A, S and D, and shot with the mouse. I'm trying to make the character points his gun to the mouse position, just like in the game Facewound, but without success. Is there any simple way to make the character point his gun to the mouse position? I've tried to change the MouseLook.cs, but the arm rotates around the center, and not at the sholder. Thanks for everything. more ▼ asked Jan 23 '11 at 02:04 AM Jorge 71 ● 2 ● 2 ● 5 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 Managed to solve it. A quick tutorial to how to do it: Create a Empty GameObject and a Cube, and stretch the cube to make the arm. Make the Empty GameObject be the parent of the Cube. Make sure that the Empty GameObject is in the end of the cube. It should be on the "sholder" of the "arm". Add the following code to the Empty GameObject. It's a modification of the MouseLook.cs: // using UnityEngine; using System.Collections; public class MouseLook : MonoBehaviour { public float sensitivityY = 15F; public float minimumY = -60F; public float maximumY = 60F; float rotationY = 0F; Quaternion originalRotation; void Update () { rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationY = ClampAngle (rotationY, minimumY, maximumY); Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left); transform.localRotation = originalRotation * yQuaternion; } void Start () { if (rigidbody) rigidbody.freezeRotation = true; originalRotation = transform.localRotation; } public static float ClampAngle (float angle, float min, float max) { if (angle < -360F) angle += 360F; if (angle > 360F) angle -= 360F; return Mathf.Clamp (angle, min, max); } } // It may have some unnecessary lines of code. I didn't filtered what I need, only modified to follow the mouse in 2D. The "arm" - the stretched Cube - will point to the mouse, because the Empty GameObject is always facing the mouse position. Hope that it helps somebody. more ▼ answered Jan 24 '11 at 12:38 AM Jorge 71 ● 2 ● 2 ● 5 I tried this script and it doesn't rotate the gun like expected. It twists the gun along the x axis. It's a good start for me though, I'll try to debug it. Sep 06 '12 at 02:44 AM creed24 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 I have actually used this in my 2d game but I had to completely scrap the X axis part of the code. It worked perfectly then. more ▼ answered Sep 06 '12 at 03:10 AM Dasherz 192 ● 1 ● 2 ● 5 Yeah, I just had to change " Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);" to " Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.forward);" Sep 06 '12 at 12:58 PM creed24 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x1034
x983
x132
x129

asked: Jan 23 '11 at 02:04 AM

Seen: 2658 times

Last Updated: Sep 06 '12 at 12:58 PM