Sunday, 26 January 2014

LODs and Misc scripts

So I've been having alot of problems with my pc lately. I'm going to have to drop $700 onto it AGAIN before I can continue to make games or anything. That and I've been having a lot of trouble signing into my blogger account for some reason...

In any case. I've started thinking about doing a quick video tutorial on how to set up a basic game and some simple scripting and stuff. Some people have asked for it, and I would like the opportunity to teach myself some things from doing it, so I figure why not.

I've been working purely on concepts, scripting and small game dev work (due to the pc problems I'm constantly stuck in those phases, any thing more and my pc just restarts)

So I've decided to post a couple scripts I cooked up in 5 min to handle anything I need.

The first one is a script that makes an object look at the another object. This is useful to make an arrow point to an object to form a way point. Or to make a gun aim at a target. Or to make a directional light look at the player in space to simulate a sun. Or even to make AI face the player.

C# LookAtObject script

using UnityEngine;
using System.Collections;

public class LookAtObject : MonoBehaviour
{
public GameObject LookingObject;
public GameObject LookingTarget;

// Use this for initialization
void Start ()
{
LookingObject.transform.LookAt(LookingTarget.transform);
}

// Update is called once per frame
void Update ()
{
if (LookingTarget != null)
{
LookingObject.transform.LookAt(LookingTarget.transform);
}
}
}

The second script is here is a simple script to handle LODs and things like that. Its a little more tweak able and editable than Unitys built in LODs and can be used for a vast variety of things. You can use it to make AI behavior change over a distance. Or disable a lens flare or shadow casting light past a distance. Or swap objects for different qualities for better performance. Or do some tripy set pieces.

C# LODs script

using UnityEngine;
using System.Collections;

// An important thing to note with this script is that the objects are all still stored in memory, but ONLY the activated LOD object is rendered, lit and checked for physics. So while this script will increase performance, its still not perfect untill it removes objects completely upion lods.
// Someday I might write code that does more than just activating and deactivating objects. Like instansiating, cloning and destroying like you would a gun shot, but for now, this is what I need at this time.

// !**INSTRUCTIONS**! ----- So. What you want to do is these easy steps:
// 1) Make 5 LODs of an object (I recomend mudbox for this) !!!!! - remember that if you are using an object that will 'pop out' completely, the lowest quality model will be an empty game object. !!!!! - If you want to use less than 5 objects, simply set the unused objects as thesame lowest quality asset or just tweek my script.
// 2) Create an empty game object in Unity.
// 3) Put your various quality models inside the empty game object and make sure they are aligned (So that when they are enabled or disabled the object is always in the same position) !!!!! - note that naming is not important.
// 4) Attach this script to the empty game object that houses all the child LOD objects.
// 5) Drag each LOD object from the empty game object into its respective slot in the script properties.
// 6) set your desired distances between the player camera and lod levels.
// THATS ALL.

public class LODs : MonoBehaviour
{
public GameObject PlayerCamera;
public GameObject LODprefab; // the empty game object holding your LODs

// Arange models from -|/Highest quality (0)\|- to the -|/lowest quality (4)\|-
public GameObject LOD0;
public GameObject LOD1;
public GameObject LOD2;
public GameObject LOD3;
public GameObject LOD4;

public float LODdistance0; // LOD0 is active from 0 up to this distance
public float LODdistance1; // LOD1 is active after LODdistance0 and up to this distance
public float LODdistance2; // LOD2 is active after LODdistance1 and up to this distance
public float LODdistance3; // LOD3 is active after LODdistance2 and up to this distance
// LOD4 is active from LODdistance3 and further

public float ActualDistance;

// Use this for initialization
void Start ()
{
ActualDistance = Vector3.Distance (PlayerCamera.transform.position, LODprefab.transform.position);
}

// Update is called once per frame
void Update ()
{
ActualDistance = Vector3.Distance (PlayerCamera.transform.position, LODprefab.transform.position);

if (ActualDistance < LODdistance0)
{
LOD0.SetActive(true);

LOD1.SetActive(false);
LOD2.SetActive(false);
LOD3.SetActive(false);
LOD4.SetActive(false);
}

if (ActualDistance > LODdistance0 && ActualDistance < LODdistance1)
{
LOD0.SetActive(false);

LOD1.SetActive(true);

LOD2.SetActive(false);
LOD3.SetActive(false);
LOD4.SetActive(false);
}

if (ActualDistance > LODdistance1 && ActualDistance < LODdistance2)
{
LOD0.SetActive(false);
LOD1.SetActive(false);

LOD2.SetActive(true);

LOD3.SetActive(false);
LOD4.SetActive(false);
}

if (ActualDistance > LODdistance2 && ActualDistance < LODdistance3)
{
LOD0.SetActive(false);
LOD1.SetActive(false);
LOD2.SetActive(false);

LOD3.SetActive(true);

LOD4.SetActive(false);
}

if (ActualDistance >= LODdistance3)
{
LOD0.SetActive(false);
LOD1.SetActive(false);
LOD2.SetActive(false);
LOD3.SetActive(false);

LOD4.SetActive(true);
}
}
}



Hope that someone who needs these scripts finds them here.


No comments:

Post a Comment