The functionality of my Player GameObject in my first 3D game
Published: 2023-11-28 04:13:41

- in the script, I used [Input.GetAxisRaw()](https://docs.unity3d.com/ScriptReference/Input.html) method to make the Player object move around and jump.

**Note:** I used Rigidbody's AddForce() method to make the object jump. Set the ForceMode to "Impulse" to make the jump instant with a small amount of force, and the Unity Built-in [OnCollisionEnter(Collision collision)](https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html) method to make sure the player object jumps only when it's back on the ground, and I used a condition to specify the very first touch of the player to the ground is the one I'm looking for.

```
// condition to check if the player has touched the ground
    private void OnCollisionEnter(Collision collision)
    {
        if(collision.GetContact(0).normal == Vector3.up)
        {
            isGrounded = true;
        }
    }
```

**Note:** GetContact(0).normal
"Contact" is when 2 collider objects hit each other. And if they have weird shapes and touch each other from a few spots, then we need to specify which contact we need to work on. That's why we are passing 0 to the GetContact() function to specify that we need the first contact. "normal" means we are getting the normal direction of the contact, which is basically the direction that the object we are hitting is facing. (if we hit the ground, then the ground is facing up, so the normal direction is up).

- To make the game object faces the direction of its movement, we set the [transform.forward](https://docs.unity3d.com/ScriptReference/Transform-forward.html) to rigidbody.velocity's x and z. (set the y to 0 to prevent the object facing up or down).
- When the Player object hits the enemies we want to restart the current scene and start over (Game Over). To do so, we use [SceneManager](https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html).LoadScene() method, which accepts the index of the scene we want it to load. However, since we don't know which scene at the time the player is going to be on, we just pass the index of the Scene in the Unity  Build Settings that "GetActiveScene().buildIndex" returns to us.

```
// restarts the current scene
    public void GameOver()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
```
To help the Enemy and Bonfire objects know if the Player object overlaps them, I just added a tag to the Player object. Read about them [here](https://dev.to/atenajoon/crafting-my-first-3d-game-in-unity-4521-temp-slug-26410?preview=f6084ccc7b60ad9a8ce22e3e3b9974f309840d242920c3023f7ece4948abba1fee43f48228005be5325f0a7e0fec50b4d84301176d8f95621b0cdcb2).
- The character is immune and blinks for 1.5 seconds after hitting a bonfire. I made a script named "BlinkGameObject" and added it to the HealthText and the Player objects. In the Bonfire.sc I check the conditions and call the Start and Stop the blink from the new script.

**Note:** To Hide and Show the Player object (Blink) once it touches the Bonfires I just set its local scale to 0 and brought it to 1 for 1.5 seconds via the InvokeRepeating() method.

```
public void CallStartBlinkGameObject()
    {
        InvokeRepeating("StartBlinkGameObject", 0f , blinkRepeatTime);   
    }

    void StartBlinkGameObject()
    {
        if(targetBlinkObject.transform.localScale == new Vector3(1, 1, 1)) {
            targetBlinkObject.transform.localScale = new Vector3(0, 0, 0);
        } else targetBlinkObject.transform.localScale = new Vector3(1, 1, 1);
    }
    public void StopBlinkGameObject()
    {
        CancelInvoke();
        targetBlinkObject.transform.localScale = new Vector3(1, 1, 1);
    }
```