Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Jorge · Jan 23, 2011 at 02:04 AM · 2dmouseshooterpoint

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

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Jorge · Jan 24, 2011 at 12:38 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image creed24 · Sep 06, 2012 at 02:44 AM 0
Share

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.

avatar image
0

Answer by Dasherz · Sep 06, 2012 at 03:10 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image creed24 · Sep 06, 2012 at 12:58 PM 0
Share

Yeah, I just had to change " Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);

" to " Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.forward);

"

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Rotate a gameobject(player)on the y axis towards mouse position in c# 2 Answers

2D platformer cursor 1 Answer

Shooting in direction of mouse cursor 2d 5 Answers

I need help with clamping a rotation towards the mouse position 2 Answers

Cannonball not shooting according to the rotation of the cannon. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges