Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Un4given · May 29, 2011 at 07:07 PM · movementplayerbackground

Check if player is moving

I am sure there is way to do this but im struggling to find how.

I am making a 2d platform and basically I want to check if the player is actually moving left or right (x axis) so I can move my background accordingly.

I know I could use:

 if(Input.GetAxis("Horizontal") > 0); 

But if my player hits a wall and the user continues to hold down the button, I dont want the background to continue moving.

Edit:
Here is some code. I haven't tested it or anything, but I've done this exact thing before.

 var previous_position = transform.position.z;
 var current_position = transform.positon.z; 
 //or whatever axis you're platforming along
 
 function Update()
 {
    if(previous_position > current_position)
    { 
       //do whatever you do when you move left
    }
    if(previous positoin < current_position)
    {
       //move right stuff
    }
    previous_position = current_position;
    current_position = transform.position.z;
 }
 // party time!

Make sense?

Comment
Add comment · Show 4
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 SirGive · May 29, 2011 at 08:35 PM 0
Share

I edited your question to provide the code. I'm looking into a solution involving the dot product now :P

And do notice that your using vectors for position, so they aren't "greater" than one another in the sense that your using them.

avatar image SirGive · May 29, 2011 at 08:37 PM 0
Share

Well the solution is quite easy, actually. Check my updated answer.

avatar image Un4given · May 29, 2011 at 08:38 PM 0
Share

Seems like I am getting there, the only problem is Unity doesn't like me attaching trasform to a variable

avatar image SirGive · May 29, 2011 at 08:42 PM 0
Share

alright, done updating.

5 Replies

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

Answer by Un4given · May 29, 2011 at 10:01 PM

Ok so I got it to work using:

 var old_pos : float;
 function Start(){
 old_pos = transform.position.x;
 }
 function Update(){
 if(old_pos < transform.position.x){
 print("moving right");
 }
 if(old_pos > transform.position.x){
 print("moving left");
 }
 old_pos = transform.position.x;
 }
 



Seems to work find and dandy so thanks for all the help guys.

Especially SirGive

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 SirGive · May 29, 2011 at 10:10 PM 0
Share

don't forget to choose this as the answer

avatar image
11

Answer by Meltdown · May 29, 2011 at 07:12 PM

If your player has a rigidbody attached you can check the velocity of your player.

 if(rigidbody.velocity.magnitude > 0)
 {
   // Player is moving
 }
Comment
Add comment · Show 3 · 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 SirGive · May 29, 2011 at 07:58 PM 1
Share

This would work if he is using a rigidbody, but what if he isn't? ;)

avatar image Mr_Mavik · Aug 04, 2020 at 06:15 PM 0
Share

How do I check only changes of Y axis magnitude?

avatar image scintilla0 · Apr 16, 2021 at 07:30 PM 0
Share

Best option for Rb users thanks

avatar image
3

Answer by $$anonymous$$ · Feb 19, 2019 at 01:08 AM

Ok i know im really really late but this might help someone so heres my solution for it its a bit more quicker than doing something that @Un4given has given.

 if (transform.hasChanged)
         {
             animationPlayer.SetBool("isWalking", true);
             transform.hasChanged = false;
         }
         else
         {
             animationPlayer.SetBool("isWalking", false);
             stillTime++;
             animationPlayer.SetInteger("canIdle", stillTime);
             if (stillTime == 1030)
             {
                 stillTime = 0;
             }
             print(stillTime);
         }

hope that helped

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 TheUninvited · Sep 22, 2019 at 10:52 AM 0
Share

i just logged in , just to say thank you really much it worked like a charm i am really appreciative about that!

avatar image
2

Answer by Catlard · May 29, 2011 at 07:14 PM

Yeah, you can also check to see if the player's position changed from the previous frame. Just store the transform.position.x, or whatever axis you're platforming along in a frame, and then during the next pass, before you reassign it, check to see if the player's position has changed...

Comment
Add comment · Show 5 · 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 Un4given · May 29, 2011 at 07:55 PM 0
Share

This is what I had in $$anonymous$$d but I am unsure how to implement this.

Could you help me out?

avatar image SirGive · May 29, 2011 at 07:59 PM 0
Share

Global variables. Can you post your movement code?

avatar image Catlard · May 29, 2011 at 08:14 PM 0
Share

Sure, I'll edit my original question!

avatar image Catlard · May 29, 2011 at 08:53 PM 2
Share

Huh? I did NOT say this ^

avatar image SirGive · May 29, 2011 at 08:59 PM 0
Share

I edited your comment and original answer so that the code was in the question.

Cool Story Bro btw :/

avatar image
0

Answer by SirGive · May 29, 2011 at 08:04 PM

Put this in your update:

 var old_pos : Vector3;
 
 
 function Update()
 {
    //movement code
 
    if(old_pos.x == transform.position.x)
    { 
       //move background
    }
    old_pos = transform.position;
 }
 // party time!

Its exactly as you said, you wanted to check the old position and see if it was the same, right?

Comment
Add comment · Show 4 · 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 SirGive · May 29, 2011 at 08:40 PM 0
Share

Updated :)

avatar image Un4given · May 29, 2011 at 08:49 PM 0
Share

Still getting this error:

ArgumentException: You are not allowed to call get_transform when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function.

so I have tried:

var previous_position; var current_position;

then in Start:

previous_position = transform.position.x; current_position = transform.position.x;

but still no joy :(

avatar image SirGive · May 29, 2011 at 08:51 PM 0
Share

don't use a transform, use Vector3. And make sure you declare it at the top of your script, before all of the functions.

avatar image SirGive · May 29, 2011 at 08:53 PM 0
Share

edited again using original code. and you wouldn't want to set previous_position to the same position as current. You want to wait until the position is new, then you would check it.

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

6 People are following this question.

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

Related Questions

Move player or background 0 Answers

Player isn't affected by AddForce(), Lerp() etc. 1 Answer

Player making Zigzag movement 0 Answers

auto run key 1 Answer

How do I fix collisions while using this PlayerController script? 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