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 SC4V4NGER · Jan 15, 2014 at 05:41 PM · c#2dmovementbooleansquare

Stop Player movement when bool changes 2D

Well i thought a while on how to ask this question in the most simple way possible, as it could maybe help someone in the future and not just me :D

Here we go:

I have a script that moves my Player as long as I press the button "D", the trick to it is that the player moves in "Squares" (so if he stops moving he got exactly 1/2/3/4/5 .... "Squares" depending on how long you hold "D")

That looks something like this:

 public Vector3 pos;
 public float walkingSpeed = 2.0f;

 Start(){ pos = transform.position; }

 Update(){
           if (Input.GetKey (KeyCode.D) && transform.position == pos) {
                             pos += Vector3.right;

            transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * walkingSpeed);
  }


Now I want to know a way to make him do the exact same thing, but instead of "D" I want a boolean to handle this. [This boolean is activated when the player "collides" with a trigger, it has nothing to do with any keys]

So if the boolean changes to false, he got exactly 1/2/3/4/5 ..... Sqaueres depending on how long the boolean was true.

(How) Can I do this?

Thanks for any help :)

(c# would be great but java examples are welcome as well)

Comment
Add comment · Show 2
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 ABerlemont · Jan 15, 2014 at 06:07 PM 0
Share

public bool keyState = false; //property

Update(){ keyState = Input.Get$$anonymous$$ey($$anonymous$$eyCode.D); (...)

Or I didn't understand what you want to do ?

avatar image SC4V4NGER · Jan 15, 2014 at 06:52 PM 0
Share

no the boolean should not have anything to do with the key

if a bool is set to true (when colliding with a trigger but I know how that works) that should work INSTEAD of using any keys

3 Replies

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

Answer by SC4V4NGER · Jan 16, 2014 at 07:07 PM

So I am soory if my question was a little confusing...

Any way I found a solution to my problem (might be a pretty bad one but it works in my case :D)

I allready knew how to check for trigger "collision" (i asked that a while ago :D) but my specific Problem was that the action I wanted to excecute as long as the boolean is true needed to be excecutet over and over in order to work, here is my solution:

[mainScript] public Vector3 pos;

  Start() {pos = transform.position;}

 Update() {
  if (Input.GetKey (KeyCode.D) && transform.position == pos && move_index ==  0) {
             mainScript.toRight = true; //for the animation and the direction
             

             onIceMovement = true;

             ice_index = 1;

                 pos += Vector3.right;

         
         //////////////////////////////////////////////////////////////////////// INDEX 1

         if (mainScript.toRight && transform.position == pos && ice_index == 1)
         {
             
             
             pos += Vector3.right;

             ice_index = 2;

         }

         
         ////////////////////////////////////////////////////////////////////////////// INDEX 2

         if (mainScript.toRight && transform.position == pos && allowDirectionRight && ice_index == 2)
         {
             onIceMovement = true;
             
             mainScript.pos += Vector3.right;
             
             ice_index = 1;
             
         }
         
         


         /////////////////////////////////////// COLLISION -> INDEX 0


         if (mainScript.col_state_up && mainScript.toDown || mainScript.col_state_down && mainScript.toUp || mainScript.col_state_left && mainScript.toRight || mainScript.col_state_right&& mainScript.toLeft ) // this is basicly to stop him from moving as soon as he "collides"
         {
             ice_index = 0f;
             
         }
         
             

         ////////////////////
         transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * mainScript.walkingSpeed);  //this is what needed to be excecutet over and over, together with mainScript.pos += Vector3.right

  }


This is probably one of the worst answers you will ever see but I am not that good in explaining....

To summarize it:

I just made a kind of loop, because I always changed between two different if statements which would call each other in a way...

And as soon as the process should end, none of the if statement are called until it is startet aggain with the key :)

Aggain thanks for all the support, you simply have to love the unity community, you guys are great!

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
avatar image
0

Answer by sheffieldlad · Jan 15, 2014 at 09:32 PM

Something like...

 Void OnCollisionEnter()
 
 {
 if(myBool){
 // code to move your player here...
 }

Untested an unformatted mainly because im answering this on my phone but hopefully it will point you in the right direction.

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 sheffieldlad · Jan 15, 2014 at 09:37 PM 0
Share

Ignore that answer. I misread the question. I will give public the proper answer when i get home and i am in front of a computer.

avatar image
0

Answer by Squabbler · Jan 15, 2014 at 09:42 PM

If you want it to activate on a collision trigger, then you need to use the appropriate method for that.

Note that this is untested code, but I think should work - or at least get you in the right direction.

 // The key to check
 private bool myKey = true;


 void Update() {

   // Add the key to the if statement
   if (myKey && (transform.position == pos)) {
     pos += Vector3.right;
     transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * walkingSpeed);
   }

 }


 // Triggers for when colliders touch
 private void OnTriggerEnter (Collider col) {

   // First, if you want, check if it's the correct collider item by name or tag
   if (col.tag == "wall") {

     // Set the key, which will stop movement in Update method
     myKey = false;

   }
     
 }


 // Triggers for when the colliders stop touching
 private void OnTriggerExit (Collider col) {

   // First, if you want, check if it's the correct collider item by name or tag
   if (col.tag == "wall") {

     // Set the key, which will continue movement in the Update method
     myKey = true;

   }
     
 }
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 SC4V4NGER · Jan 16, 2014 at 06:32 PM 0
Share

thanks for the explenation but I allready knew that part :) But i think I have solved my problem, i posted an answer but it is still beeing checked, i hope I haven't explained what i did to poorly :D

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

21 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

2D-Enemy AI problem (2problems) 2 Answers

How do I access a bool from another script? 2 Answers

making an object move to a certain point 3 Answers

How to make sprites in SideScroller move in smooth curves 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