UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"throw a baseball ball"
using UnityEngine; public class ThrowBaseball : MonoBehaviour { public GameObject baseballPrefab; public float throwForce = 20f; // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { ThrowBall(); } } void ThrowBall() { // Instantiate the baseball at the player's position GameObject baseball = Instantiate(baseballPrefab, transform.position, Quaternion.identity); // Add a Rigidbody component to the baseball Rigidbody rb = baseball.AddComponent<Rigidbody>(); // Add force to the baseball in the forward direction of the player rb.AddForce(transform.forward * throwForce, ForceMode.Impulse); } }