Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Locking controller until Collision

Discussion in 'Scripting' started by Cous, Nov 27, 2015.

  1. Cous

    Cous

    Joined:
    Apr 17, 2015
    Posts:
    36
    Hii,


    I'm trying to have my puzzle game so you can only input one direction at a time. What I want to do is have the character be able to travel North, east, south and west; but not North east, south east, south west or north west. With this, I want the character to move constantly in one direction until collision, then after that you may change direction again.

    My game is similar to Orbox-C, if you've heard of that.
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Can you post your movement script (please use code tags) so we can see how you are currently doing it.
     
  3. Cous

    Cous

    Joined:
    Apr 17, 2015
    Posts:
    36
    This is the code, its ever so slightly adaptation of the roller-a-ball tutorial, that is hosted by unity.
    My guess is I need to edit the vector3, to ignore commands when my character is in motion but I have no idea how to do that.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.  
    7.     public float speed;
    8.  
    9.     private Rigidbody rb;
    10.  
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         float moveHorizontal = Input.GetAxis("Horizontal");
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.  
    21.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    22.  
    23.      
    24.     rb.AddForce(movement * speed);
    25.     }
    26.  
    27.  
    28. }
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    To prevent diagonal movement, & this may not be the best way but I used it & it worked, I used if statements along the lines of (pseudo code follows)

    Code (CSharp):
    1. If (moveHorizontal != 0 && moveVertical ==0) {
    2. Insert movement code here for horizontal movement
    3. }
    4.  
    5. Else if ( insert the vertical equivalent of above)
    This should then only accept user input if they are using one movement key.

    If you want to only let them move after they have collided with something you could have a movement bool that is set to false when they use a movement key & is then set to true when they collide with whatever they need to collide with. You would then add the book into your if statements for checking if they can move. Just make sure that they will always collide with something or they will either keep moving forever or get stuck where the bool is still false & the player can no longer control it.
     
  5. Cous

    Cous

    Joined:
    Apr 17, 2015
    Posts:
    36
    Thank you for your help, would this code replace the float or vector3?
     
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    the vector 3 would be incorporated into it so that yo uhave the vert movement in the bit related to that & the rest 0's & similar for the horiz movement
     
  7. Cous

    Cous

    Joined:
    Apr 17, 2015
    Posts:
    36
    Still got an error, it doesnt understand the if statement?

    (26,12) " Unexpected symbol. ' else'

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.  
    7.     public float speed;
    8.  
    9.     private Rigidbody rb;
    10.  
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         float moveHorizontal = Input.GetAxis("Horizontal");
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.  
    21.         If(moveHorizontal != 0 && moveVertical == 0);
    22.         {
    23.             Vector3 movement = new Vector3(moveHorizontal, 0.0f);
    24.         }
    25.  
    26.         else if Vector3 movement = new Vector3(moveVertical, 0.0f);
    27.  
    28.  
    29.  
    30.  
    31.         rb.AddForce(movement * speed);
    32.    
    33. }
     
  8. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    The code should look like this :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float Speed;
    7.  
    8.     private Rigidbody rigidBody;
    9.  
    10.     private void Start()
    11.     {
    12.         rigidBody = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.     private void FixedUpdate()
    16.     {
    17.         float moveHorizontal = Input.GetAxis("Horizontal");
    18.         float moveVertical = Input.GetAxis("Vertical");
    19.         if (moveHorizontal != 0f && moveVertical == 0f)
    20.         {
    21.             Vector3 movement = new Vector3(moveHorizontal, 0f);
    22.         }
    23.         else
    24.         {
    25.             Vector3 movement = new Vector3(moveVertical, 0f);
    26.         }
    27.         rigidBody.AddForce(movement * Speed);
    28.     }
    29. }
    You should watch some courses on C# basics, i'm guessing you never took any lessons at all judging by the errors.
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I think the else statement should also include all the requirements for moving that way, e.g. Bool check, direction != to 0 etc otherwise it will always move that way
     
  10. Cous

    Cous

    Joined:
    Apr 17, 2015
    Posts:
    36

    Having issues with an undeclared movement variable... ?
     
  11. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Your vector3's need some another point, you are showing 2 but need 3
     
  12. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Yup, didn't notice that. The Vector3 constructor takes 3 arguments.
     
  13. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
  14. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Write it out in plain English as bullet points with one instruction per point then convert that to code e.g.
    1. When moving left or right the player can't move forward or backward
    2. When moving forward or backward the player can't move left or right
    3. Once moving the player can't change direction or move again until the character collides with an object
    Etc

    This gets you rules like:
    To move left or right the horizontal input is not = 0 & vertical input must =0, they must have collided with something so they are able to move (e.g. A bool says they can move), once moving they can't move again until they collide with something (the bool is set to say they can't move)

    Build the script for moving left/right & test it moving between 2 objects that it collides with. Once it is working then the code is the same for moving forward/backward but you change the input parameters, checks & movement vector.