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
0
Question by blackmethod · Sep 19, 2011 at 02:23 AM · physicsraycastgravity

Raycasting problem

I was altering this script I got from another post on unity forums, but now upon editing it so that it raycasts from the mouse position instead of forward I get the errors:

ArgumentException: get_main can only be called from the main thread.

Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. Gravitygun..ctor () (at Assets/Gravitygun.js:12)

And

UnityEngine.Camera:get_main() Gravitygun:.ctor() (at Assets\Gravitygun.js:12)

Here is the script

     var catchRange = 30.0;
 
 var holdDistance = 4.0;
 
 var minForce = 1000;
 
 var maxForce = 10000;
 
 var forceChargePerSec = 3000;
 
 var layerMask : LayerMask = -1;
 
 
 
 enum GravityGunState { Free, Catch, Occupied, Charge, Release};
 
 private var gravityGunState : GravityGunState = 0;
 
 private var rigid : Rigidbody = null;
 
 private var currentForce = minForce;
 
 private var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
 
 
 function FixedUpdate () {
 
     if(gravityGunState == GravityGunState.Free) {
 
         if(Input.GetButton("Fire1")) {
 
             var hit : RaycastHit;
 
             if (Physics.Raycast (ray, 100)) {
 
                 if(hit.rigidbody) {
 
                     rigid = hit.rigidbody;
 
                     gravityGunState = GravityGunState.Catch;
 
                     
 
                     // for debuging, remove it
 
                     print("force: " + currentForce);
 
                 }
 
             }
 
         }
 
     }
 
     else if(gravityGunState == GravityGunState.Catch) {
 
         rigid.MovePosition(transform.position + transform.forward * holdDistance);
 
         if(!Input.GetButton("Fire1"))
 
             gravityGunState = GravityGunState.Occupied;
 
     }
 
     else if(gravityGunState == GravityGunState.Occupied) {
 
         rigid.MovePosition(transform.position + transform.forward * holdDistance);
 
         if(Input.GetButton("Fire1"))
 
             gravityGunState = GravityGunState.Charge;
 
     }
 
     else if(gravityGunState == GravityGunState.Charge) {
 
         rigid.MovePosition(transform.position + transform.forward * holdDistance);
 
         if(currentForce < maxForce) {
 
             currentForce += forceChargePerSec * Time.deltaTime;
 
         }
 
         else {
 
             currentForce = maxForce;
 
         }
 
         if(!Input.GetButton("Fire1"))
 
             gravityGunState = GravityGunState.Release;
 
             
 
         // for debuging, remove it
 
         print("force: " + currentForce);
 
     }
 
     else if(gravityGunState == GravityGunState.Release) {
 
         rigid.AddForce(transform.forward * currentForce);
 
         currentForce = minForce;
 
         gravityGunState = GravityGunState.Free;
 
         
 
         // for debuging, remove it
 
         print("");
 
     }
 
 }

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 timberlandboots · Sep 21, 2011 at 11:52 AM 0
Share

====( http://www.clothes6.us )=====

online store wholesale sneakers,Jerseys, jewelry, glasses, shirt, sports,handbags,clothes ,news, vogue,jeryse at ugg boots,luxury fashion ysl women boots, christian louboutin boots,ed hardy clothes, jordan shoes,nike shoes,hoodies,t-shirts,nfl jerseys,mlb jerseys,nhl jerseys,coach handbags,handbags,wholesale, retail, sunglasses,belts, caps, ed hardy caps,suit,fashion good,newest style goods All the products are free shipping,

====( http://www.clothes6.us )=====

free shipping

competitive price

any size available

accept the paypal

jordan shoes $32

nike shox $32

$$anonymous$$BT shoes 48

NFL jersys 24

NBA jersys 24

Timberland boots 45

Christan Audigier bikini $20

Ed Hardy Bikini $23

Smful short_t-shirt_woman $14

ed hardy short_tank_woman $15

Sandal $26

christian louboutin $60

Sunglass $14

COACH_Necklace $18

handbag $33

AF tank woman $16

====( http://www.clothes6.us )=====

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Clunk · Sep 19, 2011 at 06:03 AM

This sounds kinda jank, but I have done it, and it works nicely. I used it for a moving crosshair... Create a gameobject that moves with the mouse position. Put the raycast on that object.

Comment
Add comment · Show 3 · 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 blackmethod · Sep 20, 2011 at 03:30 AM 1
Share

I'll try that when I'm actually awake, thanks clunk

avatar image syclamoth · Sep 21, 2011 at 11:32 AM 0
Share

Or alternatively you could just get the mouse position using Input? I don't see why a gameObject is required here. Besides, a gameObject occupies a single point in 3d space, where a ray more accurately reflects what a point on the screen transforms into in the game world.

avatar image Clunk · Sep 21, 2011 at 05:18 PM 0
Share

Ye, that's y I attach a raycast to the gameobject lol. It would be better to control the pixelInset.x and pixelInset.y based on mousePosition, though :)

avatar image
0

Answer by Peter G · Sep 19, 2011 at 02:47 AM

I can see how the error is a little intimidating for a beginner, but it should be pointing at this line:

 private var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

Unity doesn't want you calling camera.ScreenPointToRay() from the field initializer and that makes sense strictly from a logical point. You don't want the ray to point at the spot where the mouse was the moment the object was created. That doesn't make a lot of sense for a gun.

You should probably move it to the FixedUpdate where it belongs. Delete that line and modify your FixedUpdate() to be more like this

 function FixedUpdate () {
 
     if(gravityGunState == GravityGunState.Free) {
 
        if(Input.GetButton("Fire1")) {
             var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             var hit : RaycastHit;
             ///.........
             //The rest of your code.
Comment
Add comment · Show 1 · 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 blackmethod · Sep 19, 2011 at 04:05 AM 0
Share

Thanks, error gone, now I just gotta figure out how to get it to actually grab it

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

false gravity on a cube planet 6 Answers

Calculate where an object is going to land 0 Answers

[C#] Raycast based physics and clipping 0 Answers

Applying Normal force from ground to player 1 Answer

changing guiTexture color with raycast 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