Question by
leeminsik999 · May 04, 2019 at 05:30 AM ·
vector3clamp
clamp crosshair(vector3) to player
Hi I am working on 2D game, and trying to make an attack system like the one from "Katana Zero".
public float distanceFromCamera = 10.0f; void update(){ var pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceFromCamera); pos = Camera.main.ScreenToWorldPoint(pos); transform.position = pos; }
I am using this script to have crosshair follow mouse. This works fine but the problem is you can kill any enemy on map no matter how far it is from the player.
So I want to limit the movement of crosshair near the player, but I have no idea how.
I have tried pos.Normalize();
, and
pos.x = Mathf.Clamp(pos.x, min, max); pos.y = Mathf.Clamp(pos.y, min, max);
but none of them worked. Please help, and thank you very much.
Comment