- Home /
Question by
RealEfrain12 · Jul 27, 2021 at 01:45 PM ·
animation3dfpsweaponchanging
How to reset gameObject position when switched back to?
I have a weapon-changing system that works fine. The only problem I have is that whenever I try switching back and forth, my player fist (one of the weapons) is freezing mid animation. I have tried turning the animator off and on but that isn't working. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Punching : MonoBehaviour
{
public Animator playerAnimator;
public Transform fpsCam;
public float range = 100f;
public Vector3 pushPosition;
public float knockBack = -650f;
public float UltimateKnockBack = -5000f;
private int randomNumber;
public bool canPunch = false;
public float currentStamina;
public float maxStamina;
public Stamina stamina;
public bool continueBarrage = false;
public Transform leftArm;
public Transform rightArm;
void Start()
{
Animator playerAnimator = GetComponent<Animator>();
currentStamina = maxStamina;
stamina.SetStamina(currentStamina);
}
void OnEnable()
{
canPunch = false;
playerAnimator.SetBool("Barraging", false);
playerAnimator.SetBool("LeftArm", false);
playerAnimator.SetBool("RightArm", false);
}
public void FixedUpdate()
{
if (currentStamina <= 0)
return;
if (canPunch)
return;
if (Input.GetKeyDown(KeyCode.Z) && canPunch == false && currentStamina > 0f)
{
UltimatePunch();
DecreaseStamina(35f);
randomNumber = Random.Range(0, 2);
if (randomNumber == 0)
{
playerAnimator.SetBool("LeftArm", true);
return;
}
else if (randomNumber == 1)
{
playerAnimator.SetBool("RightArm", true);
return;
}
}
if (currentStamina <= 100f)
{
currentStamina += Time.fixedDeltaTime;
stamina.SetStamina(currentStamina);
}
if (Input.GetMouseButton(0) && canPunch == false && currentStamina > 0f)
{
Punch();
DecreaseStamina(5f);
randomNumber = Random.Range(0, 2);
if (randomNumber == 0)
playerAnimator.SetBool("LeftArm", true);
else if (randomNumber == 1)
playerAnimator.SetBool("RightArm", true);
StartCoroutine(DelayPunching());
}
if (Input.GetKeyDown(KeyCode.Q) && currentStamina > 0f && continueBarrage == false)
{
playerAnimator.SetBool("Barraging", true);
continueBarrage = true;
DecreaseStamina(1f);
InvokeRepeating("Punch", 0, 0);
}
else if (Input.GetKeyUp(KeyCode.Q) || currentStamina < 0f || continueBarrage == true)
{
continueBarrage = false;
playerAnimator.SetBool("Barraging", false);
CancelInvoke("Punch");
}
}
IEnumerator DelayPunching()
{
canPunch = true;
yield return new WaitForSeconds(0.1f);
playerAnimator.SetBool("LeftArm", false);
playerAnimator.SetBool("RightArm", false);
canPunch = false;
}
void Punch()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
if (hit.collider.CompareTag("Zombie") || hit.collider.CompareTag("Player"))
{
Transform enemy = hit.transform.GetComponent<Transform>();
Rigidbody enemyRb = hit.transform.GetComponent<Rigidbody>();
if (enemy != null)
{
pushPosition = transform.position - enemy.transform.position;
enemyRb.AddForce(pushPosition.normalized * knockBack);
}
}
}
}
void UltimatePunch()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
if (hit.collider.CompareTag("Zombie") || hit.collider.CompareTag("Player"))
{
Transform enemy = hit.transform.GetComponent<Transform>();
Rigidbody enemyRb = hit.transform.GetComponent<Rigidbody>();
if (enemy != null)
{
pushPosition = transform.position - enemy.transform.position;
enemyRb.AddForce(pushPosition.normalized * UltimateKnockBack);
}
}
}
}
public void DecreaseStamina(float staminaDecreaseRate)
{
currentStamina -= staminaDecreaseRate;
stamina.SetStamina(currentStamina);
}
}
Comment