Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cr0ss · Dec 14, 2014 at 11:56 PM · vector3lerptransform.position

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cr0ss · Dec 15, 2014 at 02:48 AM 0
Share

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.

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges