Detect edge collision...?
Hi everybody! I created a sprite that should walk across the screen with a costant speed, bouncing on edges with non-regular bounce (when touches an edge, it change direction to a random number)...
I wrote this so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PredaControllerScript : MonoBehaviour
{
float movX, movY;
// Use this for initialization
void Start()
{
}
void FixedUpdate()
{
if (/*Sprite is Touching an Edge*/){
changeDirection();
}
GetComponent<Rigidbody2D>().velocity = new Vector2(movX, movY);
}
void changeDirection()
{
int rand;
movX = Random.Range(5, 15);
movY = Random.Range(5, 15);
do
{
rand = Random.Range(-10, +10);
if (rand > 0)
movX = -movX;
} while (rand == 0);
do
{
rand = Random.Range(-10, +10);
if (rand > 0)
movY = -movY;
} while (rand == 0);
}
}
What should I write instead of /Sprite is Touching an Edge/ ?
I tried to use Edge Collider 2D, but I didn't understand how to use it... Thank you in advance! bye!
Ale
Comment