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
0
Question by LuxSemper · Mar 17, 2014 at 02:38 AM · 2dangleshootercirclecrosshair

2D Aiming with a mouse angle problem

Hi, I've been trying to find a solution for this particular problem for a while now, but with no result. I'm a bit new to Unity and scripting in general, so I decided to ask here if someone might have an idea how to solve the issue.

Basically, what I'm trying to do is have "object B" (crosshair for instance) circle around "object A" (player) using the mouse. I've made that work, but here's the problem:

Since it follows the mouse position and rotates towards it, the angle to mouse-movement changes depending on the distance of the mouse to the player character, who is the pivot point (the closer the mouse is, each mouse movement makes the angle more erratic, and vice versa). But since I intend to make the mouse pointer invisible in-game, there is no way to know where the pointer is, thus the angle is all over the place.

question/tldr:

Is there a way to bind mouse movement to a circle around a specific object or have consistent mouse movement to crosshair angle ratio for a 2d shooter?

Script I'm using at the moment, if someone is interested.

using UnityEngine;

using System.Collections; public class AimingFinal : MonoBehaviour {
public Transform target; public float fRadius = 3.0f;
void Update () { Vector3 v3Pos = Camera.main.WorldToScreenPoint (target.position); v3Pos = Input.mousePosition - v3Pos; float angle = Mathf.Atan2 (v3Pos.y, v3Pos.x) Mathf.Rad2Deg; v3Pos = Quaternion.AngleAxis (angle, Vector3.forward) (Vector3.right * fRadius); transform.position = target.position + v3Pos; } }
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

1 Reply

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

Answer by robertbu · Mar 17, 2014 at 04:40 AM

Try this.

 using UnityEngine;
 using System.Collections;

 public class RotaterUnitCircle : MonoBehaviour 
 {
     public float sensitivity = 1.0f;
 
     private Vector3 trackingPos;
 
     void Start() {
         trackingPos = Vector3.right;
     }
 
     void Update ()
     {
         trackingPos.x += Input.GetAxis ("Mouse X") * sensitivity;
         trackingPos.y += Input.GetAxis ("Mouse Y") * sensitivity;
         trackingPos = trackingPos.normalized;
 
         float angle = Mathf.Atan2 (trackingPos.y, trackingPos.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
     }
 } 

This code assumes that the front of your sprite is on the right.

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 LuxSemper · Mar 17, 2014 at 11:38 AM 0
Share

It works perfectly! Thank you so much, you're awesome! :D

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D platformer cursor 1 Answer

Sidescroll Shooter Crosshair? 0 Answers

2D Animation does not start 1 Answer

Making a Turret Have a Circle Sector Range 1 Answer

2D Top down shooter, gun is shaking while moving and Cursor solutions 0 Answers


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