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

Adding force to rigidbody if it collides with obstacle [image example] [Help]

Discussion in 'Scripting' started by i3artyy2222, Oct 17, 2015.

  1. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hi all,
    I want to make an obstacle in my game that moves from left to right but i want to make it like if it collides with 'ball' then kick the ball in direction the object was moving i will attach picture to explain it better
    hel.PNG
    Im using animation to move the obstacle [hammer] from left to right in swing motion and im wondering how can i made the ball get kicked if it collides with this obstacle i tried changing mass of hammer but it didnt do well both hammer and ball has rigidbody
    i wrote something like this but i couldnt find on google anything how to add force as i want
    Code (JavaScript):
    1.  
    2. var power : int = 500;
    3.  
    4. function OnCollisionEnter (hit : Collision)
    5. {
    6. if(hit.gameObject.tag == "Ball")
    7. {
    8. Debug.Log("BallHit");
    9. hit.rigidbody.AddForce(transform.forward * power);
    10. }
    11.  
    12. }
     
  2. Wowo51

    Wowo51

    Joined:
    Oct 12, 2015
    Posts:
    25
    I'm not entirely sure but you might be able to make a Physic Material asset and set the bounciness to do what you want. It worked for me in a somewhat similar situation.
     
  3. Skyous

    Skyous

    Joined:
    Oct 18, 2015
    Posts:
    4
    I'm not sure if this is entirely what you're looking for or that I'm just confused on everything, but maybe making a collider separate to the Hammer that 'isTrigger' and you use that to knockback the ball the way you want? I hope this helps or I'm just being a weirdo xD