Search Unity

Rolling a Ball

Discussion in 'Editor & General Support' started by VoidHavoc, Sep 22, 2014.

  1. VoidHavoc

    VoidHavoc

    Joined:
    Jun 25, 2014
    Posts:
    6
    Hey guys, I'm new to Unity and programming in general, but I have a good understanding how things work and how they fit together. I had some previous programming experience and played around a lot with Map Editors, but never have I delved this deep into programming. So far I have made 2 functional games in C# with the help of Unity tutorials found all over the internet. However I'm having a hard time making a ball roll in certain directions, whether it's being pushed, bumps into something and or rolling down hill. I don't want to add a player controller script to it and make it roll by pressing buttons, I want it to react to it's surroundings. How can I go about this? Can anyone assist me with this please? I would greatly appreciate it. Thanks in advance!! :)
     
  2. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    perhaps add a Rigidbody > make sure Gravity is enabled and it should just roll everywhere to the weakest point... Such as down hill.

    Guessing that's what you wanted? Elaboration a bit more would help.
     
  3. VoidHavoc

    VoidHavoc

    Joined:
    Jun 25, 2014
    Posts:
    6
    Thanks for the reply, that seemed to have worked, last time I had a script attached to it which made it function weird. I have no idea why I tried adding a script to the object to make it move, but in my mind I thought it needed to be moved with forces or collisions such as in the tutorial "Roll-a-Ball" found on Unity. But now the thing is, how do I make the ball react to impact, such as being fired on from one direction sending the ball flying/moving in the opposite direction.
     
  4. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    You're welcome.

    Don't quote me on this but I believe you will need scripts for that one, at least to mess with how it reacts to impact.. Now you might could try adding Bouciness to a Physics Material, not sure if the ball needs it or the thing it impacts. However, I know it can bounce from dropping, so I don't see why it couldn't at least bounce off an object after hitting it with force.

    However again, you may need scripts to make it work a bit difference.

    Something like (Untested) just something I'm jotting down in C#.
    I'm not sure if the code is correct, I don't usually mess with Physics too much.
    But if it's not correct it will at least give you an idea of where to go with it.

    But I believe it's something to do with rigidbody.
    (TO TEST THIS) and work upon it.

    Where ever it is the ball is supposed to collide put a Cube with Mesh Renderer disabled
    and make sure it's Collision Box is marked Is Trigger and attach the script below to it.

    I do know the Function is properly made in C#
    however the "col.rigidbody.AddForce(Vector3.right * 10)" part I'm not sure about.

    void OnTriggerEnter(Collider col){

    if(col.gameObject.tag == "Player"){
    col.
    rigidbody.AddForce(Vector3.right * 10);
    }

    }



    Or I just thought about it try this as well Instead of Trigger just do OnCollision

    so

    void OnCollisionEnter(Collision col){
    if(col.gameObject.tag == "Player"){
    col.rigidbody.AddForce(Vector3.right * 10);
    }

    }


    Note if the Vector3.right isn't good enough look up basic Vector movements and stuff, you could declare a new Vector3 and adjust the 3 axis (0,0,0) with Example (25,40,0)
    X,Y,Z Axis... Y in Unity is Up and Down, don't let that confuse you!
     
    Last edited: Sep 22, 2014