Search Unity

pain script

Discussion in 'Documentation' started by jordisoldevilamotta, Aug 3, 2020.

  1. jordisoldevilamotta

    jordisoldevilamotta

    Joined:
    Jul 21, 2020
    Posts:
    10
    Hello,
    I'm trying to create a script that turns the screen red if the player falls and gets hurt(similar to those games were the player receives damage if he is shotted by an enemy and the screen turns red showing that the player has received damage). However the script doesn't work at all. This is the script I'm using.
    I would like to create another with the same idea to
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PainScript : MonoBehaviour
    6. {
    7.     private float time = 1f;
    8.     private bool accident;
    9.     private int it = 12;
    10.     private byte alfa = 60;
    11.     private GameObject imagePain, camera;
    12.     private void Start()
    13.     {
    14.         if (GameObject.Find("FirstPersonPlayer").GetComponent<PlayerManager>().walkSpeed > 0.5f) GameObject.Find("FirstPersonPlayer").GetComponent<PlayerManager>().walkSpeed -= 0.5f;
    15.         if (GameObject.Find("FirstPersonPlayer").GetComponent<PlayerManager>().runSpeed > 1f) GameObject.Find("FirstPersonPlayer").GetComponent<PlayerManager>().runSpeed -= 1f;
    16.  
    17.         imagePain = GameObject.Find("PainImage");
    18.         camera = GameObject.Find("PlayerCamera");
    19.         Vector3 vec = camera.transform.position;
    20.         vec.y = vec.y - 0.6f;
    21.         camera.transform.position = vec;
    22.         imagePain.GetComponent<UnityEngine.UI.Image>().color = new Color32(254, 0, 0, alfa);
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         if (it == 0)
    28.         {
    29.             Destroy(gameObject);
    30.             Destroy(this);
    31.         } else
    32.         {
    33.             --it;
    34.             alfa -= 5;
    35.             imagePain.GetComponent<UnityEngine.UI.Image>().color = new Color32(254, 0, 0, alfa);
    36.             Vector3 vec = camera.transform.position;
    37.             vec.y = vec.y + 0.05f;
    38.             camera.transform.position = vec;
    39.         }
    40.     }
    41. }
    Interact