I'm really newbie , watched about 20 videos about and tried to do something. But I still have problems with collisions.
I'm working on 2D. I have a Sprite and a Cube (GameObject>3D Object>Cube). sprite is moving to a random position on screen and when it arrived to that position it's moving to another random position. It's going on like this forever. But when sprite hit to cube, it should change direction (when collision occured) , but instead it's passing through it.
Sprite has these components:
Cube has these components:
BallControl Script
public class Move : MonoBehaviour {
public float moveSpeed = 3f;
private Transform tf;
Vector3 mousePosition;
bool isMoving=false;
void Start()
{
tf = GetComponent<Transform> ();
}
void Update ()
{
if(!isMoving) // Find a new position to move
walkToRandomPosition();
if(isMoving) // If we already found a position to move
transform.position = Vector3.MoveTowards(transform.position, mousePosition, moveSpeed * Time.deltaTime);
if (transform.position == mousePosition)
{ // Stop
isMoving = false;
}
}
void walkToRandomPosition()
{
mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0,Screen.width), Random.Range(0,Screen.height), Camera.main.farClipPlane/2));
mousePosition.z = 0f; // Because we are working on 2D
isMoving=true;
}
}
Can you tell me why cube's Box Collider not blocking sprite? Should i add something like this to my BallControl script?
void OnCollisionEnter2D(...)
{
// Change direction (find new position)
}
UPDATE 1
Just add a new sprite and gave BoxCollider2D , now collision working as expected. But it's still not working with 3D cubes.