Archive for March, 2012

Unity terrain avoiding smart follow camera



Download

I’ve been needing a terrain avoiding follow camera, I couldn’t find one that was exactly right so I produced my own.

My script uses multiple different methods to keep the followed object in view:

* Desired location with the camera adjusted for terrain height

* Rise above structures

* Zoom in to be closer to the followed object

* Camera rotation (cut view) to a different angle

Each method can be weighted to give the effect that you prefer.  Each method votes on how aggressive a movement it had to make in order to locate the camera in an unobstructed location, the winner gets to set the location of the camera.

You can download it here.

Here is it in operation – I’m moving the camera a bit to put it in difficult places, but mostly it’s under the control of the system.

, , ,

2 Comments

Unity automatic MeshCollider generation


When ever I get a model that has many sub elements, but no standard mesh colliders I use the wizard listed here to automatically generate them.  Thought it might come in useful to others…


using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Linq;
public class AddColliders : ScriptableWizard {

 [MenuItem("Wizards/Add Mesh Colliders")]
 static void CreateWizard()
 {
    ScriptableWizard.DisplayWizard<AddColliders>("Add mesh colliders", "Add Colliders");
 }

 void OnWizardCreate()
 {
    if(UnityEditor.Selection.activeGameObject != null)
    {
       foreach(var c in UnityEditor.Selection.activeGameObject.GetComponentsInChildren<MeshRenderer>().Cast<MeshRenderer>().
       Where(mr=>mr.GetComponent<MeshCollider>()==null))
       {
           c.gameObject.AddComponent(typeof(MeshCollider));
       }
    }
 }
 void OnWizardUpdate()
 {
    helpString = "Add mesh colliders to all items that have a mesh renderer";
 }

}

, , ,

Leave a comment