Search Unity

Setting boundaries for movement

Discussion in 'Scripting' started by DanC4217, Apr 27, 2015.

  1. DanC4217

    DanC4217

    Joined:
    Apr 25, 2015
    Posts:
    2
    So, I'm completely new to scripting, C# and Unity in general, and I got this piece of code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     public float Speed = 10;
    7.     public float StrafeSpeed = 5;
    8.  
    9.     bool TouchingWalls = false;
    10.    
    11.  
    12.     void Update ()
    13.     {
    14.         transform.Translate (Vector3.forward * Speed * Time.deltaTime);
    15.  
    16.         if (Input.GetKey (KeyCode.D) && TouchingWalls == false)
    17.             transform.Translate (new Vector3(1, 0f, 0f) * StrafeSpeed * Time.deltaTime);
    18.         if (Input.GetKey (KeyCode.A) && TouchingWalls == false)
    19.             transform.Translate (new Vector3(-1, 0f, 0f) * StrafeSpeed * Time.deltaTime);
    20.     }
    21.  
    22.     void OnTriggerEnter(Collider other)
    23.     {
    24.         if (other.gameObject.CompareTag ("Stage"))
    25.             print ("I'm touching a wall");
    26.             TouchingWalls = true;
    27.     }
    28. }
    What I want to do is to make it so that my player isn't able to move a direction (in this case left or right) once it touches a wall (Tagged "Stage"), which I've already done, but say, once it touches the left wall, it then isn't able to move left or right. I want the player to be able to move right is it's touching the left wall and viceversa. I'm not very bright so what I've thought is each wall having a different tag, but I reckon that wouldn't be very good.

    The game is some sort of 3D infinite runner, so he moves forward by default. Also, I want to say that I'm aware I could be using GetAxis, custom functions and probably more stuff that would be more efficient, but for now this is only to practice scripting and I want to solve my problem with what I'm using (if there's even a way).
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Rigidbodies can't pass through walls.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I have to disagree with both of these statements.

    What you want is that he can only move right if he's touching the left wall, and can only move left if he's touching the right wall. So, each wall has to have a different tag.

    And the fact that you came up with that on your own, and for that matter came up with such a nontrivial (and mostly correct) script when you're brand new to scripting, indicates to me that you are, in fact, quite bright. You've done much better already than many other beginners have done.

    Good luck, and have fun!
    - Joe
     
    spaceace_ and hamsterbytedev like this.
  4. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    A gentleman as always Joe. Cheers!
     
  5. DanC4217

    DanC4217

    Joined:
    Apr 25, 2015
    Posts:
    2
    Thanks to both for answering!
    @outwarddesign I know that, but these walls are very thin and since I'm using transform.Translate as my way of moving, the player (a cube, for prototyping purposes) tries to get to the other side of the walls. As the walls are thin, if Speed is to high, the cube will be translated to the other side. If Speed is too low, it would be painfully slow to strafe. If Speed is within a medium ground, I get this weird animation every frame where the cube glitches in the wall but gets back to the stage in the next frame. This problem is what I was trying to solve.

    @JoeStrout Well, thank you, I didn't think this was much or even a fair start in scripting. :p

    I guess then that's the answer then. Thanks to both again, I'll get going to set another scene for myself to practice then. :)
     
    JoeStrout likes this.