- Home /
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.
Answer by justin35f · Dec 15, 2014 at 01:20 AM
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.
Hi Justin,
I see what you're talking about, and I think you might be on to something, but sadly that isn't the exact issue (at least I don't think so). Even if I take out everything but two lines:
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = Vector3.Lerp(transform.position, mousePosition, charSpeed * Time.fixedDeltaTime);
Just those two lines itself causes the character to disappear after a few seconds.
I also noticed it disappears as well if I just AddForce() to the position, forcing it to move forward. I am unsure why this is.
I think you might be right as far as the character rotating out of view somehow. I will look into it further.
Answer by cr0ss · Dec 15, 2014 at 04:37 AM
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!
Your answer
Follow this Question
Related Questions
MOVE GameObject to point A to B and to B to A 2 Answers
smoothly change position of an Object 1 Answer
Object keeps moving when using Vector3.Lerp on transform.position 0 Answers
Simple script to move object doesn't work 1 Answer
Unit Formation Aligns to a Reset Forward Position Rather Than Parent Object's Rotation 1 Answer