Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by cidmodder · Feb 24, 2012 at 04:17 PM · javascripterrorraycasts

Raycast problems

Hi all. I am having trouble getting a raycast to go from a ship to the ground when I hold the mouse button down. Here is the script I have so far

 var horizontalSpeed = 2.0;
 var verticalSpeed = 2.0;
 var particle : GameObject;
 var myTransform : Transform;
 
 
 function Update ()
 {
     // Get the mouse delta. This is not in the range -1...1
 
     var h = horizontalSpeed * Input.GetAxis ("Mouse X");
     var v = verticalSpeed * Input.GetAxis ("Mouse Y");
 
     transform.Translate (v, 0, -h);
 
 
 if(Input.GetMouseButton(0)){
 //construct a ray from the mouse point
 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 if ( Physics.Raycast(myTransform.position, ray)){
 
 Instantiate (particle, transform.position, transform.rotation);
 }
 //horizontalSpeed=0;
 //verticalSpeed=0;
 
 }
 }


right now I get an error that says Assets/UFO.js(20,21): BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.Ray)' was found. but I'm not sure where the arguments aren't right. how can I get the raycast to go from the ship to the ground? Thanks!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Berenger · Feb 24, 2012 at 04:27 PM

Raycast have many overload, but basically it needs 1- Origine 2- Direction (and some option, not relevant here. You can either give a ray, which contains those two informations, or the origine and the direction (both Vector3). Try :

 if( Physics.Raycast(ray) )

or

 if( Physics.Raycast(ray.origin, ray.direction) )
Comment
Add comment · Show 6 · 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
avatar image cidmodder · Feb 24, 2012 at 04:35 PM 0
Share

how could I make the direction the mouse position?

avatar image ZweiD · Feb 24, 2012 at 04:53 PM 0
Share

http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToWorldPoint.html

you have to calculate the distance from the camera to your ground (i am assu$$anonymous$$g you want to point on a ground).

avatar image cidmodder · Feb 24, 2012 at 05:10 PM 0
Share

yes I do. is there anyway to send a messege through this raycast to what you hit? I'm trying to do Send$$anonymous$$essege and ray.Raycasthit.Send$$anonymous$$essege but neither of those are working.

avatar image Berenger · Feb 24, 2012 at 05:34 PM 0
Share

Unless the camera never moves, ScreenToWorldPoint will require a raycast to know the distance from the ground anyway.

To click on an object and send him a message, you have two ways.

  • The unity built-in way : Attach a script on those objects you want to clic, and add the function On$$anonymous$$ouseUp() (or Down), and do what needs to be done.

  • Your own way : Raycast can give you the collider hit by the ray. Collider is a component, and thus can call Send$$anonymous$$essage. Proceed like that (I am not familiar with UnityScript, there might be mistakes) :

var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if( Physics.Raycast(ray, hit) ) hit.collider.Send$$anonymous$$essage( "Hi, how are you ?" ); // Don't send that message :p

avatar image cidmodder · Feb 24, 2012 at 05:39 PM 0
Share

I realised the problem is actually that the ray it is drawing is going way off into space no matter where I click it always goes to the same point and it isn't even in the cameras clipping plane. Any idea why this might be?

Show more comments
avatar image
0

Answer by Imankit · Sep 27, 2012 at 01:03 PM

if ( Physics.Raycast(myTransform.position,-Vector3.up, ray)){ .....

}

Ushould specify the direction in which you want to cast the ray....

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My Javascript is not opening 0 Answers

Trouble converting player controller from JS to C# 1 Answer

Change gameobject sprite with button 1 Answer

Error Message Coordinates 1 Answer

Javascript Snakegame Error Cannot convert ....Gameobject to .....Transform 1 Answer


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