0
\$\begingroup\$

This works well on clicking layers but not click and dragging layers.

public class FishClicker : MonoBehaviour {

public LayerMask whatIsFish;

public float clickRadius = 0.1f;

void Update() {

    if (Input.GetMouseButtonDown(0)) {
        var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        var hit = Physics2D.OverlapCircle(mousePos, clickRadius, whatIsFish);

        if (hit != null) {
            var fish = hit.GetComponentInParent<Fish>();

            if (fish != null) { fish.DoClickThing(); }
        }
    }
} 

}

for DoClickThing(); this works

public void ControlFishClick()
{
    screenPos = myCam.WorldToScreenPoint(transform.position);
    Vector3 v3 = Input.mousePosition - screenPos;
    angleOffset = (Mathf.Atan2(transform.right.y, transform.right.x) - Mathf.Atan2(v3.y, v3.x)) * Mathf.Rad2Deg;

}

for DoDragThing() i want this to work

public void ControlFishDrag()
{
    stopMovement = true;
    rigid2D.velocity = new Vector2(0, 0);
    Vector3 v3 = Input.mousePosition - screenPos;
    float angle = Mathf.Atan2(v3.y, v3.x) * Mathf.Rad2Deg;
    transform.eulerAngles = new Vector3(0, 0, angle + angleOffset);
    //print(stopMovement);
}

for DoReleaseClick() i want this

public void ControlFishUp()
{
    stopMovement = false;
    SnapToPosition();
}
\$\endgroup\$
0
\$\begingroup\$
public class FishClicker : MonoBehaviour {

    public LayerMask whatIsFish;
    
    public float clickRadius = 0.1f;

    Fish _clickedFish;
    Vector3 _clickOffset;
    
    void Update() {
        var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        // Drag an already-clicked fish.
        if (_clickedFish != null)
            _clickedFish.transform.position = mousePos + _clickOffset;

        // Click a new fish.
        if (Input.GetMouseButtonDown(0)) {
            
            var hit = Physics2D.OverlapCircle(mousePos, clickRadius, whatIsFish);
    
            if (hit != null) {
                _clickedFish = hit.GetComponentInParent<Fish>();
    
                if (_clickedFish != null) { 
                    _clickedFish.DoClickThing();
                    _clickOffset = _clickedFish.transform.position - mousePos;
                }
            }
        }

        // Release a dragged fish.
        if (Input.GetMouseButtonUp(0))
            _clickedFish = null;
    }

}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Your code works but I think I asked it wrong. \$\endgroup\$ – MM1Content Nov 14 at 2:39
  • \$\begingroup\$ Then would you like to edit your question to clarify the problem you need help with? \$\endgroup\$ – DMGregory Nov 14 at 2:43
  • \$\begingroup\$ Yes i hope this clarifies the circular motion that was working for me in its original script. The mouse also collide with other fish too causing it to move as well on contact \$\endgroup\$ – MM1Content Nov 14 at 2:52
  • \$\begingroup\$ It works =) i put the first original code into Input.GetMouseButton(0). Do i need to ask a separate question for the code not to detect other fish while click and dragging one fish? \$\endgroup\$ – MM1Content Nov 14 at 3:02
  • \$\begingroup\$ GetMouseButton(0) is what causes that. Don't do that. It says "Keep looking for a new fish to grab as long as the button is held" instead of what you want "Grab a new fish only when the mouse button changes from up to down" \$\endgroup\$ – DMGregory Nov 14 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.