Declare a Rigidbody at the beginning of the script:
float distToGround;Create a boolean function to check if the Game Object is on the floor:
bool IsOnTheGround() { return (Physics.Raycast(transform.position, - Vector3.up, distToGround + 0.1F));}We will make the Game Object to jump and then control the Physic velocity of the object in the air to make it look more realistic. First the jump:
rb.AddForce(Vector3.up.normalized * jumpingForce, ForceMode.Impulse);We can limit the Physic speed of the Game Object in the air to make it look more realistic. For that, we have to find out first whether the object is on the ground or in the air, and act accordingly. Note that we are using the boolean fuction IsOnTheGround() that we created in the previous tip. We create this snippet in FixedUpdate():
if (IsOnTheGround ()) { rb.AddForce (movement.normalized * speedOnGround);} else { //If player is on the air, we limit velocity to a max of 10.0F rb.velocity = Vector3.ClampMagnitude(rb.velocity, 10.0F); }Let's explain with an example: If any dynamic object touches (collides) wit an certain type of object (bouncer), the dynamic object will be violently pushed away from the bouncer object. I create a 'Bouncer' object and I drag this script into the bouncer object Inspector:
using UnityEngine;using System.Collections;public class Bouncer : MonoBehaviour { public float bouncingRadius = 0.0F; public float power = 2000.0F; public float upwardsModifier = 0.0F; public float maxBouncingVelocity = 100F; Vector3 pushPos; void Start() { pushPos = transform.position; } void OnCollisionEnter(Collision other) { if (other.gameObject.tag == "Player") { other.rigidbody.AddExplosionForce (power, pushPos, bouncingRadius, upwardsModifier); } if (other.gameObject.tag == "Enemy") { other.rigidbody.AddExplosionForce (power, pushPos, bouncingRadius, upwardsModifier); } }}Here we explain how to make a ball follow you by adding forces to the ball movement in 8 different directions depending of the angle relative between player position and the ball position. We create this script in the ball Inspector:
using UnityEngine;using System.Collections;public class MoveTo : MonoBehaviour { public float speed; GameObject player; Rigidbody rb; float horizontalAngle; void Start () { speed = 10; player = GameObject.Find ("Player"); rb = GetComponent<Rigidbody> (); } void FixedUpdate() { LocatePlayer (); Pursuit (); } void LocatePlayer() { //Get the angle relative between player position and the enemy position Vector3 delta = player.transform.position - transform.position; Quaternion look = Quaternion.LookRotation(delta); //float vertical = look.eulerAngles.x; horizontalAngle = look.eulerAngles.y; //Debug.Log (horizontalAngle); } void Pursuit() { // We apply a particular force depending of the angle relative between player position and the enemy position if (horizontalAngle >= 337.5 && horizontalAngle <= 360) rb.AddForce (Vector3.forward.normalized * speed); else if (horizontalAngle >= 0 && horizontalAngle < 22.5) rb.AddForce (Vector3.forward.normalized * speed); else if (horizontalAngle >= 22.5 && horizontalAngle < 67.5) rb.AddForce (new Vector3(1, 0, 1) * speed); else if (horizontalAngle >= 67.5 && horizontalAngle < 112.5) rb.AddForce (Vector3.right.normalized * speed); else if (horizontalAngle >= 112.5 && horizontalAngle < 157.5) rb.AddForce (new Vector3(1, 0, -1) * speed); else if (horizontalAngle >= 157.5 && horizontalAngle < 202.5) rb.AddForce (Vector3.back.normalized * speed); else if (horizontalAngle >= 202.5 && horizontalAngle < 247.5) rb.AddForce (new Vector3(-1, 0, -1) * speed); else if (horizontalAngle >= 247.5 && horizontalAngle < 292.5) rb.AddForce (Vector3.left.normalized * speed); else if (horizontalAngle >= 292.5 && horizontalAngle < 337.5) rb.AddForce (new Vector3(-1, 0, 1) * speed); }}