I would like to instantiate something on the mouse down position, to show clicks from an AI, otherwise, it gets boring, CPU wouldn’t look human.
Something like a ripple effect or a small spark.
When I instantiate something on Mouse Down, it gets the position of the clicked object and instantiate the effect at the pivot, not at the mouse (or touch) position…
Any suggestions?
It could also be in the middle of the object as well.
C#
You can use Raycast to get a point. Object to get the point to set position of your Object instance need to have a Collider, and rigidbody to better physic operations, set it kinematic if you dont need for some else thing.
public Camera yourCam; // Reference of your Camera
public Ray ray;
public RaycastHit hit;
public LayerMask yourLayer; // On Inspector, you can set wich layers detects the ray if you not set nothing, ray not detect nothing
private void Update()
{
ray = yourCam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, yourLayer))
{
Debug.DrawLine(yourCam.transform.position, hit.point, Color.green); // If your ray detect any collider, a green line to the impact point is showed
} else
{
Debug.DrawLine(yourCam.transform.position, ray.GetPoint(10), Color.red); // If your ray dont detect nothing, a red line in direction to the ray is showing
}
}