mousepos does not move when I move the mouse

I have tried everything but what works please help
what I want it to do is add 1 to the currentcloud int when i click the gun see image it reads equal to the main cam x and y

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rightgun : MonoBehaviour {
    // sprite for Rightgun
    public Sprite Rgun;
    // render for Right gun
    private Collider2D Rgcollider; // 2d collder for the Right gun

    private SpriteRenderer Rgunrender;

    private Color Rgold = new Color(207F, 181F, 59F, 0.5F);


    // Use this for initialization
    void Start () {
        Rgunrender = gameObject.GetComponent<SpriteRenderer>();
        Rgcollider = gameObject.GetComponent<CapsuleCollider2D>(); // sets your 2d collder equal to an compent? maybe



    }

    // Update is called once per frame
    void Update () {
        //Makes a gameobject equal to the thoughtbubble object
        GameObject DialogRgun = GameObject.Find("Thoughtbubbles");
        Rgunrender.sprite = Rgun;
        Actionbubbles ClassRgun = DialogRgun.GetComponent<Actionbubbles>();

        // makes a class to access for my data check on current cloud ex dialogRgun.current cloud
 
        if (ClassRgun.Currentcloud <= ClassRgun.Numberofclouds) {
         
            GetComponent<SpriteRenderer> ().color = Rgold;
            if (isRgtouched ()) {
                // do stuff
                ClassRgun.Currentcloud = ClassRgun.Currentcloud + 1;

            }




        } else {

            GetComponent<SpriteRenderer>().color = Color.red;
        }


    }
    public bool isRgtouched() {
        bool result = false;
        Vector3 wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 mousePos = new Vector2(wp.x, wp.y);
        wp.z = 20.192019463f;
        Debug.Log (mousePos);

        if (Input.GetMouseButtonUp(0))
        {
         


            if (Rgcollider == Physics2D.OverlapPoint(mousePos)) {

                result = true;

            }

        }
        return result;


    }
}

3231123--247980--gunloos.png

I read something about Z distance but I’m not sure if it relates to clicking on things with your Mouse

Yes, the problem is probably your call to ScreenToWorldPoint. Keep in mind that there isn’t a single point in the world that corresponds to one point on screen. A line from the camera, through some point on the screen, and off to infinity will pass through an infinite number of world points.

So, you need to tell ScreenToWorldPoint which of those you want, using the Z value of the point you pass in. You didn’t specify one, so that’s the same as Z=0. If that’s not the same as your gun, then you won’t be getting a point that is “over” the gun in X and Y (which is all that Physics2D.OverlapPoint cares about).

So, long story short, try changing lines 55-56 to something like

Vector3 wp = Input.mousePosition;
wp.z = Rgun.transform.position.z;
Vector3 mousePos = Camera.main.ScreenToWorldPoint(wp);
2 Likes

Thank you very much it worked
of course I had to use this near the end
" wp.z = gameObject.transform.position.z;"
But bottom line I was lost without you

1 Like