- Home /
Raycasting as a selection tool
Hi, I am new to unity and not great with scripting. Essentially what I am trying to do is use a raycast coming out of a gun to select a cube and float it like a tractor beam. I can do this with mouse over commands but will not be able to in the actual game so I am trying to set it to work off of a ray, only problem is i dont really understand rays to well yet. i set one on my gun object before but was unable to get it to collide with any object.
Any help or insight would be great here is my code so far.
// By clicking on a GameObject, you can move it around or make it "float" // Attach script to gameobject you want to float
//Color change for Debug/Feedback purposes only var mouseOverColor = Color.blue; private var originalColor : Color; function Start () { originalColor = renderer.sharedMaterial.color; }
//Change to new color when mouse moves over GameObject function OnMouseEnter () { renderer.material.color = mouseOverColor; }
//Change color to original when mouse moves off GameObject function OnMouseExit () { renderer.material.color = originalColor; }
function OnMouseDown () { var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
while (Input.GetMouseButton(0)) { var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z); var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset; transform.position = curPosition; //coroutine yield; } }
please always selecte your code and press the code button in the editor.
Answer by Ashkan_gc · May 10, 2010 at 03:23 AM
the screentoworld method needs the distance of the object from the camera to work correctly. screenSpace.z is not the distance of your gameobject from the camera so you need a script like this for dragging.
function OnMouseDown()
{
while (Input.GetMouseButton(0))
{
temp = Input.mousePosition;
temp.z = -mainCamera.transform.position.z;
myTransform.position = mainCamera.ScreenToWorldPoint(temp);
yield;
}
}
}
use temporary variables for performance reasons. try to define your vars on top of your script instead of in update or ... as you see i use the distance between camera and the enemy to drag it. the method needs this distance.
about rays. you can shoot rays in your world to see who this ray will collide too. then you can get the exact point and the name of the object that the ray is collided too. rays can have limited/infinite lengths and they are verry flexible but they are heavy operations so use them just when you need them. be careful when you are on a device like the iphone. for example you can use ScreenPointToRay method of camera class to shoot a ray from mouse position and see who is under the mouse instead of using onMouseDown. but unity do that itself once per frame and calls your OnMouseDown functions so you don't need that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612223254im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Having to select an object multiple times to continue 1 Answer
GUI button vanishes when I release mouse button 1 Answer
Is it possible to detect if a object is occluded without using raycasting? 2 Answers
What Is The Best AI Solution For A Restaurant Style Strategy Game? 0 Answers
how can i instantiate a GameObject in mouse position and in a special distance from the camera? 2 Answers