Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by St3pup4 · Jun 11, 2021 at 12:43 PM · playerbugbulletshitboxistrigger

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 :(

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character Controller Component, Gravity/inertia, and Root Motion 0 Answers

Release Unity Player as open-source? 1 Answer

Possible Unity BUG? Or am i missing something 1 Answer

4.6.0 to 4.6.1 makes player lag, bug? 1 Answer

,Unity 2020 Player mesh bug 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges