Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Saturday, May 18, 2013

Realtime Terrain Manipulation

Now that I have a game world split into cells, it can allow me to easily do some fancy things, such as targetted real-time terrain manipulation.  Think Populous where you would raise or lower the game world  or spells or technology that can cause a mountain or crater appear at the feet of an enemy (or player!), and have that mountain or crater become an actual part of the game level.

There are other things to focus on before this.  Camera setups, entity management, path finding - they take priority.  But I am going to revisit this.

I found a script on the forums.  Its in Java, I'll convert it to c# at a later date.
http://forum.unity3d.com/threads/87086-Realtime-Unity-Terrain-Manipulation-Possible?p=755093&viewfull=1#post755093

I ran the script as directed.  The end result is nothing fancy, just random heights being applied to a specific section of the map without recalculating the entire terrain.  That last part is important because recalculating the entire terrain mesh every frame is a massive performance hit.

All I need is a script to simulate curves...... mmmm curves...

Thursday, April 25, 2013

More C# conversions of the Unity scripts for the Lynda.com Unity essentials course


Projectile Class

public class Projectile : MonoBehaviour {

public GameObject projectile;
public float FireRate = 0.5f;
public float Speed = 5f;
internal float nextFire;

void Update ()
{
if (Input.GetButton ("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + FireRate;
GameObject clone = (GameObject)Instantiate(projectile, transform.position, transform.rotation);
clone.rigidbody.velocity = transform.TransformDirection(new Vector3(0,0,Speed));
Physics.IgnoreCollision(clone.collider, transform.root.collider); //stops projectile from colliding with player
}
}
}

Final Projectile Handler Class


public class ProjectileHandler : MonoBehaviour {

void Start ()
{
//Destroy(gameObject, 5);  // destroy object after set time
}

void Update () {

}

void OnCollisionEnter(Collision collison)
{
if (collison.transform.tag != "Dont Destroy")
{
//send message to collided object
collison.transform.SendMessage("BeenHit", SendMessageOptions.DontRequireReceiver);

//Debug.Log(collison.transform.name);

Destroy(gameObject, 1);
}
else
{
Destroy(rigidbody, 5);
//Debug.Log("stopped " + collison.transform.name);
}
}
}

DoorDamage Controller


public class DoorDamageController : MonoBehaviour {

public Collider doorJam;
public Collider wall;

void BeenHit()
{
//Destroy(gameObject, 1);
Physics.IgnoreCollision(doorJam.collider, transform.collider);
Physics.IgnoreCollision(wall.collider, transform.collider);
}
}

GameManager


public class GameManager : MonoBehaviour {

public GameObject fpc;

// Use this for initialization
void Start ()
{
//hide cursor
Screen.showCursor = false;

}

// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown("escape"))
{
Screen.showCursor = !Screen.showCursor;
fpc.SendMessage("ToggleInput", Screen.showCursor);
}
}
}







Tuesday, April 23, 2013

C# versions of the Lynda.com Unity course scripts

I recently completed the Unity 3.5 essentials course on Lynda.com in order to get past the limitations of the drag and drop features.  The course uses Javascript, but I prefer C#.  I'm putting my converted code here for anyone else that would like it. 

I use the internal instead of private access modifier as this will hide the variable from the inspector, but still make it available to other classes.


Final RotationController class.

using UnityEngine;
using System.Collections;

public class RotationController : MonoBehaviour {
public bool RotationState = true;
public float RotationSpeed = 20f;
internal float initialSpeed;
// Use this for initialization
void Start () {
initialSpeed = RotationSpeed;
}
// Update is called once per frame
void Update ()
{
if(RotationState && RotationSpeed > 5f)
{
transform.Rotate(0, RotationSpeed * Time.deltaTime, 0);
}
}
void OnMouseDown()
{
RotationState = !RotationState;
RotationSpeed -= 0.5f;
if (RotationSpeed < 0)
RotationSpeed = initialSpeed;
}
}

Final ReportDistance Class

using UnityEngine;
using System.Collections;

public class ReportDistance : MonoBehaviour {
public Transform targetObject;

// Use this for initialization
void Start () 
{
targetObject = GameObject.Find("First Person Controller").transform;
}
// Update is called once per frame
void Update () {
if (targetObject)
{
float distance = Vector3.Distance(targetObject.position, this.transform.position);
//Debug.Log("Distance to target object: " + distance);
}
}
}

Final RayCaster Class

using UnityEngine;
using System.Collections;

public class RayCaster : MonoBehaviour {
RaycastHit hit; //holds properties of detected object

// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () 
{
if(Physics.Raycast(transform.position, transform.forward, out hit, 10))
Debug.Log("There is a " + hit.transform.name + " something in front of the object!");
else Debug.Log("There is nothing within 10 metres!");
}
}