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 Grealish · Dec 04, 2014 at 01:52 PM · raycastraycasting

Pushing away rigidbody from the direction of raycast hit

So I'm working on a game where the characters are on top of a rigidbody and they push each other away using a fire key. I have a raycast so that I can determine which direction the enemy should be pushed away. So far the raycast detects but when I hit the fire button it returns "MissingMethodException: UnityEngine.Rigidbody.AddForceAtPoint". Also if it helps the object being hit by the raycast isn't the rigidbody itself but the game object on top of it.

 var ball:Rigidbody;
 var bulletForce: int = 100;
 
 function Update () {
 var hit:RaycastHit;
   var fwd = transform.forward;
   Debug.DrawRay(transform.position,fwd ,Color.red);
   if (Physics.Raycast (transform.position, fwd, hit)) {
       print ("There is something in front of the object!");
       if(hit.collider.tag == "Enemy2"){
            print(hit.point); //For testing the hit point 
            if(Input.GetButton("Fire1")){
                 print("Shit works!");
                 ball.AddForceAtPoint(bulletForce * fwd,hit.point);
            }
       }
   }
   else{
   print ("Nothing there!");
   }
  }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MolochSim · Dec 04, 2014 at 02:21 PM

You don't seem to be assigning a rigidbody anywhere in your code. The hit contains a GameObject it hit, so simply use a GetComponent() on that object. Otherwise the code seems alright, but I would recommend first checking for button press and then creating rays and using raycasts, otherwire it is just raycasting pointlesly at every update. Hope this helps.:)

EDIT: I write in C# and if it is not wastly different, the rigidbody component has to be assigned to the variable, not merely declaring the variable as a Rigidbody. Also, there is an addForceAtPosition (not addForceAtPoint) method in the Rigidbody component.

Comment
Add comment · Show 2 · 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 Grealish · Dec 05, 2014 at 05:53 PM 0
Share

The first line assigns a rigidbody to the ball, I also need a constant check because the raycast is also going to check distance between the player and the character so having it on a button click wouldn't work.

avatar image MolochSim · Dec 06, 2014 at 02:06 AM 0
Share

The method you are trying to use does not exist. I updated my answer.

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

Raycast not detecting a hit from far away, it only detects when close. 0 Answers

How can I get the tag of a child object? 1 Answer

Fixing Editor Mouse Offset Due To Local Raycast? 1 Answer

Help with Raycast C# 0 Answers

Raycast exit point of collider 0 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