- Home /
Crossing Soccer ball system Help
Hello, I'm doing a crossing system for a soccer game, and i managed to cross a ball to a target. The current code ads a curve to the ball, but it only works if the crossSpeed variable is 5, and if the player y axis is at a certain height, if i change the player y axis i need to adjust the drag and angular drag of the ball for it to work correctly. I took this formula from this unity question: https://answers.unity.com/questions/384515/addforce-to-go-a-specific-distance-and-height.html?sort=votes vWSpeed, are supposed to be the offset for the ball to go to the left, for it to then get curve from the AddCurveToBall function. Can anyone help make this code work for multiple cross speeds?
private void FixedUpdate()
{
//if we are crossing the ball add curve to it
if(addCurveToBall)
AddCurveToBall();
}
//this will cross the ball
public void StartCrossBall()
{
//get the direction from the ball to the player
Vector3 dir = playerHead.transform.position - transform.position;
//normalize the direction
dir.Normalize();
//balistic trajectory
float g = Physics.gravity.magnitude; // get the gravity value
float vSpeed = Mathf.Sqrt(2 * g * playerHead.transform.position.y + 2f); // calculate the vertical speed
//balistic trajectory
float gW = Physics.gravity.magnitude; // i use the gravity value for the curve
float vWSpeed = Mathf.Sqrt(2 * g * playerHead.transform.position.y + 2f); // calculate the curve speed
float totalTime = 2 * crossSpeed / g; // calculate the total time
float hSpeed = Vector3.Distance(transform.position, playerHead.transform.position) / totalTime; // calculate the horizontal speed
rb.velocity = dir * hSpeed;
rb.velocity += new Vector3(0, vSpeed + yOffset, vWSpeed + xOffset); // launch the projectile!
//rb.velocity += new Vector3(hSpeed, vSpeed, dir.z + 0.5f); // launch the projectile!
addCurveToBall = true;
}
void AddCurveToBall()
{
//rb.AddForce(Physics.gravity * rb.mass);
rb.AddForce(new Vector3(0,0,Physics.gravity.y) * rb.mass);
}
private void OnCollisionEnter(Collision collision)
{
addCurveToBall = false;
ResetRigidBodyPhysics();
}
Hey is this the whole script cause the StartCrossBall
function is not called anywhere ? Please give the whole script is possible.
Your answer
Follow this Question
Related Questions
How do I make my character controller jump when I use this code? 0 Answers
Delaying Physics Calculations (Friction) 1 Answer
Unity 5 workaround for concave colliders 3 Answers
How do I make car wheels spin and slow down when the car is stopped 0 Answers
Unity 3D - Rotating an object in relation to its velocity 0 Answers