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. When I click to somewhere at screen, sprite is moving to that position. When sprite hit to cube, it should stop moving (when collision occured) , but instead it’s passing through it.
Sprite has these components:
Cube has this components:
BallControl Script
using UnityEngine;
using System.Collections;
public class BallControl : MonoBehaviour {
private Transform tf;
private Rigidbody2D rb;
Vector3 mousePosition;
void Start()
{
tf = GetComponent<Transform> ();
rb = GetComponent<Rigidbody2D> ();
}
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePosition.z = 0f; // Because we are working on 2D
}
transform.position = Vector3.MoveTowards(transform.position, mousePosition, 1.5f * Time.deltaTime);
}
}
Can you tell me why cube’s Box Collider not blocking sprite? Should i add something like this to my BallControl script?
void OnCollisionEnter(...)
{
// Stop moving
}