unity raycast 2d
C#
using UnityEngine;public class Example : MonoBehaviour
{
// Float a rigidbody object a set distance above a surface. public float floatHeight; // Desired floating height.
public float liftForce; // Force to apply when lifting the rigidbody.
public float damping; // Force reduction proportional to speed (reduces bouncing). Rigidbody2D rb2D;
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
} void FixedUpdate()
{
// Cast a ray straight down.
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up); // If it hits something...
if (hit.collider != null)
{
// Calculate the distance from the surface and the "error" relative
// to the floating height.
float distance = Mathf.Abs(hit.point.y - transform.position.y);
float heightError = floatHeight - distance; // The force is proportional to the height error, but we remove a part of it
// according to the object's speed.
float force = liftForce * heightError - rb2D.velocity.y * damping; // Apply the force to the rigidbody.
rb2D.AddForce(Vector3.up * force);
}
}
}
Physics2D.Raycast(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);
Also in C#:
- Title
- panel drag all form c#
- Category
- C#
- Title
- xamarin forms alarm
- Category
- C#
- Title
- how to use K2 games Games parallax background
- Category
- C#
- Title
- c# create array of int
- Category
- C#
- Title
- unity clamp rotation
- Category
- C#
- Title
- Exception thrown: 'System.FormatException' in mscorlib.dll dates
- Category
- C#
- Title
- rotatearound unity
- Category
- C#
- Title
- c# get desktop path
- Category
- C#
- Title
- unity rigidbody constraints
- Category
- C#
- Title
- math.pow in C# using loop
- Category
- C#
- Title
- how to add a componet to a gameobject throgh code unity
- Category
- C#
- Title
- C# foreach loop async but wait at end
- Category
- C#
- Title
- c# webrequest cookies
- Category
- C#
- Title
- .net directorysearcher get manager accountname
- Category
- C#
- Title
- show double in textbox c#
- Category
- C#
- Title
- c# get list of all class fields
- Category
- C#
- Title
- c# read char
- Category
- C#
- Title
- c# convert list to string
- Category
- C#
- Title
- animator hash in unity
- Category
- C#
- Title
- prefab gets character transform
- Category
- C#
- Title
- ping with c#
- Category
- C#
- Title
- how to make a datatable in c#
- Category
- C#
- Title
- unity input get axis
- Category
- C#
- Title
- while loop in c#
- Category
- C#
- Title
- unity how to change rotation
- Category
- C#
- Title
- snippet to create constructor in asp.net c#
- Category
- C#
- Title
- add getenumerator to class c#
- Category
- C#
- Title
- generate a dropdown list from array data using razor .net mvc
- Category
- C#
- Title
- define enum c#
- Category
- C#
- Title
- navigate to another page with an object uwp c#
- Category
- C#
- Title
- isdaylightsavingtime in c#
- Category
- C#
- Title
- C# save pdf stream to file
- Category
- C#
- Title
- how to add object in dictionary in c#
- Category
- C#
- Title
- how to convert iformfile to byte array c#
- Category
- C#
- Title
- unity create a child object
- Category
- C#
- Title
- unity quaternion
- Category
- C#
- Title
- unity detect any key
- Category
- C#
- Title
- c# update control from another thread
- Category
- C#
- Title
- how to detected WindowCloseEvent in other window wpf
- Category
- C#
- Title
- razor preview
- Category
- C#
- Title
- compose graphql query string in c#
- Category
- C#
- Title
- unity 2d top down movement
- Category
- C#
- Title
- .net core copy file in folder to root
- Category
- C#
- Title
- c# clear console read chache
- Category
- C#
- Title
- create expression func c# for use in where clause
- Category
- C#
- Title
- copy a list in c# unity
- Category
- C#
- Title
- modificare una strinfa in c#
- Category
- C#
- Title
- how to disable a gameObject unity c#
- Category
- C#
- Title
- c# inline initialize dictionary
- Category
- C#
- Title
- unity normalize vector2
- Category
- C#
- Title
- C# previous method
- Category
- C#
- Title
- c# data types
- Category
- C#
- Title
- c# keyvaluepair
- Category
- C#
- Title
- Net.ServicePointManager.SecurityProtocol .net framework 4
- Category
- C#
- Title
- unity string format time
- Category
- C#
- Title
- unity oncollisionenter2d
- Category
- C#
- Title
- how to get the askii code of a char in c#
- Category
- C#
- Title
- convert from xls to xlsx C#
- Category
- C#
- Title
- convert system.byte a string c#
- Category
- C#
- Title
- how to make an array in csharp
- Category
- C#
- Title
- unity load scene
- Category
- C#
- Title
- : ? conditioanl statement c#
- Category
- C#
- Title
- c# create a text file
- Category
- C#
- Title
- what does thismean incsharp public static void Main
- Category
- C#
- Title
- Check object is in layermask unity
- Category
- C#
- Title
- c# declare an int list
- Category
- C#
- Title
- c# print
- Category
- C#
- Title
- c# make string null
- Category
- C#
- Title
- how do i limit the amount of prefabs in unity using c# script
- Category
- C#
- Title
- how to destroy a gameobject after some hits in unity 3d
- Category
- C#
- Title
- microsoft input tool need .net framework
- Category
- C#
- Title
- office open xml check if row is empty
- Category
- C#
- Title
- unity mobile controls
- Category
- C#
- Title
- roulette algorithm genetic algorithm
- Category
- C#
- Title
- c sharp string replace
- Category
- C#
- Title
- array c#
- Category
- C#
- Title
- attribute usage c#
- Category
- C#
- Title
- visual studio c# mark class deprecated
- Category
- C#
- Title
- how to run csharp in visual studio code
- Category
- C#
- Title
- how to reference the position of a game object unity
- Category
- C#
- Title
- check if number is even or odd c#
- Category
- C#
- Title
- snake game
- Category
- C#
- Title
- c# empty IEnumerable
- Category
- C#
- Title
- c# dictionary literal initializer
- Category
- C#
- Title
- c# LCP
- Category
- C#
- Title
- get permission to write read file and directory on file system C#
- Category
- C#
- Title
- c# listview
- Category
- C#
- Title
- how to declare a string c#
- Category
- C#
- Title
- c# md5 hash file
- Category
- C#
- Title
- what data type should be for contact number in asp.net
- Category
- C#
- Title
- How to execute script in C#
- Category
- C#
- Title
- c# multiple catch exceptions
- Category
- C#
- Title
- binding c#
- Category
- C#
- Title
- isprime c#
- Category
- C#
- Title
- how to say or in c#
- Category
- C#
- Title
- list of vectors c#
- Category
- C#
- Title
- how to create a variable in C#
- Category
- C#
- Title
- C# assigning image location
- Category
- C#
- Title
- wpf textblock line break code behind
- Category
- C#
- Title
- if entity.is Transient() Update Mvc 5 c#
- Category
- C#