Instantiate onMouseDown
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#
Answer by unidad2pete · Sep 04, 2017 at 12:31 AM
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
}
}
Your answer
Follow this Question
Related Questions
Instantiate object (on touch position) by touch on the collider of other object 1 Answer
Instantiate at touch.position 0 Answers
OnMouseOver not work when instantiating camera 1 Answer
Can You help me Fix This? (Infinite level Generator) 0 Answers
dragging multiple game objects simultaneously (mobile) 0 Answers