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 MakeMeASandwich · Sep 14, 2014 at 08:40 PM · physicsraycastingraycasthit

Raycast not applying force to point

The force is not being applied when I click the mouse and I can not figure out why.

 // Sets the guns object
 var gunActive : boolean;
 var gunInHand : boolean;
 var gunHoldPostion : Transform;
 var inGunPickupZone : boolean;
 
 var gunEnd : Transform;
 var gunModel : GameObject; 
 var gunEndPost : Vector3;
 var gunEndDirection : Vector3;
 var zoomedIn : boolean;
 
 var gunLine : boolean;
 
 var sparksObject : GameObject;
 var sparks : Transform;
 
 var pokeForce : int;
 var rayColor : Color;
 
 var zoomCam : Camera;
 var camSmooth : float;
 var zoomCamPost : Transform;
 var zoomCamPostDefault : Transform;
 
 function Start () 
 {
     sparksObject.SetActive(false);
     zoomedIn = false;
     
 }
 function OnTriggerEnter (col : Collider)
 {
     if(col.gameObject.tag == "Player")
     {
         inGunPickupZone = true;
         Debug.Log("In Zone!");
     }
 }
 function FixedUpdate ()
 {
 
     gunEndPost = gunEnd.position;
 
     if(Input.GetButtonUp("Action Key") && inGunPickupZone.Equals(true))
     {
         gunActive = true;
     }
     else if(Input.GetButtonUp("Action Key"))
     {
         Debug.Log("Not in Pick-Up Zone");
     }
     
 }
 
 function Update () 
 {
      if(gunActive.Equals(true))
      {
          gunModel.transform.position = gunHoldPostion.position;
          gunInHand = true;
          gunActive = false;
      }
      if(gunInHand.Equals(true))
      {
           gunModel.transform.position = gunHoldPostion.position;
      }
       
       // Fires red line when Priamry is active
          if(gunLine.Equals(true))
          {
               var hit2 : RaycastHit;
               var ray2 = new Ray (gunEndPost, gunEndDirection);
               var forward2 : Vector3 = gunEnd.TransformDirection(Vector3.forward) * 100;
               Debug.DrawRay(gunEndPost,forward2,Color.red,.01f,true);
                if(Physics.Raycast(ray2, hit2))
               {
                       Debug.Log("Red Line");
               }
          }
     
             // For Primary
       
       if(Input.GetMouseButtonDown(0))
       {
          var hit : RaycastHit;
          var ray = new Ray (gunEndPost, gunEndDirection);
          var forward : Vector3 = gunEnd.TransformDirection(Vector3.forward) * 100;
          Debug.Log("Raycast was fired");
          Debug.DrawRay(gunEndPost,forward,Color.green,.25f,true);
              
              if(Physics.Raycast(ray, hit))
              {
                  Debug.Log("Right Before force it applied");
                  
                  if(hit.rigidbody != null)
                  {
                      sparksObject.SetActive(true);
                      hit.rigidbody.AddForceAtPosition(ray.direction * pokeForce, hit.point);
                      // creates a partical effect
                      var particleClone = Instantiate(sparks, hit.point, Quaternion.LookRotation(hit.normal));
                      Destroy(particleClone.gameObject, 2);
                      Debug.Log("Force was Applied");
                  }
              }
          
          }
Comment
Add comment · Show 3
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 rutter · Sep 14, 2014 at 08:47 PM 0
Share

Have you checked your console window? Do you see any warnings or errors? Are your debug messages all showing up appropriately? Is AddForceAtPosition even being called? Help us to help you.

avatar image MakeMeASandwich · Sep 15, 2014 at 01:14 AM 0
Share

It calls the green ray and that is it, it never reaches the "if(Physics.Raycast)"

  if(Input.Get$$anonymous$$ouseButtonDown(0))
           {
             var hit : RaycastHit;
             var ray = new Ray (gunEndPost, gunEndDirection);
             var forward : Vector3 = gunEnd.TransformDirection(Vector3.forward) * 100;
             Debug.Log("Raycast was fired");
             Debug.DrawRay(gunEndPost,forward,Color.green,.25f,true);

(Green Ray)

avatar image rutter · Sep 15, 2014 at 01:27 AM 0
Share

That indicates your raycast is returning false (meaning it hasn't hit anything).

I do notice one thing that might make this tricky to troubleshoot...

On lines 86-92, the data you send to Debug.DrawRay differs from the data you send to Physics.Raycast. In particular, the debug ray travels in direction forward, while the physics ray travels in direction gunEndDirection.

If those two directions don't agree, you might get unintuitive results.

1 Reply

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

Answer by MakeMeASandwich · Sep 15, 2014 at 09:54 PM

Finally fixed, forgot to make the gunholdposition(direction for gun) the direction.

(Fixed code)

  if(Input.GetMouseButtonDown(0) && gunInHand.Equals(true))
       {
          var hit : RaycastHit;
          var ray = new Ray (gunEndPost, gunHoldPostion.forward);
          var forward : Vector3 = gunEnd.TransformDirection(Vector3.forward) * 100;
          Debug.Log("Raycast was fired");
          Debug.DrawRay(gunEndPost,forward,Color.green,.25f,true);
              
              if(Physics.Raycast(ray, hit))
              {
                  Debug.Log("Right Before force it applied");
                  
                  if(hit.rigidbody != null)
                  {
                      sparksObject.SetActive(true);
                      hit.rigidbody.AddForceAtPosition(ray.direction * pokeForce, hit.point);
                      // creates a partical effect
                      var particleClone = Instantiate(sparks, hit.point, Quaternion.LookRotation(hit.normal));
                      Destroy(particleClone.gameObject, 2);
                      Debug.Log("Force was Applied");
                  }
              }
          
          }

Thanks you rutter for helping! Much appreciated!

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 rutter · Sep 15, 2014 at 10:00 PM 0
Share

Glad to see you got it working. :) I've posted your response as an answer and marked it as "accepted".

Feel free to repost if you run into more problems.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can the resultant velocity of an object after a potential collision be predicted? 1 Answer

issues with raycasts 0 Answers

Using Ray cast for click to move 2 Answers

Having a RaycastHit event create a component? 1 Answer

Raycasting not behaving as expected? 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