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

Realistic Soccer Ball Dribbling?

Discussion in 'Scripting' started by Der_Kevin, May 11, 2016.

  1. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    Hey Folks!
    iam currently working on a soccer game and the dribbling looks like this at the moment:

    and this script on the ball:
    Code (CSharp):
    1. void OnTriggerEnter(Collider collision)
    2.       {
    3.          
    4.           ControllingFeet = collision.transform;
    5.        }
    6.  
    7.   void FixedUpdate()
    8.       {
    9.          
    10.  
    11.           bool isControlled = ControllingFeet != null;
    12.  
    13.           if (isControlled)
    14.           {
    15.          Vector3 targetPos = ControllingFeet.transform.position;
    16.              GetComponent<Rigidbody>().MovePosition(targetPos);
    17.           }
    18.       }
    which actually just mean that, when the ball enters the trigger (which is in front of the character) it transfers the position of the collider (ControllingFeet) to the Ball.

    i tried to copy the transform position of the Feet to the collider which is controlling then the ball like this:

    Code (CSharp):
    1. using UnityEngine;
    2.   using System.Collections;
    3.  
    4.   public class Copyposition : MonoBehaviour
    5.   {
    6.       public Transform itsTarget;
    7.  
    8.       void Update ()
    9.       {
    10.           transform.position = new Vector3(itsTarget.position.x, 0 ,itsTarget.position.z);
    11.       }
    12.   }
    but that looks stupid - and not natural at all as you can see in the second half of the gif.

    after that i tried to delay the movement of the ControllingFeet like this:
    Code (CSharp):
    1.  transform.position = new Vector3(itsTarget.position.x+0.8f, 0 ,itsTarget.position.z);
    and it comes a tiny bit closer to what i want to achieve. but only works on the X axis ( move to the right) and looks like this:

    i am kinda stuck how to apply the "delay" for all directions. how could i do this?

    or is somebody here having another idea how i can make the ball movement more natural?
     
    Hickna likes this.
  2. Keith90

    Keith90

    Joined:
    Sep 27, 2013
    Posts:
    116
    What if you set the rotation of the ball to that of the feet? That way when you turn, the ball constantly turns to the front of the feet?

    Also, to get the ball to move with the player, you could do a few things.

    1) Don't parent the ball to the feet. Move the ball ahead by so much based on the angle and speed of the player that touches it. So when a player encounters it, the player pushes the ball forward. That way if you constantly move in one direction the ball will move in that way, but if you angle to turn and move, the ball will head in that direction too.

    2) Parent the ball to the feet. When the player that has it moves forward, don't move the position till you detect the player colliders with it, then move the ball ahead accordingly. You may need to lerp from the position of collision to the distance you want in front of the player.
     
    Last edited: May 11, 2016
  3. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    Since you mentioned "Realistic" I would argue that you want to have physics actually control the ball movement.
    This can be achieved by having the feet have a collider and a rigidbody, and have the ball be a rigid body as well.

    You'll need to add forces, and probably find the proper settings for all bodies involved, but using this method you will get a much closer approximation of what actually happens when you dribble the ball.
     
    Flaring-Afro likes this.
  4. youngswagsrocks

    youngswagsrocks

    Joined:
    Jun 1, 2017
    Posts:
    7


    Please am working something similar. How would the player kick the ball, receive a pass or maybe chest a high pass from another player