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.