본문 바로가기
카테고리 없음

Unity Scripts

by 대마왕J 2014. 1. 16.

Character Move

 

using UnityEngine;
using System.Collections;

public class CharactorMove : MonoBehaviour {

 public Transform cameraTransform;
 
 public float movespeed = 10.0f;
 public float jumpspeed = 10.0f;
 public float gravity = -20.0f;
 // Update is called once per frame
 
 
 CharacterController characterController = null;
 float yVelocity = 0.0f;
 
 // Use this for initialization
 void Start () {
  
  characterController = GetComponent<CharacterController>();
  
 }
 
 
 
 void Update () {
  
  
  float x = Input.GetAxis("Horizontal");
  float z = Input.GetAxis("Vertical");
  
  
  Vector3 moveDirection = new Vector3(x,0,z);
  
  moveDirection = cameraTransform.TransformDirection(moveDirection);
  moveDirection *= movespeed;
  
  
  if (Input.GetButtonDown("Jump"))
  {
   yVelocity = jumpspeed;
  }

   
  yVelocity += gravity*Time.deltaTime;
  moveDirection.y = yVelocity;
 
  
  
  characterController.Move(moveDirection*Time.deltaTime);


  if (characterController.collisionFlags == CollisionFlags.Below)
  {
   yVelocity = 0;
  }
  
  
  
  //Vector3 speed = new Vector3(x,0,z);
  
  //  transform.position += (transform.forward * z + transform.right*x).normalized 
  
  
  
  
 }
}
=================================================================

 

Mouse Look

 

using UnityEngine;
using System.Collections;

public class mouselook : MonoBehaviour {

 // Use this for initialization
 void Start () {
 
 }

 float rotationX = 0.0f;
 float rotationY = 0.0f;

 public float sensitivity = 700.0f;


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

  float x = Input.GetAxis("Mouse X");
  float y = Input.GetAxis("Mouse Y");

  rotationX += x*sensitivity * Time.deltaTime;
  rotationY += y*sensitivity * Time.deltaTime;


  rotationX %= 360.0f;
  rotationY %= 360.0f;

  //Debug.Log(rotationX);

  rotationY = Mathf.Clamp(rotationY, -24.0f,50.0f);

  transform.eulerAngles = new Vector3(-rotationY,rotationX , 0);

 

 

 
 }
}

 

==============================================================================

 

Fire Manager

 

using UnityEngine;
using System.Collections;

public class firemanager : MonoBehaviour {

 public Transform cameraTransform;
 public GameObject boomBall;
 public float movespeed = 30.0f;
 float firetime = 1.0f;
 public GameObject spark;

 public GameObject Bazoo = null;
 Animation bazooAni;

 // Use this for initialization
 void Start () {
 
  bazooAni = Bazoo.GetComponent<Animation>();

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


  Vector3 moveDirection = cameraTransform.position;
  moveDirection = cameraTransform.TransformDirection(moveDirection);

 

  if (Input.GetButton("Fire1"))
  {


   if (firetime<0.2f)
   {
    firetime += Time.deltaTime;
    //Debug.Log(firetime);
    return;
   }
//   else
//   {
    
//   }

//   bazooAni.Play("shoot");
   bazooAni.CrossFadeQueued("shoot",0.1f,QueueMode.PlayNow);
   bazooAni.CrossFadeQueued("idle",0.3f,QueueMode.CompleteOthers);

   //Debug.Log ("fire");
   GameObject bullet = Instantiate(boomBall) as GameObject;
   bullet.transform.position = cameraTransform.position ;
   bullet.transform.forward = cameraTransform.forward;
   bullet.rigidbody.velocity =cameraTransform.forward *movespeed;//+  new Vector3 (0,0,10);


   GameObject sp = Instantiate(spark) as GameObject;
   sp.transform.position = cameraTransform.position ;
   sp.transform.forward = cameraTransform.forward;

   firetime = 0.0f;
   //bazooAni.animation
   }
   
 }
}
=============================================================================

Monster FSM

 

 

using UnityEngine;
using System.Collections;


public class SpiderScript : MonoBehaviour {


 enum SpiderState
 {
  idle,
  move,
  attack,
  die,
  damage
 }

 SpiderState spiderstate = SpiderState.idle;

 float stateTime = 0;
 public float idlemaxtime = 2.0f;

 Transform target;
 CharacterController cc = null;
 public float speed = 10.0f;
 public float rotatespeed = 3.0f;
 public float attackStateMaxTime = 2.0f;
 public float attackRange = 1.0f;
 int spiderHelth = 5;
 public float deathtime = 2.0f;
 float dieTime = 0.0f;


