Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Apply force to rigidbody from RayCast

Discussion in 'Scripting' started by boby-bobs, Mar 25, 2016.

  1. boby-bobs

    boby-bobs

    Joined:
    Oct 18, 2013
    Posts:
    53
    I have a game, let's just call it soccer.

    There is a player object with a controller, and this object is emitting a raycast basically around the foot area. There is a ball that is a rigidbody with a meshcollider. I can easily detect that my raycast is hitting the ball, but I want to be able to "kick" the ball when I press space.

    So of course, I have space tied to the action of kicking the ball (this method is of course in some Ball.cs attached to the ball). I have no issue using ApplyForce to the ball and use some hard coded values, but I want to apply direction based on the raycast hit. I can easily pass data to the Ball.cs, I am just not sure what data the ball should receive.

    What vector can I send to the ball so that when I call ApplyForce on it's rigidbody, it moves in the direction that a ray was hitting it?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why do you need to pass anything to the ball? just access the gameobject's rigidbody using GetComponent<>() and apply the force to it from whatever is kicking.

    Raycasthit contains the "normal" of the impact point. That's the perpendicular vector from the surface of the collider that was hit, so you might want to apply the force in the direction of "-normal" (i.e. perpendicular but towards the impact point).
     
  3. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    if you doing something like passing a ball or shooting a goal I would recommend favoring simplicity over accuracy.

    when the gamer wants to pass it to a team member, they shouldn't have to worry if they point the joystick in the exact direction. even more importantly the ball passing shouldn't rely on the kicker's animation being perfect.

    instead of doing something as accurate as exactly where the kicker contacts the ball and that it applies forces directly through its center of mass (all which depends on the animations also being perfect), just apply force in the intended direction that player wants to pass it. it simplifies the work you have to do and it makes the kicks more reliable, predictable, and less annoying for the player. it may not be as accurate, but you usually don't need so much to sell it to the gamers.
     
  4. boby-bobs

    boby-bobs

    Joined:
    Oct 18, 2013
    Posts:
    53
    Thanks, exactly what I needed to hear. Forgot about normals.