Search Unity

Player Collision problem

Discussion in 'Scripting' started by Haxxy28, Nov 17, 2019.

  1. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    Basically I have these scripts:
    1. Player Movement:

    Code (csharp):
    1. public class Playermovement : MonoBehaviour
    2. {
    3.     public float speed;
    4.     public float rotationSpeed;
    5.    
    6.  
    7.     void Update()
    8.     {
    9.  
    10.         speed = GameObject.Find("GameManager").GetComponent<GameManager>().speed;
    11.         rotationSpeed = GameObject.Find("GameManager").GetComponent<GameManager>().rotationSpeed;
    12.  
    13.  
    14.         float translation = +speed;
    15.         float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
    16.  
    17.        
    18.         translation *= Time.deltaTime;
    19.         rotation *= Time.deltaTime;
    20.  
    21.        
    22.         transform.Translate(0, 0, translation);
    23.  
    24.        
    25.         transform.Rotate(0, rotation, 0);
    26.     }
    27. }
    2. Player Collision:

    Code (csharp):
    1.  
    2. public class PlayerCollision : MonoBehaviour
    3. {
    4.    
    5.  
    6.    
    7.    
    8.     void OnCollisionEnter(Collision playercollision)
    9.     {
    10.         if (playercollision.collider.tag == "Obstacle")
    11.         {
    12.  
    13.             Debug.Log("Object Hit!");          
    14.             FindObjectOfType<GameManager>().EndGame();
    15.  
    16.  
    17.  
    18.         }
    19.  
    20.        
    21.     }
    22. }
    3. Game Manager:

    Code (csharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     public float speed = 7f;
    4.     public float rotationSpeed = 150.0f;
    5.  
    6.     bool gameHasEnded = false;
    7.  
    8.     public float restartdelay = 2f;
    9.  
    10.    
    11.  
    12.    
    13.  
    14.     public void EndGame()
    15.     {
    16.  
    17.  
    18.  
    19.  
    20.  
    21.  
    22.         if (gameHasEnded == false)
    23.         {
    24.             gameHasEnded = true;
    25.            
    26.             speed = 0f;
    27.             rotationSpeed = 0f;
    28.             Invoke("Restart", restartdelay);
    29.  
    30.         }
    31.        
    32.     }
    33.  
    34.  
    35.  
    36.  
    37.     void Restart()
    38.     {
    39.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    40.         speed = 7f;
    41.         rotationSpeed = 150.0f;
    42. }
    43.  
    44.  
    45. }
    So, I have the player dodging randomly spawned obstacles, with an object behind to destroy obstacles that have gone past (I'll call it the "Destroyer"). I had this Destroyer parented under the player so that it would follow. I did this because It is around a spherical object and can't work out any other way to do it. This worked fine until I made it so that the game ends when the player hits an object. So now because the Destroyer is parented under the player, whenever Destroyer hits an object so it can then destroy it, the game ends. How can I get around this? Thanks for trying to help!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    You effectively made the destroyer part of the player, so when it collides, your player collides. The most reasonable solution would be to separate the two objects, simply because they are not supposed to be connected design-wise. Sadly i dont think i understand your reasoning for why they are connected in the first place (mainly because i'm not sure what kind of game we are talking about, maybe a screenshot would help), so i cant give advice on that.

    As i said above, i would consider this more of a workaround in your case, but there are ways to check which object was hit. https://gamedev.stackexchange.com/q...occuring-on-child-object-from-a-parent-script

    That all out of the way: Find(), FindObjectOfType() and so on are very slow and should never be used outside of prototyping, and most certainly never in Update(), since that calls them every frame. You never have to use these methods (they mostly exist for convenience), as there are always better ways to reference what you need. But if you do (for prototyping), consider only doing so in Start().