Last active
December 15, 2019 09:15
-
-
Save phoenixperry/6889430 to your computer and use it in GitHub Desktop.
Unity c# example of Raycast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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