Hi, I am new to coding itself and trying to learn Unity. I am coding a simple wall breaker game and my script is to detect collisions in 2D and act appropriately (via the If - else loop).
One of the variables (normal) is a Vector 2 value to collect the last value in the collider.contacts array. I am getting a "Cannot implicitly convert type ‘UnityEngine.ContactPoint2D’ to ‘UnityEngine.Vector2’ error message when I execute the script. I am using the normal variable to check what collider the ball is colliding against in the script. Any help to resolve this issue is most appreciated. Thanks!
using UnityEngine;
using System.Collections;
public class Apple : MonoBehaviour
{
public float velocity=1;
private Vector3 _direction;
// Use this for initialization
void Start ()
{
float randomValue = Random.Range (-1.0f, 1.0f);
int xComponent = (int)Mathf.Sign (randomValue);
_direction = new Vector3 (xComponent, 1, 0);
_direction.Normalize ();
}
// Update is called once per frame
void Update ()
{
//GetComponent<Transform> ().position += _direction * velocity;
transform.position += _direction*velocity*Time.deltaTime;
}
//Detect Collisions
void OnCollisionEnter2D(Collision2D collider)
{
//checking whether the Apple is colliding with the "ColliderCreator"
ColliderCreator colliderCreator = collider.gameObject.GetComponent<ColliderCreator> ();
//checking whether the Apple is colliding with the "Platform"
Platform platform = collider.gameObject.GetComponent<Platform>();
Vector2 normal = collider.contacts [0];
bool isGameOver = false;
//checking whether the apple is colliding with the wall
if (colliderCreator != null)
{
if (normal == Vector2.up) //hitting the bottom wall
{
isGameOver = true;
}
}
else if (platform)
{
if (normal != Vector2.up) //if the apple strikes the platform on any side other than "Up"
{
isGameOver = true;
}
}
if (isGameOver)
{
//This is Game Over
}
else
//Reflecting the Apple's direction along the normal
_direction = Vector3.Reflect (_direction, collider.contacts [0].normal);
_direction.Normalize ();
}
}