C# Codes

For Character Movement (Move left and right)

- using UnityEngine;
using System.Collections;

public class CharacterMovement : MonoBehaviour {

public float maxSpeed = 6.0f;
public bool facingRight = true;
public float moveDirection;


public float jumpSpeed = 600.0f;


//Check Ground

public bool grounded = false;
public Transform groundCheck;
public float groundRadius = 2.0f;
public LayerMask whatIsGround;



void Awake()
{

groundCheck = GameObject.Find ("GroundCheck").transform;
}

// Use this for initialization
void FixedUpdate () {

grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);


GetComponent<Rigidbody> ().velocity = new Vector2 (moveDirection * maxSpeed, GetComponent<Rigidbody> ().velocity.y);

if (moveDirection > 0.0f && !facingRight) {

Flip ();

} else if (moveDirection < 0.0f && facingRight) {

Flip ();


}
}
void Flip(){

facingRight = !facingRight;

transform.Rotate (Vector3.up, 180.0f, Space.World);
}

// Update is called once per frame
void Update () {

moveDirection = Input.GetAxis("Horizontal");

if (grounded && Input.GetButtonDown ("Jump")) {

GetComponent<Rigidbody> ().AddForce (new Vector2 (0, jumpSpeed));
}

}
}


For camera to follow the character movement.

- using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {


public GameObject Character;

private Vector3 offset;


// Use this for initialization
void Start () {

offset = transform.position - Character.transform.position;

}

// Update is called once per frame
void Update () {


transform.position = Character.transform.position + offset;

}
}



For the items that the character needs to kelek. 

using UnityEngine;
using System.Collections;

public class myitem : MonoBehaviour {

// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}


void OnTriggerEnter(Collider other){

if (other.gameObject.tag == "Player") {

print ("MAK KAU");

gameObject.SetActive (false);

}

}

}

For "My Weapon"

- using UnityEngine;
using System.Collections;

public class MyWeapons : MonoBehaviour {


public int weapon =0;


// Use this for initialization
void Start () {
weapon = Random.Range (1, 5);

switch (weapon)

{
case 1:
print ("You found life");
break;

case 2:
print ("You found nothing");
break;

case 3:
print ("you mom is green");
break;

default:
print ("just die");
break;

}
}
// Update is called once per frame
void Update () {
}
}


Enemy Gone

- using UnityEngine;
using System.Collections;

public class enemygone : MonoBehaviour {


public float leputkau = 2.0f;


// Use this for initialization
void Start () {

Destroy (gameObject, leputkau);

}

// Update is called once per frame
void Update () {

}
}


Item Destory

- using UnityEngine;
using System.Collections;

public class itemDestroy : MonoBehaviour {


public float flarebye = 1.0f;


// Use this for initialization
void Start () {

Destroy (gameObject, flarebye);
}
// Update is called once per frame
void Update () {
}
}


Item Counter

- using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ItemCounter : MonoBehaviour {

public int itemcount = 0;

// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {

GetComponent<Text>().text = "My Items = " + itemcount ;
}
}


Bullet Destroy

- using UnityEngine;
using System.Collections;

public class MyBulletDestroy : MonoBehaviour {


public float bulletLife = 2.0f;


// Use this for initialization
void Start () {

Destroy (gameObject, bulletLife);
}
// Update is called once per frame
void Update () {
}
}

Share this:

CONVERSATION

0 comments:

Post a Comment