• 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 ciwiaf · Jul 20, 2015 at 02:20 PM · transform.positioncomparevector3.position

how to compare position.x last position to current?

Hi, I wanted to compare the last position with the current position of my player. Here's my current code:

     public GameObject player;
 
     Vector2 lastPos;
     Vector2 currentPos;
     
 
     void Update(){
 
         currentPos = player.transform.position;
 
         if (currentPos.x < lastPos.x) {
             gameObject.GetComponent<ParticleSystem> ().enableEmission = true;
         } else {
             gameObject.GetComponent<ParticleSystem> ().enableEmission = false;
         }
 
         lastPos = currentPos;
 
     }


I'm trying to play the particle system whenever the current position of the player is lower than before(the lastpos). I'm not sure what's wrong though, my logic or code or both? any help would be appreciated. Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Veldars · Jul 20, 2015 at 02:45 PM

Ok I figure out what is your problem.

You must enable your parcticle system for a minimum amount of time. actualy you active and desactive the emiter imediatly after the movement. This script works on my new project :

     public GameObject player;
     
     Vector3 lastPos;
     Vector3 currentPos;
     float _time = 0;
 
     void Awake() {
         lastPos = player.transform.position;
     }
     
     void Update(){
         
         currentPos = player.transform.position;
 
 
 
         if (currentPos.x < lastPos.x) {
             gameObject.GetComponent<ParticleSystem> ().enableEmission = true;
         } else if (_time > 2f) {
             gameObject.GetComponent<ParticleSystem> ().enableEmission = false;
             _time = 0;
         } else if (_time < 2f) {
             _time += Time.deltaTime;
         }
         
         lastPos = currentPos;
         
     }


I hope this help.

Comment
Add comment · Show 2 · 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 ciwiaf · Jul 20, 2015 at 03:06 PM 0
Share

Ah, O$$anonymous$$, I got it now, I'll try this one later. Thanks :)

EDIT: It's working perfectly now, thanks! quick question though, what was the underscore in variable time for?

avatar image Veldars · Jul 21, 2015 at 06:03 AM 0
Share

Sorry it's just a personal reflex, for private variable ^^

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I add a vector3.lerp or movetoward 1 Answer

Removing Vector3 Decimal for Position 2 Answers

bullet's flight adjusts position according to a parent object ?? 1 Answer

Moving from point A to B and back in a continuous loop? 2 Answers

Script causing huge lag 0 Answers


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