Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 rodrigodebem7 · Jun 24, 2019 at 11:32 AM · rigidbodyraycastfpsboxcollider

FPS rigidbody problem

Hi guys

I'm creating a FPS and I have the following issue:

When I shoot at the characters, sometimes the hit is not recognized even if the player is in front of them.

They have a box collider and a rigidbody attached to them.

I have this script attached to the player:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DisparaArma : MonoBehaviour
 {
     private GerenciaArma gerenciaArma;
 
     public float nDisparo = 15f;
     private float TempoProximoDisparo;
     public float damage = 20f;
 
     private Animator ZoomCameraIn;
     private bool zoomed;
 
     private Camera Maincamera;
     private GameObject mira;
     // Start is called before the first frame update
     void Start()
     {
         gerenciaArma = GetComponent<GerenciaArma>();
         ZoomCameraIn = transform.Find(Tags.LOOK_ROOT).transform.Find(Tags.ZOOM_CAMERA).GetComponent<Animator>();
 
         mira = GameObject.FindWithTag(Tags.MIRA);
         Maincamera = Camera.main;
     }
 
     // Update is called once per frame
     void Update()
     {
         Atira();
         ZoomInAndOut();
     }
 
     void Atira()
     {
         if (Input.GetMouseButtonDown(0))
         {
             if(gerenciaArma.SelecionaArma().tipoBala == WeaponBulletType.BULLET)
             {
                 gerenciaArma.SelecionaArma().AnimacaoTiro();
                 DisparaBala();
                 
                 
             }
             
         }
     }
 
     void ZoomInAndOut()
     {
         if (gerenciaArma.SelecionaArma().mira_tipo == TipoMira.AIM)
         {
             if (Input.GetMouseButtonDown(1))
             {
                 ZoomCameraIn.Play(Animacoes.ZOOM_IN_ANIM);
                // gerenciaArma.SelecionaArma().Aim(true);
                 mira.SetActive(false);
                 print("VaiZoom");
             }
 
             if (Input.GetMouseButtonUp(1))//se soltar o botão do mouse
             {
                 ZoomCameraIn.Play(Animacoes.ZOOM_OUT_ANIM);
                 //gerenciaArma.SelecionaArma().Aim(false);
                 mira.SetActive(true);
             }
         }
     }
 
     void DisparaBala()
     {
         RaycastHit hit;
         if(Physics.Raycast(Maincamera.transform.position, Maincamera.transform.forward, out hit))
         {
             
             if (hit.transform.tag == Tags.ENEMY_TAG)
             {
                 hit.transform.GetComponent<ScriptVida>().DanoAplicado(damage);
             }
         }
     }
 }
 

and this one is attached to the enemies:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class ScriptVida : MonoBehaviour
 {
     private IndioAnimações indio_Anim;
     private NavMeshAgent navAgent;
     private IndioController indio_Controller;
 
     public float vida = 100f;
     public bool is_Player, is_Indio, is_Onca;
 
     private bool morto;
     // Start is called before the first frame update
     void Awake()
     {
         if (is_Onca || is_Indio)
         {
             indio_Anim = GetComponent<IndioAnimações>();
             indio_Controller = GetComponent<IndioController>();
             navAgent = GetComponent<NavMeshAgent>();
         }
 
         if (is_Player)
         {
 
         }
     }
 
     public void DanoAplicado(float damage)
     {
         if (morto)
             return;
         vida -= damage;
 
 
         if (is_Player)//mostra a barra de vida do jogador sendo decrementada
         {
 
         }
 
         if (is_Onca || is_Indio)
         {
             if (indio_Controller.EnemyState == EnemyState.PATROL)
             {
                 indio_Controller.chase_Distance = 50f;
             }
         }
 
         if (vida <= 0)
         {
             JogadorMorre();
             morto = true;
             print(vida);
         }
 
     }
 
     void JogadorMorre()
     {
         if (is_Indio)//se o jogador mata o indio
         {
             GetComponent<Animator>().enabled = false;
             GetComponent<BoxCollider>().isTrigger = false;
             GetComponent<Rigidbody>().AddTorque(-transform.forward * 50f);
 
             indio_Controller.enabled = false;
             navAgent.enabled = false;
             indio_Anim.enabled = false;
         }
 
         if (is_Onca)// se o jogador mata a onça
         {
             GetComponent<Animator>().enabled = false;
             GetComponent<BoxCollider>().isTrigger = false;
             GetComponent<Rigidbody>().AddTorque(-transform.forward * 50f);
 
             indio_Controller.enabled = false;
             navAgent.enabled = false;
             indio_Anim.enabled = false;
         }
 
         if (is_Player)
         {
             GameObject[] enemies = GameObject.FindGameObjectsWithTag(Tags.ENEMY_TAG);
             for (int i = 0; i < enemies.Length; i++)
             {
                 enemies[i].GetComponent<IndioController>().enabled = false;
             }
 
             GetComponent<Movimentação>().enabled = false;
             GetComponent<DisparaArma>().enabled = false;
             GetComponent<GerenciaArma>().SelecionaArma().gameObject.SetActive(false);
         }
 
         if (tag == Tags.PLAYER_TAG)//caso o jogador morra, o jogo é reiniciado
         {
             Invoke("RestartGame", 3f);
         }
 
         else
         {
             Invoke("TurnOffGameObject", 3f);
         }
     }
 
     void RestartGame()//função para reiniciar o jogo caso o jogador morra
     {
         UnityEngine.SceneManagement.SceneManager.LoadScene("Gameplay");
     }
 
     void TurnOffGameObject()
     {
         gameObject.SetActive(false);
     }
 
 }

What could I do to solve this problem?

link text

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

210 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 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

Drag Rigid Bodies First Person Controller.js 0 Answers

Is there a way for a non-kinematic rigid body to be hit by a raycast?,Is there a way for a non kinematic rigidbody to be be hit by a raycast? 1 Answer

Long-distance physics 0 Answers

Moving forward relative to camera with atypical WASD script, cinemachine, rigidbody and boxcollider 0 Answers

Rigidbody.position causes shaking 1 Answer


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