I have a small test scene with a single model prefab and a single script applied to that prefab. In my scene the model picks up and throws a ball.
No matter how I modify AddForce the ball never throws forward. Here is my code:
GameObject[] ballsInGame;
public Transform ballIAmHolding;
Rigidbody ballBody;
float throwPower = 50f;
void Start () {
ballsInGame = GameObject.FindGameObjectsWithTag("pickupable");
}
void Update () {
//pickup and throw
if (Input.GetKeyDown("f")) {
anim.SetBool("pickupBool", true);
} else { anim.SetBool("pickupBool", false); }
if (ballIAmHolding == null) {
anim.SetBool("throwBool", false);
foreach (GameObject ball in ballsInGame) {
float distDiff = Vector3.Distance(transform.position, ball.transform.position);
if (distDiff < pickupDistance && Input.GetKeyDown("f")) {
ballIAmHolding = ball.transform;
}
}
}
else {
ballIAmHolding.transform.position = rightHandBone.transform.position;
anim.SetBool("pickupBool", false);
if (Input.GetKey("c")) {
Debug.Log("C DOWN");
anim.SetBool("cockBool", true);
}
if (Input.GetKeyUp("c")) {
Debug.Log("C UP");
anim.SetBool("cockBool", false);
anim.SetBool("throwBool", true);
ballBody = ballIAmHolding.GetComponent();
ballBody.AddForce(transform.forward * throwPower);
ballIAmHolding = null;
}
}
}
ballBody.AddForce(transform.forward * throwPower); should throw the ball forward from the player model that the script is attached to. The game throws the ball down every time.
Thanks!!
↧