- Home /
my lives system is buggy
so i made a unity lives counter so that when the player gets hit buy the enemy bullet he decrease a live but sometimes it works and often doesnt sometimes it reloads the level and sometimes it counts as 2 bullets hit in the same time my player have a rigidbody and 2 capsule collider one isTriggered and one is not triggered here is my cowboy(player) script :
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Cowboy : MonoBehaviour {
public float walkSpeed = 2.5f;
public float jumpHeight = 5f;
public Transform groundCheckTransform;
public float groundCheckRadius = 0.2f;
public Transform targetTransform;
public LayerMask mouseAimMask;
public LayerMask groundMask;
public GameObject bulletPrefab;
public Transform muzzleTransform;
public AnimationCurve recoilCurve;
public float recoilDuration = 0.25f;
public float recoilMaxRotation = 45f;
public Transform rightLowerArm;
public Transform rightHand;
public float Magazines = 5f;
public int lives;
private float inputMovement;
private Animator animator;
private Rigidbody rbody;
private bool isGrounded;
private Camera mainCamera;
private float recoilTimer;
private bool resetGame;
private string collisionObject;
private int FacingSign
{
get
{
Vector3 perp = Vector3.Cross(transform.forward, Vector3.forward);
float dir = Vector3.Dot(perp, transform.up);
return dir > 0f ? -1 : dir < 0f ? 1 : 0;
}
}
void Start()
{
animator = GetComponent<Animator>();
rbody = GetComponent<Rigidbody>();
mainCamera = Camera.main;
lives = 3;
}
[Obsolete]
void Update()
{
if(lives < 1)
{
Application.LoadLevel(Application.loadedLevel);
}
//gets Z input
inputMovement = Input.GetAxis("Horizontal");
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, mouseAimMask))
{
targetTransform.position = hit.point;
}
// gets Space input
if (Input.GetButtonDown("Jump") && isGrounded)
{
rbody.velocity = new Vector3(rbody.velocity.x, 0, 0);
rbody.AddForce(Vector3.up * Mathf.Sqrt(jumpHeight * -1 * Physics.gravity.y), ForceMode.VelocityChange);
}
// gets mouse input
if (Input.GetButtonDown("Fire1"))
{
if (Magazines > 0){
Fire();
}
}
}
private void Fire()
{
//Fire a bullet
recoilTimer = Time.time;
var go = Instantiate(bulletPrefab);
go.transform.position = muzzleTransform.position;
var bullet = go.GetComponent<Bullet>();
bullet.Fire(go.transform.position, muzzleTransform.eulerAngles, gameObject.layer);
Magazines--;
}
private void LateUpdate()
{
// Recoil Animation
if (recoilTimer < 0)
{
return;
}
float curveTime = (Time.time - recoilTimer) / recoilDuration;
if (curveTime > 1f)
{
recoilTimer = -1;
}
else
{
rightLowerArm.Rotate(Vector3.forward, recoilCurve.Evaluate(curveTime) * recoilMaxRotation, Space.Self);
}
}
private void FixedUpdate()
{
// Movement
rbody.velocity = new Vector3(inputMovement * walkSpeed, rbody.velocity.y, 0);
animator.SetFloat("speed", (FacingSign * rbody.velocity.x) / walkSpeed);
// Facing Rotation
rbody.MoveRotation(Quaternion.Euler(new Vector3(0, 90 * Mathf.Sign(targetTransform.position.x - transform.position.x), 0)));
// Ground Check
isGrounded = Physics.CheckSphere(groundCheckTransform.position, groundCheckRadius, groundMask, QueryTriggerInteraction.Ignore);
animator.SetBool("isGrounded", isGrounded);
}
private void OnAnimatorIK()
{
// Weapon Aim at Target IK
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
animator.SetIKPosition(AvatarIKGoal.RightHand, targetTransform.position);
// Look at target IK
animator.SetLookAtWeight(1);
animator.SetLookAtPosition(targetTransform.position);
}
[Obsolete]
void OnTriggerEnter(Collider other)
{
// Get Bullet magazine
if (other.gameObject.layer == 8)
{
other.gameObject.SetActive(false);
Magazines += 5;
}
//Die and respawn when fall out of world
if(other.gameObject.layer == 9)
{
Application.LoadLevel(Application.loadedLevel);
}
if(other.gameObject.layer == 11)
{
lives--;
}
}
}
and here is my Bullet script :
using System; using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; using UnityEngine;
public class Bullet : MonoBehaviour { public float velocity = 20f; public float life = 1f;
private int firedByLayer;
private float lifeTimer;
void Update()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, velocity * Time.deltaTime, ~(1<< firedByLayer)))
{
transform.position = hit.point;
Vector3 reflected = Vector3.Reflect(transform.forward, hit.normal);
Vector3 direction = transform.forward;
Vector3 vop = Vector3.ProjectOnPlane(reflected, Vector3.forward);
transform.forward = vop;
transform.rotation = Quaternion.LookRotation(vop, Vector3.forward);
Hit(transform.position, direction, reflected, hit.collider);
}
else
{
transform.Translate(Vector3.forward * velocity * Time.deltaTime);
}
if (Time.time > lifeTimer + life)
{
Destroy(gameObject);
}
}
private void Hit(Vector3 position, Vector3 direction, Vector3 reflected, Collider collider)
{
// Do something here with the object that was hit (collider), e.g. collider.gameObject
Destroy(gameObject);
}
public void Fire(Vector3 position, Vector3 euler, int layer)
{
lifeTimer = Time.time;
transform.position = position;
transform.eulerAngles = euler;
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
Vector3 vop = Vector3.ProjectOnPlane(transform.forward, Vector3.forward);
transform.forward = vop;
transform.rotation = Quaternion.LookRotation(vop, Vector3.forward);
}
void OnTriggerEnter(Collider other)
{
// Get Bullet magazine
if (other.gameObject.layer == 7)
{
Destroy(gameObject);
}
}
}
i dont know what to do :(
Your answer
![](https://koobas.hobune.stream/wayback/20220613034857im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Finding a certain variable is failing 0 Answers
Player invisible on respawn 2 Answers
Crouch jump bug. 1 Answer
Bug. Player can't move. 3 Answers
photon player movement 1 Answer