Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How can i move game object to mouse position while in players range

Discussion in 'Scripting' started by Rinqq, Jan 24, 2023.

  1. Rinqq

    Rinqq

    Joined:
    Oct 9, 2020
    Posts:
    2
    I am trying to make weapon which is an orb. I tried almost everything but couldn't make weapon following mouse while in range of player. Can anyone help me?

    Here is player hierarchy image:


    Here is my code. The problem in this code is maxDistance is always between 0-6. Max distance doesn't fit with player movement. If you have better solution to this it would be awesome to hear. I deleted player movement lines because they are not neccessary.

    Code (CSharp):
    1.     public Transform weaponParent;
    2.     public float maxMoveSpeed = 10;
    3.     public float smoothTime = 0.3f;
    4.     Vector2 currentVelocity;
    5.     [SerializeField] private float maxDistance = 6f;
    6.  
    7.     void FixedUpdate()
    8.     {
    9.         Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    10.  
    11.         var target = Vector2.ClampMagnitude(mousePosition, maxDistance);
    12.  
    13.         weaponParent.position = Vector2.SmoothDamp(weaponParent.position, target, ref currentVelocity, smoothTime, maxMoveSpeed);
    14.  
    15.     }
     
  2. JustinNaicker

    JustinNaicker

    Joined:
    Jan 4, 2023
    Posts:
    47
    Is maxDistance consecutively updating with the players X Position? You can add the the Players X Position to the Max Distance your gameObject should be able to travel.
     
  3. Rinqq

    Rinqq

    Joined:
    Oct 9, 2020
    Posts:
    2
    Weapon parent shouldn't be away more than maxDistance from player so if i increase maxDistance it wouldn't do what I I am trying to do. So I worked on it few hours and this worked. There is porbably better way to get summary of Vectors but i dont mind since this worked.

    Code (CSharp):
    1.        
    2.         Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    3.         Vector3 mousePosition_ = new Vector3(mousePosition.x, mousePosition.y, transform.position.z);
    4.  
    5.         var target = new Vector2(transform.position.x + Vector2.ClampMagnitude(mousePosition_ - transform.position, maxDistance).x, transform.position.y + Vector2.ClampMagnitude(mousePosition_ - transform.position, maxDistance).y);
    6.  
    7.         weaponParent.position = Vector2.SmoothDamp(weaponParent.position, target, ref currentVelocity, smoothTime, maxMoveSpeed);
     
  4. JustinNaicker

    JustinNaicker

    Joined:
    Jan 4, 2023
    Posts:
    47

    I wouldn’t say it would be increasing, but it would be scaling with the players position. The maxDistance isn’t changing, but your clamping distance should be equal to the players current X position plus the maxDistance. I might have misinterpreted your question.
     
    Rinqq likes this.