 // Use this for initialization
 void Start () {

  target = GameObject.Find("Player").transform;

  cc = GetComponent<CharacterController>();

  spiderstate = SpiderState.idle;
  animation.Play("iddle");
  stateTime = 0;
 
 }


 void OnDrawGizmos()
 {
  Gizmos.color = Color.red;
  Gizmos.DrawSphere(transform.position + Vector3.up*2.0f, attackRange);

  if (target)
  Gizmos.DrawLine(transform.position,target.position);
 }


 void OnCollisionEnter(Collision col)
 {

  if (spiderstate != SpiderState.die)
  {
  if (col.gameObject.name.Contains("boom") == false)
  {
   return;
  }

  spiderstate = SpiderState.damage;
  }
 }

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


  switch(spiderstate)
  {

  case SpiderState.idle:

   stateTime += Time.deltaTime;
   if (stateTime>idlemaxtime)
   {
    stateTime =0;
    spiderstate = SpiderState.move;


   }

   break;

  case SpiderState.move:
   animation.Play("walk");

   Vector3 dir = target.position - transform.position;

   float distance = dir.magnitude;

   dir = Vector3.Normalize(dir);

   cc.SimpleMove(dir*speed);
   transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion.LookRotation(dir),rotatespeed*Time.deltaTime);

   if (distance<attackRange)
   {
    spiderstate = SpiderState.attack;
    stateTime = attackStateMaxTime;
   }

 


   break;


  case SpiderState.attack:

   stateTime += Time.deltaTime;
   if (stateTime > attackStateMaxTime)
   {

    stateTime = 0.0f;
    animation.Play("attack_Melee");
    animation.PlayQueued("iddle",QueueMode.CompleteOthers);

   }

   Vector3 dir2 = target.position - transform.position;
   
   float distance2 = dir2.magnitude;

   if (distance2>attackRange)
   {
    spiderstate = SpiderState.move;

   }


   break;

  case SpiderState.damage:

   spiderHelth--;

   animation["damage"].speed = 0.5f;
   animation.Play("damage");
   animation.PlayQueued("iddle",QueueMode.CompleteOthers);
   stateTime =0;
   spiderstate = SpiderState.idle;

   if (spiderHelth <= 0)
   {
    animation.Play("death");
    spiderstate = SpiderState.die;
   }

   break;

  case SpiderState.die:

   //animation.Play("death");


   if (dieTime < deathtime)
   {
    dieTime += Time.deltaTime;
   }
   else
   {
   Destroy(gameObject);
   }

   break;

 


  }
 
 }
}
====================================================================

Destroy zone

 

using UnityEngine;
using System.Collections;

public class DestroyZone : MonoBehaviour {

 // Use this for initialization
 void OnTriggerEnter (Collider other) {
  if (other.gameObject.name.Contains("Player"))
  {
   other.gameObject.transform.position = new Vector3(0.0f,20.0f,0.0f);
  }
  else if(other.gameObject.name.Contains("boom"))
  {
   Destroy(other.gameObject);

  }
 
 }
}
 

 

 

==================================================================

 

BoomProcess

 

using UnityEngine;
using System.Collections;

public class BoomProcess : MonoBehaviour {

 public GameObject explosion;
 public GameObject airexplosion;

 // Use this for initialization
 void Start () {
 
 }


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

  int layer = collision.gameObject.layer;

  if (layer == LayerMask.NameToLayer("ground"))
  {
  //Debug.Log(collision.gameObject.name,collision.gameObject);
  GameObject ex = Instantiate(explosion)as GameObject;
  ex.transform.position = transform.position;
  }

  else  {
   //Debug.Log(collision.gameObject.name,collision.gameObject);
   GameObject ex = Instantiate(airexplosion)as GameObject;
   ex.transform.position = transform.position;
  }


  Destroy(gameObject);
 }
}
================================================================

 

코루틴 예제

 

using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour {

 Vector3 myLocalPosition ;

 

 // Use this for initialization
 void Start () {

  myLocalPosition = transform.localPosition;
 
 }

 

 public void PlayCameraShake()
 {

  StartCoroutine(CameraShakePrcess(1.0f,0.2f));
 }


 IEnumerator CameraShakePrcess(float shakeTime, float shakeSense)
 {
  float deltaTime = 0.0f;

  while (deltaTime < shakeTime)
  {
   deltaTime += Time.deltaTime;
   Vector3 pos = Vector3.zero;
   pos.x = Random.Range(-0.1f,0.1f);
   pos.y = Random.Range(-0.1f,0.1f);
   pos.z = Random.Range(-0.1f,0.1f);

   transform.localPosition += pos;
   //transform.localPosition = myLocalPosition;
   yield return new WaitForEndOfFrame();

  }

  transform.localPosition = myLocalPosition;
 }


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

 

반응형

댓글