Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Linezokode · May 26 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 this 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 at 06:53 AM 0
Share

Just for testing, instead of using a sprite's scale try using a Line Renderer... Then see what happens


Also instead of calculating the distance manually I think you can use Vector3.Distance

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.

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

Unity 2D problem with displaying sprites in final build game 1 Answer

Unity 2D Sprites cost many draw calls 1 Answer

Changing how my character looks depending on his current lifes. 2 Answers

UI vs Sprites 0 Answers

Can't Get .enabled to Work for Children Sprites 2 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