Trying to get a Circle Collider 2d to move around in a play area.
Super basic Game Jam Project: All I need is 2d (topdown) sprites with CircleColliders, to move around in an area of walls made from collider boxes. I've spent hours trying to solve and I'm stumped any help would be SUPER appreciated, even if somebody could just point me to the right documentation.
Start at Spawn Point
Move Right with a Random Y axis
Detect Wall and Stop
Pick a new X and Y then Move
Repeat.
This is what I have thus far(that works):
public float moveSpeed = 1.0f;
public Vector2 userDirection = new Vector2(-1,1);
public float turnVelocity = 0.5f;
public Transform moving;
public GameObject movingTarget;
void Start(){
this.gameObject.transform.position = new Vector2 (-6, -1.5f);
}
void Update(){
transform.Translate (userDirection * moveSpeed * Time.deltaTime);
transform.rotation = Quaternion.identity;
if (!moving) {
Debug.Log ("HIT WALL");
} else {
Debug.Log ("MOVING");
}
}
Answer by DroidifyDevs · Jan 31, 2016 at 12:31 AM
1: To start at spawn point, simple move the object you're moving to the spawn in the scene editor. So if you want it at x = 1, y = 8, z = 6, then simply set it's position to those values, and when you load the scene it will start there.
2: You'll need to make controls. For simple controls this will do: http://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-player
I think transform.Translate is for mobile only, but not sure.
3: To detect a wall you can use a collider on your game object and set it to "trigger". The use the OnTriggerEnter to stop the object (http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html)