This is my first time posting on a forum of these sorts so please forgive me if I mess up anywhere.
I’m currently trying to make a simple game just to learn some Unity and I’m having issues with a particular thing. It’s a 2D topdown game, a football game, where the player (a circle) can kick a ball into a goal. If anyone has played the browser game Haxball that’s pretty much it.
My problem is I don’t really know how to make the player kick the ball when a particular key is pressed. What I have right now is a collision system that allows me to run into the ball and push it around which is what I want, but I would like to be able to kick it on command therefore pushing it with more force.
I’ve seen plenty of tutorials and read other forum posts and what I have currently is a separate collider around the player that acts as a trigger and runs the script which kicks the ball, exactly how I want it. The problem is that said collider is always active meaning I have no control of when the ball is kicked.
So my goal is making it so the collider only activates when I press for example the “E” key.
This is my “Kick” script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Kick : MonoBehaviour
{
public float thrust;
void Start()
{
}
void Update()
{
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Ball"))
{
Rigidbody2D Ball = other.GetComponent<Rigidbody2D>();
if(Ball != null)
{
Vector2 difference = Ball.transform.position - transform.position;
difference = difference.normalized * thrust;
Ball.AddForce(difference, ForceMode2D.Impulse);
}
}
}
}
Any help would be much appreciated, and if there’s something I need to provide in order to better explain my issue please say so!
Thank you.