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: Player Prefab
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.
public Transform weaponParent;
public float maxMoveSpeed = 10;
public float smoothTime = 0.3f;
Vector2 currentVelocity;
[SerializeField] private float maxDistance = 6f;
void FixedUpdate()
{
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var target = Vector2.ClampMagnitude(mousePosition, maxDistance);
weaponParent.position = Vector2.SmoothDamp(weaponParent.position, target, ref currentVelocity, smoothTime, maxMoveSpeed);
}
CodePudding user response:
I think I get your issue now
you are clamping a position (which is a vector starting at world (0,0,0), you rather want to clamp the vector between them like e.g.
var target = transform.position Vector2.ClampMagnitude(mousePosition - transform.position, maxDistance);