Vector3.Lerp making my object invisible.

Hi there,

So I am just making a simple little test game and my character I have on a 2D plane is moving via following (Lerping toward) my mouse cursor. When I start the game (whether in editor or a built standalone .exe) after about 3 seconds of run time, the character goes invisible. In the editor, it still shows within the Scene, but the Gameplay has it disappear(invisible). I have narrowed the problem down to when I am doing the Lerp method “transform.position = Vector3.Lerp(transform.position, mousePosition, charSpeed * Time.fixedDeltaTime);”.

Otherwise, this functions perfectly how I want it so far, but I have no idea why it is making my character go invisible after 3 seconds of runtime. Any thoughts at all?

On an Update () I have the following code:

using UnityEngine;
using System.Collections;

public class CharacterMove : MonoBehaviour 
{
	float charSpeed = 1.0f;

	// Use this for initialization
	void Start () 
	{

	}
	
	// Update is called once per frame
	void Update () 
	{
		Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

		Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition);
		rot *= Quaternion.Euler(0,0,90);
		
		// Set x scale to negative if on left
		if ((transform.position.x - mousePosition.x) > 0)
		{
			transform.localScale = new Vector3(-0.3f, 0.3f, 0.3f);
		}
		
		// Set x scale to positive if mouse is on right side
		if ((transform.position.x - mousePosition.x) < 0)
		{
			transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
		}

		
		// Strip x and y rotation
		transform.eulerAngles = new Vector3(0,0,transform.eulerAngles.z);
		
		// Apply movement - prevent y axis movement (only left and right)
		if (mousePosition.y > transform.position.y || mousePosition.y < transform.position.y)
		{
			mousePosition.y = transform.position.y;
		}
		
		transform.position = Vector3.Lerp(transform.position, mousePosition, charSpeed * Time.fixedDeltaTime);

	}

}

Any thoughts? If you want to plug this into a C# script and try it out, I’m fairly certain it will happen to yours as well.

It looks like some of your script is missing, as you create a rotation Quaternion, but aren’t actually using it anywhere. Unfortunately I can’t tell for sure, but I have a strong suspicion this is the issue as it is most likely rotating out of view. Since you are using a 2D plane, just a little bit of rotation on an axis other than the z axis (assuming default Unity2D setup) will rotate it out of view.

The reason you would still see it in the scene view, is because the scene view is still 3D, and you can rotate it around to see it.

Ok so I messed with it a bit, and thanks to Justin35f I found the solution. Turns out, due to some reason I cannot actually comprehend just yet (and if anyone knows, please let me know!), the character was moving to a negative Z position. I just added a firm “mousePosition.z = 0;” to the Update() and it keeps it from being hidden behind all the other sprites on the screen.

Thanks a lot!