- Home /
 
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)
public bool keyState = false; //property
Update(){ keyState = Input.Get$$anonymous$$ey($$anonymous$$eyCode.D); (...)
Or I didn't understand what you want to do ?
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
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!
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.
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.
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;
   }
     
 }
 
              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