- Home /
kick a ball
Hello I am making a soccer game I want that the player can shoot the ball by using the left mouse button the problem is i have no script and I ask the community for help not for the whole script but for hints (the whole script is good to) I have explain what i want 1. I want how longer you hold the mouse button pushed how harder you shoot(sorry for the bad sentence) 2. i want a shoot angle between 0 and 20 degrees random
sorry that i have no script
and tank you so much for helping me
Just track the time the button is depressed then apply force to the ball based on a scaling between $$anonymous$$ and max values.
For the random lift angle: "Random.Range(0,20)". You still need to turn this into a direction vector to use with AddForce.
Answer by JustFun · Aug 17, 2014 at 11:07 AM
This script makes the ball move forward (along z-axis) and bounce after landing. It limits force, applied to ball by tMax. Force is applied only if mouse cursor points the ball.
Attach RigidBody component to the ball, Use Gravity must be checked, adjust Drag value.
using UnityEngine;
using System.Collections;
public class KickScript : MonoBehaviour {
public float bounceFactor = 0.9f; // Determines how the ball will be bouncing after landing. The value is [0..1]
public float forceFactor = 10f;
public float tMax = 5f; // Pressing time upper limit
private float kickStart; // Keeps time, when you press button
private float kickForce; // Keeps time interval between button press and release
private Vector3 prevVelocity; // Keeps rigidbody velocity, calculated in FixedUpdate()
void Update()
{
if(Input.GetMouseButtonDown(0))
{
kickStart = Time.time;
}
if(Input.GetMouseButtonUp(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if(hit.collider.name == "Ball") // Rename ball object to "Ball" in Inspector, or change name here
kickForce = Time.time - kickStart;
}
}
}
void FixedUpdate () {
if(kickForce != 0)
{
float angle = Random.Range(0,20) * Mathf.Deg2Rad;
rigidbody.AddForce(new Vector3(0.0f,
forceFactor * Mathf.Clamp(kickForce, 0.0f, tMax) * Mathf.Sin(angle),
forceFactor * Mathf.Clamp(kickForce, 0.0f, tMax) * Mathf.Cos(angle)),
ForceMode.VelocityChange);
kickForce = 0;
}
prevVelocity = rigidbody.velocity;
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Ground") // Do not forget assign tag to the field
{
rigidbody.velocity = new Vector3(prevVelocity.x,
-prevVelocity.y * Mathf.Clamp01(bounceFactor),
prevVelocity.z);
}
}
}
Does this script go on the first person controller or the ball itself?
How can I use these codes for a 2D game? I made some changes on the code. However, I got these errors:
Assets/Scripts/$$anonymous$$ickTheBall.cs(19,16): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2, UnityEngine.Force$$anonymous$$ode2D)'
Assets/Scripts/$$anonymous$$ickTheBall.cs(24,30): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.velocity'
Assets/Scripts/$$anonymous$$ickTheBall.cs(53,16): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.velocity'
Here are my codes:
using UnityEngine;
using System.Collections;
public class $$anonymous$$ickTheBall : $$anonymous$$onoBehaviour {
public float bounceFactor = 0.9f; // Deter$$anonymous$$es how the ball will be bouncing after landing. The value is [0..1]
public float forceFactor = 10f;
public float t$$anonymous$$ax = 5f; // Pressing time upper limit
private float kickStart; // $$anonymous$$eeps time, when you press button
private float kickForce; // $$anonymous$$eeps time interval between button press and release
private Vector2 prevVelocity; // $$anonymous$$eeps rigidbody velocity, calculated in FixedUpdate()
[SerializeField]
private EdgeCollider2D BatCollider;
void FixedUpdate () {
if(kickForce != 0)
{
float angle = Random.Range(0,20) * $$anonymous$$athf.Deg2Rad;
Rigidbody2D.AddForce(new Vector2(0.0f,
forceFactor * $$anonymous$$athf.Clamp(kickForce, 0.0f, t$$anonymous$$ax) * $$anonymous$$athf.Sin(angle)),
Force$$anonymous$$ode2D.Impulse);
kickForce = 0;
}
prevVelocity = Rigidbody2D.velocity;
}
void Update(){
if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
{
kickStart = Time.time;
}
if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if(hit.collider.name == "Ball") // Rename ball object to "Ball" in Inspector, or change name here
kickForce = Time.time - kickStart;
}
}
}
public void $$anonymous$$ickBall(){
BatCollider.enabled = true;
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Ground") // Do not forget assign tag to the field
{
Rigidbody2D.velocity = new Vector2(prevVelocity.x,
-prevVelocity.y * $$anonymous$$athf.Clamp01(bounceFactor));
}
}
}
Your help will be very appreciated. Thank you!
As this is an old question, it's using some depracated functionality. You'll want to make a variable for the Rigidbody2D. For example:
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
Then, replace all your current usage of Rigidbody2D (a base class, where you're currently attempting to call functions as if they were static), with rb. This includes, for example:
rb.velocity = new Vector2(prevVelocity.x, -prevVelocity.y * $$anonymous$$athf.Clamp01(bounceFactor));
Thank you so much! You are wonderful! The script now works properly.
However, I am still unable to kick the ball. I would like the user to be able to kick the ball when he presses on the "space" button. Could the problem be in the following code?
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Here are my all Update function codes:
void Update(){
if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
{
kickStart = Time.time;
}
if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if(hit.collider.name == "Ball") // Rename ball object to "Ball" in Inspector, or change name here
kickForce = Time.time - kickStart;
}
}
}
What should i change in this script to kick the ball in the x axis instead of the y axis?
Answer by ReubenXXX · Jul 24, 2017 at 12:59 AM
basically you are going to only ever have 1 ball in the soccer game so make a variable. this script goes on to the player.
//drag the ball onto the spot create by this line
public Gameobject ball;
public float power = 0;
//if mouse button is down
if(Input.GetMouseButtonDown(0))
{
power =+ Time.Deltatime
{
//if mouse is not down
if(!Input.GetMouseButtonDown(0))
{
if(power > 0)
{
kickball();
{
power = 0;
{
//when the player is in range to shoot ball
void Kickball()
{
var Vector3 angleX = this.trasform.forward;
ball.getcomponent<rigidbody>().velocity = new Vector3(angleX.x * power,angleX.y* power + Random.Range(0,20),angleX.z* power);
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to face Hovercraft physics 4 Answers
Side scroller, Scripting Bike Controls using hinge joints 0 Answers
FIshing Pole 0 Answers