Hi i am trying to get my character to not move while aiming and throwing but so far unsuccesful i there a way to stop this from happening?
public class ThrowStinkbomb : MonoBehaviour
{
public Animator animator; // Reference to the character’s Animator component.
public string throwAnimationTrigger = “Throw”; // The name of the trigger parameter in the Animator for the throwing animation.
public GameObject objectToThrow; // Reference to the object to throw.
public float throwForce = 10f; // The force applied to the thrown object.
public float movementSpeed = 5f; // Character’s movement speed.
private bool isAiming = false; // Flag to track if the character is aiming.
void Update()
{
// Check for right mouse button press to initiate aiming.
if (Input.GetMouseButtonDown(1) && !isAiming)
{
StartAiming();
}
// Check for right mouse button release to throw when aiming.
if (Input.GetMouseButtonUp(1) && isAiming)
{
Throw();
}
}
void StartAiming()
{
// Set the boolean parameter in the Animator to true to trigger the aiming animation.
animator.SetBool("Aim", true);
isAiming = true; // Set the flag to indicate the character is aiming.
// Disable character's movement during aiming.
animator.SetFloat("Speed", 0f); // Assuming "Speed" is a parameter controlling movement in your Animator.
}
void Throw()
{
// Trigger the throw animation using the trigger parameter.
animator.SetTrigger(throwAnimationTrigger);
// Stop aiming by setting the boolean parameter back to false.
animator.SetBool("Aim", false);
isAiming = false;
// Enable character's movement after throwing.
animator.SetFloat("Speed", 1f); // Set to 1 to enable movement, adjust if needed.
}
// Animation event called at the point in the animation where the object should be thrown.
public void ThrowObjectEvent()
{
// Instantiate the object at the character's position.
GameObject thrownObject = Instantiate(objectToThrow, transform.position, transform.rotation);
// Get the rigidbody of the thrown object.
Rigidbody rb = thrownObject.GetComponent<Rigidbody>();
if (rb != null)
{
// Apply force to the thrown object.
rb.AddForce(transform.forward * throwForce, ForceMode.VelocityChange);
}
}
}