How Click and Move in Unity part 2

Using Mouse input and raycast to get a position in world space

Player Script Pseudocode

Player Script Actual Code

if (Input.GetMouseButtonDown(0))
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.Log("Ray: "+ray);
if (Physics.Raycast(ray, out hit))
Debug.Log("Raycast hit something");
Debug.Log("hit world pos: "+ hit.point);
GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
capsule.transform.position = hit.point;

Player Script Result

Next Up



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.