Tuesday, August 26, 2014

Unity Web Player | Emily Bloode Level Design 1
Unity Web Player | Emily Bloode Level Design 1

Friday, October 11, 2013

First Person Character Controller; no head movement.

I love how this one works. It's a first person character controller, that jumps. However, it has support for footsteps, and running, (though I haven't put in the footsteps when running yet, but I will be). I'm using this for a game I'm working on, and it works great. Here is the character controller script, ready to be implemented.


var speed : float = 1;
var rotSpeed : float = 1;
var jumpStrength : float = 5;
var isGrounded : boolean;
var horizontal : float;
var vertical : float;
var leftFoot : AudioClip;
var left : boolean;
var right : boolean;
var rightFoot : AudioClip;
var steps : float;

function Start () {

}

function Update () {
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
if (Input.GetAxis("Horizontal"))
transform.Rotate(Vector3(0, horizontal * rotSpeed, 0));
if (isGrounded)
{
if (Input.GetAxis("Vertical"))
{
rigidbody.AddRelativeForce(Vector3(0, 0, vertical * speed), ForceMode.VelocityChange);
steps += Time.deltaTime;

if (steps >= .5 && steps < .8 && !left)
{
audio.PlayOneShot(leftFoot);
left = true;
right = false;
}
if (steps >=1 && steps < 1.3 && !right)
{
audio.PlayOneShot(rightFoot);
steps = 0;
right = true;
left = false;
}
if (steps >1.016)
steps = 0;
}
if (Input.GetButtonDown("Jump"))
rigidbody.AddRelativeForce(Vector3(0, 1 * jumpStrength,0), ForceMode.Impulse);
if (Input.GetAxis("Vertical") && Input.GetKey(KeyCode.LeftShift))
rigidbody.AddRelativeForce(Vector3(0, 0, vertical * speed*1.5), ForceMode.VelocityChange);
if (vertical == 0)
{
steps = 0;
left = false;
right = false;
}
}
if (!isGrounded)
{
if (Input.GetAxis("Vertical"))
rigidbody.AddRelativeForce(Vector3(0, 0, vertical * speed/3), ForceMode.VelocityChange);
}
}

function OnCollisionEnter (Col : Collision) {
if (Col.gameObject.tag == "Floor")
isGrounded = true;
}

function OnCollisionExit (Col : Collision) {
if (Col.gameObject.tag == "Floor")
isGrounded = false;
}

Wednesday, October 9, 2013

2D Character Controller

A simple character control script I wrote, which allows movement, jump, and wall jumping.


var speed : int = 1;
var isGrounded : boolean;
var isWalled : boolean;
var horizontal : float;
var vertical : float;
var directional : boolean;

function Start () {

}

function Update () {
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
directional = true;
else
directional = false;
if (isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space))
{
rigidbody.AddForce(Vector3.up * 5, ForceMode.Impulse);
}
if (Input.GetAxis("Horizontal"))
{
rigidbody.AddForce(Vector3(horizontal * (speed * 2), 0, 0), ForceMode.Acceleration);
}
}
if (isWalled)
{
rigidbody.drag = 6;
if (Input.GetAxis("Horizontal") && Input.GetKeyDown(KeyCode.Space))
{
rigidbody.AddForce(Vector3(horizontal * (speed * 10), 5, 0), ForceMode.Impulse);
}
if (Input.GetKeyDown(KeyCode.Space) && !directional)
{
isWalled = false;
}
}
if (!isWalled)
{
rigidbody.drag = 1;
}
}

function OnCollisionEnter (Col : Collision) {

if (Col.gameObject.tag == "Floor")
{
isGrounded = true;
isWalled = false;
}
if (Col.gameObject.tag == "Wall")
{
isWalled = true;
horizontal = 0;
}

}

function OnCollisionExit (Col : Collision) {

if (Col.gameObject.tag == "Floor")
isGrounded = false;
if (Col.gameObject.tag == "Wall")
isWalled = false;
}

The above script allows the arrow keys, as well as spacebar, to control a character on the X axis, and jump using the Y axis. When used with the following camera script, it can be used to control a 2D platformer character with no additional code.



//The target is the character you want to follow. 
var target : Transform;


function Start () {

}

function Update () {

gameObject.transform.position.x = target.transform.position.x;
gameObject.transform.position.y = target.transform.position.y;
}