- Home /
Question by
Coaster-Mind · Apr 27, 2016 at 03:20 PM ·
raycastrigidbody2daddforcevector2
Raycast 2D
Hi, I want to make a thing that if it comes close to a collider it pushes back (This case up). But what do I need to change to get it to work for 2D?
using UnityEngine;
using System.Collections;
public class Cast : MonoBehaviour {
public Rigidbody2D RigidBody;
public float Force = 100f;
void Update ()
{
RaycastHit2D hit;
Vector2 down = transform.TransformDirection (Vector2.down) * 10;
Debug.DrawRay (transform.position,down,Color.green);
if(Physics2D.Raycast(transform.position,(down), out hit))
{
if (hit.distance <= 5)
{
GetComponent<Rigidbody2D> ().AddForce (Vector2.up * Force);
print ("Hit");
}
}
}
}
Thanks for reading. -CoasterMind
Comment
First of my internet can't handle the API. Well, a little but not much. second of, I didn't find anything usefull. https://i.gyazo.com/d840868497747b9bf0051df351f06662.png https://i.gyazo.com/b10f8e6bb7ebef74374b06260c2b5469.png
See, that's why you HAVE to find a way to use the Scripting API, it tells you exactly how functions like Physics.Raycast2D are called and what parameters are available.