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

Maze Game

Discussion in 'Scripting' started by shawnpigott, Oct 4, 2005.

  1. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    Hello Again,

    I posted a request for camera movement help and received the information I requested (thanks again). Unfortunately I hadn't quite thought the problem through and I now have a new problem.

    I'll recap. I work with a young man with cerebral palsy. He accesses the computer using two chin switches. I have mapped these switches to the 'a' and 'b' keys. I asked for a way to move the character forward with the 'a' key and to turn 90 degrees to the right with the 'b' key. This way he can move around. This is the script that was suggested to me:

    var walkStep = 10.0;
    var angleStep = 90.0;
    function Update () {
    if (Input.GetKeyDown("a")) {
    transform.position += transform.TransformDirection(Vector3.fwd * walkStep);
    }
    if (Input.GetKeyDown("b")) {
    transform.RotateAround(transform.position, Vector3.up, angleStep);
    }
    }

    ...and it works perfectly for what I asked. The problem is that I am creating a maze style game and by having the character jump forward a specific distance, the character is able to jump through the walls so there is no physics detection. I guess I need the character to move forward a specified distance but to stop if it hits a wall.

    Any help with this would be appreciated.
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    As you may guess, there are scads of ways to go about this.

    A simple approach would be to raycast the step distance in the direction the character is facing when the movement button is pressed. If the raycast hits something, don't actually move. If it doesn't, move. One of the manuals around here has an example of raycasting to get you started.

    You could then expand this to keep track of a goalPosition in which you interpolate between the current transform.position and the goalPosition each frame. Now the character moves smoothly instead of in choppy increments. You could probably use Vector3.Lerp for the interpolation just fine.

    Good luck
    -Jon
     
  3. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I found the raycast reference in the manual and came up with this script:

    var walkStep = 10.0;
    var angleStep = 90.0;

    function Update () {
    var hit : RaycastHit;
    var fwd = transform.TransformDirection (Vector3.forward);

    if (Physics.Raycast (transform.position, fwd, 10)) {
    // You can only turn as there is something in front of you
    if (Input.GetKeyDown("b")) {
    transform.RotateAround(transform.position, Vector3.up, angleStep);
    }
    }
    else {
    // You can move and turn
    if (Input.GetKeyDown("a")) {
    transform.position += transform.TransformDirection(Vector3.fwd * walkStep);
    }
    if (Input.GetKeyDown("b")) {
    transform.RotateAround(transform.position, Vector3.up, angleStep);
    }
    }

    }


    Unfortunately, it isn't working properly. I am able to turn but not move no matter what is in front of the character. My programming experience comes from realbasic so I may just be missing something in the layout of information.

    Any help would be appreciated.
     
  4. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    I would guess that you are casting a ray from the inside of the character object... inside the primitive collider. That means the ray hits the inside of the character before it ever has a chance to escape. A quick solution would be to put the character and its colliders in the Ignore Raycast layer.

    -Jon
     
  5. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I hadn't thought of that. I'll give it a try.

    Thanks.
     
  6. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I poked around a bit and managed to get it working.

    One final question. This works great for flat areas but if I want to include uneven terrain or ramps would it work the same as for the character itself.

    I mean, if the ramp is in the ignore raycast layer but it is still working as a collider, will the character be able to move up or down them?

    Thank for all your help.
     
  7. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Cool.

    I think adding another dimension to the game starts to complicate things. Now you're going to want to start adding forces and snapping to the object downwards and such instead of moving it directly.

    It may be better to discuss this more in real time in for instance our Unity IRC channel: http://forum.otee.dk/viewtopic.php?t=734

    There are usually a few great people there at least.

    -Jon
     
  8. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I think I have enough to keep me busy for a while. My friend is interested in me creating a model of his house for him to explore (he's in a wheel chair), but I think I'll start him off on a flat one level maze and as his skill (as well mine) increases we'll look into other options.

    I'll check out the IRC channel.

    Thanks for all you help.