Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Jarrett · May 10, 2010 at 01:55 AM · raycastingselection

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; } }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ashkan_gc · May 10, 2010 at 03:12 AM 0
Share

please always selecte your code and press the code button in the editor.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges