top of page

C# Scripts:

-UNITY-

​

ANIMATED TEXT:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class AnimatedText : MonoBehaviour
{
    //The time for each letter to appear - The lower it is, the faster letters appear
    public float letterPauseRate = 0.5f;
    //Message that will be displayed
    public string messageText;
    //Text for the message to display
    public Text assignedText;    

    void Start()
    {
        //Get text component
        assignedText = GetComponent<Text>();
        //messageText will display assignedText
        messageText = assignedText.text;
        //Sets the assignedText to be blank
        assignedText.text = "";
        //Calls the function
        StartCoroutine(TypeText());
    }

    IEnumerator TypeText()
    {
        //Split each char into a char array
        foreach (char letter in messageText.ToCharArray())
        {
            //Add 1 letter each time
            assignedText.text += letter;
            yield return 0;
            // Waits for seconds assigned in letterPauseRate (eg. 0.5f)
            yield return new WaitForSeconds(letterPauseRate);
        }
    }
}

​

Animated Text.gif

​

Activating sounds on button hovers and clicks:

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ButtonHover : MonoBehaviour, ISelectHandler , IPointerEnterHandler
{
    // This script gives the game more flavour by adding in audio sounds when mouse hovers over buttons and presses buttons

    public AudioClip buttonhover;
    public AudioClip buttonPress;
    AudioSource audioSource;

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        // When mouse hovers over button play sound
        audioSource.clip = buttonhover;
        audioSource.Play();

    }

    public void OnSelect(BaseEventData eventData)
    {
        // On mouse click on button play sound
        audioSource.clip = buttonPress;
        audioSource.Play();
    }
}

​

BUTTON HOVER/ CLICK:

CAMERA SHAKE:

Shakes the camera - set your own duration and shake amount

using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour
{
    // Transform of the camera to shake. Grabs the gameObject's transform
    // if null.
    public Transform camTransform;

    // How long the object should shake for.
    public float shakeDuration = 0f;

    // Amplitude of the shake. A larger value shakes the camera harder.
    public float shakeAmount = 0.7f;
    public float decreaseFactor = 1.0f;

    public bool shaketrue = false;

    Vector3 originalPos;
    float originalShakeDuration;

    void Awake()
    {
        if (camTransform == null)
        {
            camTransform = GetComponent(typeof(Transform)) as Transform;
        }
    }

    void OnEnable()
    {
        originalPos = camTransform.localPosition;
        originalShakeDuration = shakeDuration;
    }

    public void Update()
    {
        if(shaketrue)
        {
            if (shakeDuration > 0)
            {
                camTransform.localPosition = Vector3.Lerp(camTransform.localPosition, originalPos + Random.insideUnitSphere * shakeAmount, Time.deltaTime*3);

                shakeDuration -= Time.deltaTime * decreaseFactor;
            }
            else
            {
                shakeDuration = originalShakeDuration;
                camTransform.localPosition = originalPos;
                shaketrue = false;
            }
        }
        
    }

    public void shakecamera()
    {
        shaketrue = true;
    }
}

CameraShake.gif
Contact me
  • Twitter Social Icon
  • YouTube Social  Icon

Twitter: @stamp_lauren  |   Youtube: DaCookieMonster

© 2019 by Lauren Stamp

bottom of page