unity3d raycast
C#
using UnityEngine;
// C# example.
public class ExampleClass : MonoBehaviour
{
void Update()
{
// Bit shift the index of the layer (8) to get a bit mask
int layerMask = 1 << 8;
// This would cast rays only against colliders in layer 8.
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
layerMask = ~layerMask;
RaycastHit hit;
// Does the ray intersect any objects excluding the player layer
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log("Did Hit");
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
Debug.Log("Did not Hit");
}
}
}using UnityEngine;
// C# example.
public class ExampleClass : MonoBehaviour
{
void Update()
{
// Bit shift the index of the layer (8) to get a bit mask
int layerMask = 1 << 8;
// This would cast rays only against colliders in layer 8.
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
layerMask = ~layerMask;
RaycastHit hit;
// Does the ray intersect any objects excluding the player layer
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log("Did Hit");
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
Debug.Log("Did not Hit");
}
}
}
This example creates a simple Raycast, projecting forwards from the position of the object's current position, extending for 10 units.
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
void FixedUpdate()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, 10))
print("There is something in front of the object!");
}
}
Also in C#:
- Title
- how to convert float to int c#
- Category
- C#
- Title
- c# long to int
- Category
- C#
- Title
- exception handling c# stack overflow
- Category
- C#
- Title
- string to guid c#
- Category
- C#
- Title
- c# get the last item in a list
- Category
- C#
- Title
- how to get object to spawn in a curcle
- Category
- C#
- Title
- how to turn a string in a char list c#
- Category
- C#
- Title
- c# mathf.ceiling
- Category
- C#
- Title
- try catch c#
- Category
- C#
- Title
- c# array Reverse method
- Category
- C#
- Title
- calculate impact damage + unity
- Category
- C#
- Title
- what does thismean incsharp public static void Main
- Category
- C#
- Title
- c# 2 timespan return yesterday
- Category
- C#
- Title
- epplus excel vb.net
- Category
- C#
- Title
- c sharp check if key in dictionary
- Category
- C#
- Title
- C# .net core convert string to enum
- Category
- C#
- Title
- unity randomly generated numbers c#
- Category
- C#
- Title
- c# split a string and return list
- Category
- C#
- Title
- how to convert from hexadecimal to binary in c#
- Category
- C#
- Title
- order by C#
- Category
- C#
- Title
- compile in one single exe c#
- Category
- C#
- Title
- webutility.urlencode space
- Category
- C#
- Title
- C# public 2d array
- Category
- C#
- Title
- c# convert double to int
- Category
- C#
- Title
- generate a dropdown list from array data using razor .net mvc
- Category
- C#
- Title
- c# ramdom
- Category
- C#
- Title
- c# reverse a string
- Category
- C#
- Title
- how to make an array in csharp
- Category
- C#
- Title
- check if number is even or odd c#
- Category
- C#
- Title
- c# System.Resources.MissingManifestResourceException error
- Category
- C#
- Title
- how to make % posibility to spawn an object C# in unity
- Category
- C#
- Title
- c# read excel file
- Category
- C#
- Title
- public enum c#
- Category
- C#
- Title
- how to stop player rotating when hit by object
- Category
- C#
- Title
- how to parse a string to an integer c#
- Category
- C#
- Title
- decimals not stored in azure tables
- Category
- C#
- Title
- cannot convert string to generic type c#
- Category
- C#
- Title
- how to access first child of parent unity
- Category
- C#
- Title
- convert array object to int[] c#
- Category
- C#
- Title
- dto and automapper
- Category
- C#
- Title
- doest all the methos in interface need to implement c#
- Category
- C#
- Title
- unity custom editor save changes
- Category
- C#
- Title
- how to create a variable in C#
- Category
- C#
- Title
- unity onclick addlistener
- Category
- C#
- Title
- c sharp substring
- Category
- C#
- Title
- kill child C#
- Category
- C#
- Title
- C# linq mselect
- Category
- C#
- Title
- c# empty char
- Category
- C#
- Title
- how to clear console in c#
- Category
- C#
- Title
- c# list remove item based on property duplicate
- Category
- C#
- Title
- c# mailmessage set sender name
- Category
- C#
- Title
- orderby make sunday last day c#
- Category
- C#
- Title
- edit form item from class C#
- Category
- C#
- Title
- configure 1 to 1 relation ef
- Category
- C#
- Title
- show double in textbox c#
- Category
- C#
- Title
- HashSet C# append
- Category
- C#
- Title
- Unity C# instantiate prefab
- Category
- C#
- Title
- .net 4.5 use tls 1.2
- Category
- C#
- Title
- new command - latex
- Category
- C#
- Title
- c list add element
- Category
- C#
- Title
- computer
- Category
- C#
- Title
- how to delay something in c# unity
- Category
- C#
- Title
- c# inline initialize dictionary
- Category
- C#
- Title
- array copy c#
- Category
- C#
- Title
- unity onselect gizmos wireframe
- Category
- C#
- Title
- c# expression func automatically select return type
- Category
- C#
- Title
- first sentence letter capital in c#
- Category
- C#
- Title
- c# make string null
- Category
- C#
- Title
- c# authorize attribute
- Category
- C#
- Title
- c# .net core memory cache
- Category
- C#
- Title
- how to make a game
- Category
- C#
- Title
- winforms messagebox with button
- Category
- C#
- Title
- assign datasource to dropdownlist in c#
- Category
- C#
- Title
- list c#
- Category
- C#
- Title
- enums c#
- Category
- C#
- Title
- c# global enumerator
- Category
- C#
- Title
- displayname c#
- Category
- C#
- Title
- isInstanceOf nunit
- Category
- C#
- Title
- c# find element in list of list
- Category
- C#
- Title
- c# windows application get current path
- Category
- C#
- Title
- set object to random color unity
- Category
- C#
- Title
- csharp attributes as generics constraints
- Category
- C#
- Title
- bundle.config in mvc is missing
- Category
- C#
- Title
- unity remove gameobject
- Category
- C#
- Title
- c# get excel column number from letter
- Category
- C#
- Title
- how to locate a specific element in a list c#
- Category
- C#
- Title
- JavaScriptSerializer() and convert to base64
- Category
- C#
- Title
- unity oncollisionenter2d
- Category
- C#
- Title
- c# string to datetime
- Category
- C#
- Title
- drag object unity 2d
- Category
- C#
- Title
- Basic fps camera C#
- Category
- C#
- Title
- create a file in the directory of the exe and write to it c#
- Category
- C#
- Title
- Move player on planets in unity 2d
- Category
- C#
- Title
- out parameters c#
- Category
- C#
- Title
- asign only common fields in c# object
- Category
- C#
- Title
- rotation unity script 2d
- Category
- C#
- Title
- vector3.lerp
- Category
- C#
- Title
- wpf restart application c#
- Category
- C#
- Title
- how to wait in c#
- Category
- C#
- Title
- unity deactive all object in list
- Category
- C#