Hi everyone, I am looking for solution how to attach 3d object to mouse instead of cursor. It must be ping pong racket. I am new in scripting and if you give me few examples I will be grateful to you. Thank you.
1 Answer
1- If you don’t have a collider to raycast against for depth reference, but you know the distance from the camera you would like the point to be, you can use Ray : Unity - Scripting API: Ray
- The cursor can be hidden with this command (note : itdoesn’t work in Editor, build your project to see that the cursor is hidden) : Unity - Scripting API: Screen.showCursor
Here is a script that will do it :
#pragma strict
public var cursorObject : Transform;
public var distance : float = 1.5;
function Start()
{
Screen.showCursor = false;
}
function Update()
{
ObjectFollowCursor();
}
function ObjectFollowCursor()
{
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var point : Vector3 = ray.origin + (ray.direction * distance);
//Debug.Log( "World point " + point );
cursorObject.position = point;
}
Distance is distance from the camera that the object is, play with this value. You may have to scale your cursorObject alot so it doesn’t fill the screen.
Thank you so much - its a good start for me!
– ZhatThank you alucardj SO MUCH! After 2 years since I asked this question, I came back here and this code is working! After 2 years of absence I am starting to do that game... Awesome! Thank you again...
– meritant