• 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 Linezokode · May 26, 2022 at 03:36 AM · unity 2dspritesanglesvectors

How can I get the distance from the center of an object to the mouse position?

So basically, I have a sprite and I need to set the size.x of it to the distance to the mouse. all fine and dandy until I move the mouse above the object, and now it's getting shorter... Any help is much appreciated!

like so: https://youtu.be/6GRJ8iU1gWk

and t$$anonymous$$s is my PlayerController.cs: (I kinda know the problem is that im setting the sprite size to the distanceFromMouse.x, but I have no idea how I could fix it)

 public class PlayerController : MonoBehaviour
 {
     [Header("Aiming")]
     [SerializeField] private GameObject rotatePoint;
     [SerializeField] private Transform aimIndicator;
 
     [Header("Shooting")] 
     [SerializeField] private float strengthThreshold;
     [SerializeField] private float shootStrength;
 
     [Header("Movement")]
     [SerializeField] private float _moveSpeed;
     
     private Vector3 _mousePosition;
     private Vector3 _distanceFromMouse;
     
     private float _horizontalInput;
 
     private SpriteRenderer _aimSprite;
     private Camera _mainCam;
     private Rigidbody2D _playerRb;
 
     bool isMoving;
 
     private void Start()
     {
         _mainCam = Camera.main;
         _aimSprite = aimIndicator.GetComponent<SpriteRenderer>();
         _playerRb = GetComponent<Rigidbody2D>();
     }
     
     
     
     private void Update()
     {
         GetMousePosition();
         AimTowardsMousePosition();
         DrawLineTowardsMouse();
         GetUserInput();
         CheckPlayerMovement();
 
         aimIndicator.gameObject.SetActive(!isMoving);
     }
 
 
     private void FixedUpdate()
     {
         MovePlayer();
     }
 
 
     void GetUserInput()
     {
         _horizontalInput = Input.GetAxis("Horizontal");
     }
 
 
     bool CheckPlayerMovement()
     {
         isMoving = _horizontalInput != 0;
 
         return isMoving;
     }
 
 
     void MovePlayer()
     {
         _playerRb.velocity = new Vector2(_horizontalInput * _moveSpeed,_playerRb.velocity.y);
     }
     
     
     
     void GetMousePosition()
     {
         _mousePosition = _mainCam.ScreenToWorldPoint(Input.mousePosition);
         // Debug.Log(_mousePosition);
     }
 
     void AimTowardsMousePosition()
     {
         // Get the distance between _rotatePoint and the _mousePosition 
         _distanceFromMouse = _mousePosition - rotatePoint.transform.position;
         Vector2 normalisedDistance = _distanceFromMouse.normalized;
         
         // Get angle from mouse location and rotate point location
         float zAngle = Mathf.Atan2(normalisedDistance.y, normalisedDistance.x) * Mathf.Rad2Deg;
         rotatePoint.transform.rotation = Quaternion.Euler(0,0,zAngle); // Rotate _rotatePoint.
     }
 
     void DrawLineTowardsMouse()
     {
         _aimSprite.size = new Vector2(Mathf.Abs(_distanceFromMouse.x), 1);
         
         if (_aimSprite.size.x < 1)
         {
             _aimSprite.size = new Vector2(1, 1);
         }else if (_aimSprite.size.x > strengthThreshold)
         {
             _aimSprite.size = new Vector2(strengthThreshold, 1);
         }
     }
 }
Comment
Add comment · Show 1
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 Caeser_21 · May 26, 2022 at 06:53 AM 0
Share

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

156 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I want to calculate angles between a series of points and find the total angle between start and finish. 2 Answers

2D top-down multiple sprite rotation 1 Answer

How to add material/texture to basic Sprite? 1 Answer

Vector2.MoveTowards problem 0 Answers

How to make sprite animation when the sprites are of different sizes? 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