- Home /
2D Ball Kicking, similar to "Sports Heads", or "1on1 Soccer"
Hey there,
I'm looking to create a simple 1v1 football game where 2 players simply run into the ball to try and get it into the opposite net. When the player runs into the ball, it should react as if you had just kicked it, bouncing up into the air. I have a small scene setup, with my two players, but whenever I attempt to kick the ball it rolls along the floor, not heading into the air at all.
I am not sure how to go about it, I've tried using RigidBody2D.AddForce to no avail (I may not be using it correctly) I've also tried adding a bouncy physics2D material to the ball, to make it retaliate in some way, but that hasn't worked either.
Here's what I want it to look like: (Skip to 1:10 for gameplay, and btw the video isn't by me.)
So any idea's on how I can make the ball react in this way? Am I missing something blatantly obvious, that's usually the case haha.
Anyway thanks for any help, I'll post my script so you can have a look at what's going on, thanks!
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float maxSpeed = 4;
public float jumpForce = 558;
public float kickStrength = 10;
public Transform groundCheck;
public LayerMask whatIsGround;
[HideInInspector]
public bool lookingRight = true;
private Rigidbody2D rb2d;
private bool isGrounded = false;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
if(Input.GetButtonDown("Jump") && isGrounded)
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0f, jumpForce));
}
void FixedUpdate()
{
float hor = Input.GetAxis ("Horizontal");
rb2d.velocity = new Vector2 (hor * maxSpeed, rb2d.velocity.y);
isGrounded = Physics2D.OverlapCircle (groundCheck.position, 0.15F, whatIsGround);
if ((hor > 0 && !lookingRight) || (hor < 0 && lookingRight))
Flip ();
}
public void Flip()
{
lookingRight = !lookingRight;
Vector3 myScale = transform.localScale;
myScale.x *= -1;
transform.localScale = myScale;
}
void OnColliderEnter2D(Collider2D coll)
{
if (coll.transform.tag == "Ball")
coll.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (kickStrength, 100));
}
}
Your answer
Follow this Question
Related Questions
2D swinging physics. 0 Answers
Paper Ball Physics Implementation 0 Answers
2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers
[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers
ex2d with physics ? 1 Answer