- Home /
How to make a ball stay in the air longer when force is added?
I am making a game and the player is allowed to pick up and throw a ball in the direction of where the camera is facing. I have 2 scripts for this that are exactly the same except one is for the x and z axis and the other is for the y axis. I'm trying to make it so that when the player throws the ball it goes straight and not curve up and then back down. Here is my script using UnityEngine; using System.Collections;
public class throwx : MonoBehaviour {
public Rigidbody rb;
public Transform t_Camera; // Drag&Drop your camera over here from the inspector.
private Vector3 v3_Force; // Force reference vector.
public float f_Multiplier; // A multiplier value if the force wouldn't be enough.
public GameObject carryact;
public Transform ball;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Time.deltaTime == 0f)
return;
if(carryact.activeInHierarchy)
{
if (Input.GetMouseButtonDown(0))
{
v3_Force = t_Camera.forward;
ball.GetComponent<Rigidbody>().AddForce(v3_Force * f_Multiplier, ForceMode.Force);
}
}
}
}
Answer by HenBOMB · Jan 12, 2019 at 03:29 AM
You can contrain the rigidbodies axis position, so it doesn't move up and down:
I still want to use the y axis and gravity I just don't want the ball to curve down to quickly.
Then you can add this to your script:
[Range(0,9.81f)]public float ySlowdownForce = 9.81f;
private void FixedUpdate()
{
ball.GetComponent<Rigidbody>().AddForce(Vector3.up * ySlowdownForce );
}
This basically just adds the same amount of force that is pulling the object downwards (gravity), upwards. So, if your force value is at 9.81, then the object wont fall downwards, now, you can still change the force variable to make it so it doesnt fall to fast, by reducing it less than 9.81.
Thank you for your help but after trying a few times I have decided to stick with the regular gravity and just keep the curve. Is there a way to land the ball to where the screen over lay cross hair is pointing?