hello everyone, i am not sure how understandable is the title of this question. anywhay ill do my best to explain a little better. i want to instantiate an object the way the diagram below sort of represents:
i want to know what is the Vector3 at which the object spawns, i know how to get the normal of the hit and the position at which it hit, but what i do not know is how to get the vector3 where the object would be spawned, and that is what i dont know how to get.
i want to let you know i do not want you guys to write any code for me other than maybe some exaple, but if possible to explain how would you do this.
In this C# Example, I attach the script to the main Camera. For testing, I placed some cubes in random positions somewhere in front of the camera. Each time you click on a cube in the scene, this will instantiate your assigned prefab halfway between your Camera and the object you’re clicking on.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
Ray ray;
RaycastHit hit;
public GameObject prefab;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown (0))
{
GameObject clone = Instantiate(prefab, Vector3.Lerp(transform.position, hit.transform.position, 0.5F), hit.transform.rotation) as GameObject;
}
}
}