Skip to content

Instantly share code, notes, and snippets.

@phoenixperry
Last active December 15, 2019 09:15
Show Gist options
  • Save phoenixperry/6889430 to your computer and use it in GitHub Desktop.
Save phoenixperry/6889430 to your computer and use it in GitHub Desktop.
Unity c# example of Raycast
using UnityEngine;
using System.Collections;
public class RayCastExample : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
var up = transform.TransformDirection(Vector3.up);
//note the use of var as the type. This is because in c# you
// can have lamda functions which open up the use of untyped variables
//these variables can only live INSIDE a function.
RaycastHit hit;
Debug.DrawRay(transform.position, -up * 2, Color.green);
if (Physics.Raycast(transform.position, -up, out hit, 2))
{
Debug.Log("HIT");
if (hit.collider.gameObject.name == "floor")
{
Destroy(GetComponent("Rigidbody"));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment