- Home /
Other
Collision for a block maze script C#
I'm building a game for a project for school. I had the idea for a maze game with a couple twist and turns. The Idea for the script is to press one of the WASD keys down and you would move one space forwards. I got this but it has no collision. I need to know how to move a set amount with one press of a key.
The script
using UnityEngine;
using System.Collections;
public class MazeController : MonoBehaviour {
void Update () {
if(Input.GetKeyDown("d"))
transform.position += Vector3.right;
if(Input.GetKeyDown("a"))
transform.position -= Vector3.right;
if(Input.GetKeyDown("w"))
transform.position += Vector3.forward;
if(Input.GetKeyDown("s"))
transform.position -= Vector3.forward;
}
}
Answer by rejwan1 · Feb 08, 2014 at 01:18 AM
Assuming on what exactly you want to do, I can suggest 2 solutions:
Your maze will be built of tiles, your character moves exactly 1 tile every time.
Don't use colliders or physics at all, store your maze in a 3d bool array (whether or not each tile has a wall). Then when the player tries to move, move him first in the array - if there is a block there, don't allow the move.
Your maze will be built of irregular sizes, movement will be irregular or you want to add more complex behavior
Put a rigidbody + collider on the moving object. Put colliders on the walls (no need for a rigidbody as they are fully static). Then inside FixedUpdate (not update), use rigidbody.AddForce to move your character
I was afraid you'd say that so I just decided to take a simpler approach and go with the 3rd person controller and leave it at that. Thank you anyways.