unity raycast 2d
// detect object that was clicked using raycast
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider != null)
{
Debug.Log ("Target name: " + hit.collider.name);
}
3.8
5
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);
}
}
}
Thank you!
5
0
4.57
7
Physics2D.Raycast(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);
Thank you!
7
0
4
1
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);
}
}
}
Thank you!
1
0
Are there any code examples left?
New code examples in category C#
-
C# 2022-03-27 22:40:23 c# movement script
-
C# 2022-03-27 22:15:17 Delete last modification on EntityFramework Core
-
C# 2022-03-27 22:15:02 file.create folder c#
-
C# 2022-03-27 19:25:04 Error inflating class android.support.constraint.ConstraintLayout
-
C# 2022-03-27 19:15:02 top down shooting in unity 2D
-
C# 2022-03-27 18:50:02 c# remove invalid directory characters
-
C# 2022-03-27 18:30:07 how to use K2 games Games parallax background
-
C# 2022-03-27 18:25:10 c# replace crlf
-
C# 2022-03-27 18:25:09 keycode for left mouse click unity