2D Move to Mouse Click Option causing my Sprite to disappear

I have a Character with these components:
SpriteRenderer
Rigidbody2D
Circle Collider
Box Collider
PlayerController script (where all the magic happens)

And for some reason with the code I have below, whenever my sprite makes it to the location it just vanishes from the screen. I think it has something to do with me not accounting for the Camera perspective, and I don’t think it has to do with all the components above since I added a new GameObject with just the SpriteRenderer component and the script, but the same problem persisted.

Using the Debug.Log() the coordinates all work out, the only thing that is off is that the initial location is Vector3(0,0,0) for the sprite, and the z-location goes to -10 so I’m not sure if that is having an affect.

There is absolutely nothing else in the scene, and my sprite just vanishes once the sprite vector = mouse click location. Also the Debug.Log() shows that if I click again with the image gone, the coordinates are still updating.

Sorry for all the comment lines, I thought maybe leaving them in would help give others insight to my train of thought and see where I was going wrong on this. Any and all advice is appreciated! Still new to Unity, if it’s not obvious. I’ve also tried leaving this portion of code

transform.position = Vector3.MoveTowards(transform.position, pos, speed);

in the update call, but the same error happens.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
    public float speed;
    private Vector3 pos;
    //private Vector3 cameraPos;

    // Use this for initialization
    void Start ()
    {
        pos = transform.position;
        //cameraPos = Camera.main.WorldToViewportPoint(transform.pos);
        speed = 0.1f;
        Debug.Log(string.Format("START"));
    }

    // Update is called once per frame
    void Update()
    {
        Movement(); 
    }

    /// <summary>
    /// Check for Mouse input and if received move player towards the mouse coordinates
    /// </summary>
    void Movement()
    {
        if (Input.GetMouseButtonDown(0))
        { 
            //This was part of the tutorial, I'm not sure why it's here.
            // Leaving it here until game is complete to see if reasoning for it being here blossoms
            //pos.z = -5.0f;   
            pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Debug.Log(string.Format("Pushed!"));
        }
        // needs fixing, the movement is not smooth nor updateable with a simple value change for movement
        if (transform.position != pos)
        {
            //Debug.Log(string.Format("NO"));
            transform.position = Vector3.MoveTowards(transform.position, pos, speed);
        }
        else if (transform.position == pos)
        {
            //Debug.Log(string.Format("YES"));
        }
        

        Debug.Log(string.Format(transform.position.ToString() + "Char"));
        Debug.Log(string.Format(pos.ToString() + "Mouse"));

        if (Input.GetKeyDown(KeyCode.A))
        {
            transform.position = new Vector3(-0.2f, -2.1f, 0.0f);
            pos = transform.position;
        }
        //cameraPos.x = Mathf.Clamp01(cameraPos.x);
        //cameraPos.y = Mathf.Clamp01(cameraPos.y);
        //transform.position = Camera.main.ViewportToWorldPoint(cameraPos);
    }
}

Hi there

As you’ve noticed, the z coordinate of your object is changing. When you call ScreenToWorldPoint, it will return you a point on the front camera plane for your camera.

This is a common bit of misunderstanding when just starting out! :slight_smile:

Picture it for a while, and you can probably see that any point on the screen really corresponds to a line going from somewhere on the ‘front’ of the camera outwards into the world. In fact, an extra unity function exists - ScreenPointToRay for exactly this reason:

When converting from a screen point to a position in the world, you need to make sure you pick the correct position on that ray to work with.

Because you’re just taking the screen point, what you’re seeing is that the object is moving closer and closer to the camera. i.e. in addition to its x and y changing, its z is moving towards the camera. When it eventually gets there it’ll be so close that it’ll be ‘just behind’ the camera and seem to dissapear.

If you’re using an orthographic camera, its as simple as reseting the z of your position:

pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = transform.position.z;

This is because orthographic cameras are ‘flat’ so the rays all point in the same direction regardless of the pixel.

If it’s a perspective camera, it’s a little more complex as you need to work out where your screen position corresponds to on the same plane as your object. I answered this one the other day:

that might be helpful :slight_smile:

-Chris