smallarrows.GIF

156 syllabus      site index      3D artists      past student work        DMA

Unity Scripts

Rotate an object around the Y axis
Select an object in the Hierarchy
Select the Add Component button
     Scroll down to New script
          Type a name: rotate_object
          Double click the C# script in your Assets folder

Replace entire script with this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rotate_object : MonoBehaviour {
 void Update () {
        //this.transform.Rotate(x,y,z)
        this.transform.Rotate(0, Time.deltaTime*40, 0);
    }
}




Stretch a Ball along the X and Z axis in inverse relation to Y axis animation
This includes some work arounds since scaling is a bit limited in Unity.
Because you can not really separate x,y,z very easily, especially if animated.
Make a sphere with an extra node or parent in Maya.
Animate the sphere compressing the Y axis when it hits the ground using the parent level.
Add this script named squish to the child in Unity.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class squish : MonoBehaviour
{
    float baseScale, b, currentScale, c;
    void Start()
    {
        Vector3 baseScale = transform.localScale;
        b = baseScale.x; //x,y,z the same
    }
    void Update()
    {
        Vector3 currentScale = transform.parent.localScale;
        c = currentScale.y;
        transform.localScale = new Vector3(b/c, b, b/c);
    }
}




Start an Animation with a mouse click
Select the animated object in the Hierarchy
Select the Add Component button
     Scroll down to New script
          Type a name: mouse_start_anim
          Double click the C# script in your Assets folder

Replace entire script with this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mouse_start_anim : MonoBehaviour {
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
            GetComponent<Animation>().Play();
    }
}



Randomize the time and speed for multiple animations.
Written by Alex Rickett UCLA Game Lab

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomlyPerturbAnimation : MonoBehaviour
{
    public float speedRandomizationAmount = .05f;
    IEnumerator Start()
    {
        yield return null;
        yield return null;
        yield return null;
        Animator animator = this.GetComponent<Animator>();
        if (animator != null)
        {
            animator.speed = Random.Range(1 - speedRandomizationAmount, 1 + speedRandomizationAmount);
            animator.Play(animator.GetAnimatorTransitionInfo(0).nameHash, 0, Random.Range(0f, 1f));
           
        }

        Animation animation = this.GetComponent<Animation>();
        if (animation != null)
        {

            animation[animation.clip.name].speed = Random.Range(1 - speedRandomizationAmount, 1 + speedRandomizationAmount);
            animation[animation.clip.name].normalizedTime = Random.Range(0f, 1f);
        }
        //yield return new WaitForSeconds(0, .2f);
    }

}



Disable Teleport Hint on Controllers for VR
Select Teleporting in the Hierarchy.
     Scroll down to New script
          Type the name:  CancelTeleportHintScript
          Double click the new C# script in your Assets folder

Replace entire script with this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;

public class CancelTeleportHintScript : MonoBehaviour
{
    // Update is called once per frame
    void Start()
    {
        Teleport.instance.CancelTeleportHint();
    }
}



Hide the mouse cursor & add an Escape button
Select the Main Camera or an object
Select the Add Component button in the Inspector
     Scroll down to New script
          Type a name: hide_cursor_escape
          Double click the new C# script in your Assets folder

Replace entire script with this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class hide_cursor_escape : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        Cursor.visible = false;
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
}




Sound when an object Collides
Select an object with colliders
Select
Add Component->Audio Source
     Drag an drop a sound clip from the Assets tab onto the slot for AudioClip

Select the object in the Hierarchy
Select the Add Component button
     Scroll down to New script
          Type the name: ImpactSound
          Double click the C# script in your Assets folder

Replace entire script with this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ImpactSound : MonoBehaviour
{
    AudioSource source;
    void Start()
    {
        source = GetComponent<AudioSource>();
        source.volume = 0.0f;
    }

    private void OnCollisionEnter(Collision collision)
    {
        source.volume = Random.Range(0.5f, 1f);
        source.Play();
    }
}










OLD
Script to help the SteamVR simulator work
This helps throwing objects and starting animation when you do not have a headset.
Written by Alex Rickett
Download this file   It is in your Downloads folder.
     PC right mouse over the file, select Extract All...
Place the file in your Assets folder
Drag and drop this script onto the Player->NoSteamVRFallbackObjects->FallbackHand in your Hierarchy  (SteamVR Player)



WebGL script to add videoPlayer to an object
This is a work around for WebGL, the Unity videoPlayer does not get published correctly with WebGL.

Select an object, add this script to the object
Create a StreamingAssets folder in Assets, put video in this folder
Add the video clip name in the Inspector

using UnityEngine;
using System.IO;
using UnityEngine.Video;


public class video_on_object : MonoBehaviour
{
    private UnityEngine.Video.VideoPlayer videoPlayer;
    public string videofile;
    private GameObject this_object_cam;

    void Start()
    {
        GameObject cam = this.gameObject;
        videoPlayer = cam.AddComponent<UnityEngine.Video.VideoPlayer>();

        // Obtain the location of the video clip.
        videoPlayer.url = Path.Combine(Application.streamingAssetsPath, videofile);

        videoPlayer.isLooping = true;

        videoPlayer.Play();
    }
}