EJEMPLOS DE C# CON UNITY


EJEMPLOS DE C# CON UNITY...

LOS MAS DIFICILES DE APLICAR Y DE COMPRENDER SON LOS DE DISPARAR Y EL DE QUE TE SIGAN ENEMIGOS.....LO IRE AMPLIANDO Y EXPLICANDO POCO A POCO PORQUE LO ACAVO DE APRENDER Y AUN E DE PRACTICAR MUCHISIMO LOS DOS TEMAS.








...EnemyMovement..ENEMIGOS QUE ME PERSIGEN-


 ----------------------------------------------------------------------------------------------------------------------
 EJEMPLOS DE C# CON UNITY......EnemyMovement..ENEMIGOS QUE ME PERSIGEN-

-----------------------------------------------------------------------------------------------------------------------

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour
{
    Transform player;
    //PlayerHealth playerHealth;
    //EnemyHealth enemyHealth;
    UnityEngine.AI.NavMeshAgent nav;


    void Awake ()
    {
        player = GameObject.FindGameObjectWithTag ("Player").transform;
        //playerHealth = player.GetComponent ();
        //enemyHealth = GetComponent ();
        nav = GetComponent ();
    }


    void Update ()
    {
        //if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
        //{
            nav.SetDestination (player.position);
        //}
        //else
        //{
        //    nav.enabled = false;
        //}
    }
}





------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
--------------------------ActivateTrigger------
OTRO EJEMPLO C#  DE COLISIONES Y CAMBIO DE GRAFICOS PARA DISPAROS
---------------------------------------------------------------------------------------------------------

using System;
using UnityEngine;
using Object = UnityEngine.Object;

namespace UnityStandardAssets.Utility
{
    public class ActivateTrigger : MonoBehaviour
    {
        // A multi-purpose script which causes an action to occur when
        // a trigger collider is entered.
        public enum Mode
        {
            Trigger = 0,    // Just broadcast the action on to the target
            Replace = 1,    // replace target with source
            Activate = 2,   // Activate the target GameObject
            Enable = 3,     // Enable a component
            Animate = 4,    // Start animation on target
            Deactivate = 5  // Decativate target GameObject
        }

        public Mode action = Mode.Activate;         // The action to accomplish
        public Object target;                       // The game object to affect. If none, the trigger work on this game object
        public GameObject source;
        public int triggerCount = 1;
        public bool repeatTrigger = false;


        private void DoActivateTrigger()
        {
            triggerCount--;

            if (triggerCount == 0 || repeatTrigger)
            {
                Object currentTarget = target ?? gameObject;
                Behaviour targetBehaviour = currentTarget as Behaviour;
                GameObject targetGameObject = currentTarget as GameObject;
                if (targetBehaviour != null)
                {
                    targetGameObject = targetBehaviour.gameObject;
                }

                switch (action)
                {
                    case Mode.Trigger:
                        if (targetGameObject != null)
                        {
                            targetGameObject.BroadcastMessage("DoActivateTrigger");
                        }
                        break;
                    case Mode.Replace:
                        if (source != null)
                        {
                            if (targetGameObject != null)
                            {
                                Instantiate(source, targetGameObject.transform.position,
                                            targetGameObject.transform.rotation);
                                DestroyObject(targetGameObject);
                            }
                        }
                        break;
                    case Mode.Activate:
                        if (targetGameObject != null)
                        {
                            targetGameObject.SetActive(true);
                        }
                        break;
                    case Mode.Enable:
                        if (targetBehaviour != null)
                        {
                            targetBehaviour.enabled = true;
                        }
                        break;
                    case Mode.Animate:
                        if (targetGameObject != null)
                        {
                            targetGameObject.GetComponent().Play();
                        }
                        break;
                    case Mode.Deactivate:
                        if (targetGameObject != null)
                        {
                            targetGameObject.SetActive(false);
                        }
                        break;
                }
            }
        }


        private void OnTriggerEnter(Collider other)
        {
            DoActivateTrigger();
        }
    }
}







--------------------------------------------------------------------------------------------------------------
-----C# EXCLUSIVO PARA DISPARAR........CAÑONAZO
---------------------------------------------------------------------------------------------------------










///









/// Launcher.
///
/// 3/2/2013
/// Steve Peters
/// Game Developers Guild - Miami, FL
///
/// Allows us to launch projectiles at a wall. It preinstantiates and stores the projectiles in a
/// stack to improve performance
///

using UnityEngine;
using System.Collections;

public class CAÑONAZO : MonoBehaviour
{
   
    public Rigidbody projectile;
    public Rigidbody explosiveProjectile;
    public float launchspeed = 50;
    public bool useExplodingProjectiles = false;

   
    private float _LaunchDelayTime = 0.10f;
   
    public int stackSize = 6;///////AKI SALEW LO DE INFINITAS BALAS HAY QUE PONER 2000 POR EL OTRO LADO Y AKI 6           
    public Transform launchHole1;
    public Transform launchHole2;
   
    private Stack _Projectiles;
    private Stack _ExplosiveProjectiles;
    private Transform _myTransform;
   
    // Use this for initialization
    void Start ()
    {
        _myTransform = transform;
        _Projectiles = new Stack();
        if(useExplodingProjectiles)
        {
            _ExplosiveProjectiles = new Stack();
        }
       
        for(int i = 0; i <  stackSize; i++)
        {







            Rigidbody tr = Instantiate (projectile, _myTransform.position, _myTransform.rotation) as Rigidbody;
            PushProjectile(tr);
           
            if(useExplodingProjectiles)
            {
                Rigidbody rr = Instantiate (explosiveProjectile, _myTransform.position, _myTransform.rotation) as Rigidbody;
                PushExplosiveProjectile(rr);
            }
        }
    }
   
    // Update is called once per frame
    void Update ()
    {
        if(_Projectiles.Count > 1)
        {
            if(Time.time > _LaunchDelayTime)
            {   
                if (Input.GetButtonDown ("Fire1"))
                   
                   
                    //GetComponent().Play();////////////////////////////////////////////////////////PUESTO YO
               
               
               
               
               
                {
                    Rigidbody tr = PopProjectile();
                    tr.gameObject.SetActive(true);
                    tr.transform.position = launchHole1.position;
                    tr.transform.rotation = launchHole1.rotation;
                    tr.velocity = transform.TransformDirection (Vector3.forward * launchspeed);
                   
                    tr = PopProjectile();
                    tr.gameObject.SetActive(true);
                    tr.transform.position = launchHole2.position;
                    tr.transform.rotation = launchHole2.rotation;
                    tr.velocity = transform.TransformDirection (Vector3.forward * launchspeed);
                   
                    _LaunchDelayTime = Time.time + 0.5f;
                }
            }   
        }
       
        if(useExplodingProjectiles)
        {
            if(_ExplosiveProjectiles.Count > 0)
            {
                if(Time.time > _LaunchDelayTime)
                {   
                    if (Input.GetButtonDown ("Fire2"))
                    {
                        Rigidbody tr = PopExplosiveProjectile();
                        tr.gameObject.SetActive(true);
                        tr.transform.position = launchHole1.position;
                        tr.transform.rotation = launchHole1.rotation;
                        tr.velocity = transform.TransformDirection (Vector3.forward * launchspeed);
                       
                        tr = PopExplosiveProjectile();
                        tr.gameObject.SetActive(true);
                        tr.transform.position = launchHole2.position;
                        tr.transform.rotation = launchHole2.rotation;
                        tr.velocity = transform.TransformDirection (Vector3.forward * launchspeed);
                       
                        _LaunchDelayTime = Time.time + 0.5f;
                    }
                }   
            }
        }
    }
   
    public void PushProjectile(Rigidbody x)
    {
        x.gameObject.SetActive(false);
        _Projectiles.Push(x);
    }
   
    public Rigidbody PopProjectile()
    {
        return (Rigidbody)_Projectiles.Pop();
    }
   
    public void PushExplosiveProjectile(Rigidbody x)
    {
        x.gameObject.SetActive(false);
        _ExplosiveProjectiles.Push(x);
    }
   
    public Rigidbody PopExplosiveProjectile()
    {
        return (Rigidbody)_ExplosiveProjectiles.Pop();
    }
}



--------------------------------------------------------------------------------------------------
C# PARA QUE SE BORRE EL PROYECTIL QUE LANZAMOS Y NO SE CUELGE EL JUEGO
---------------------------------------------BORRAMIBALA---------------------------------
---------------------------------------------------------------------------------------------




using UnityEngine;
using System.Collections;

public class BORRAMIBALA : MonoBehaviour {
   
    // Use this for initialization
    void Start () {
       
    }
   
   
   
   
    void OnCollisionEnter (){
       
        Destroy (gameObject, 10.30f);
       
       
       
       
       
    }
}


--------------------------------------------------------------------------------------------------------------
C#  SIMPLE PARA BORRAR DESTRUIR OBJETOS Y LIVERAR MEMORIA YO LO TITULE TOLDO
PERO POR SUPUESTO SE PUEDE PONER CUALQUIR NOMBRE.
--------------------------------------------------------------------------------------------------------







using UnityEngine;
using System.Collections;

public class TOLDO : MonoBehaviour {
   
    // Use this for initialization
    void Start () {
       
    }
   
   
   
   
    void OnCollisionEnter (){
       
        Destroy (gameObject, 0.0f);
       
       
       
       
       
    }
}

--------------------------------------------------------------------------------------------------------------------
C#   PARA MANEJAR AL PLAYER O PROTAGINISTA DEL JUEGO CON EL TECLADO DEL PC


TransformFunctions
---------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------




using UnityEngine;
using System.Collections;

public class TransformFunctions : MonoBehaviour
{
    public float moveSpeed = 10f;
    public float turnSpeed = 50f;


    void Update ()
    {
        if(Input.GetKey(KeyCode.UpArrow))
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

        if(Input.GetKey(KeyCode.DownArrow))
            transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);

        if(Input.GetKey(KeyCode.LeftArrow))
            transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);

        if(Input.GetKey(KeyCode.RightArrow))
            transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
    }
}


----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
C#   PARA ROTAR OBJETOS.....Scriptrotar......DAR MOVIMIENTO


----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------



using UnityEngine;
using System.Collections;

public class Scriptrotar : MonoBehaviour {

    // Use this for initialization
    void Start () {
   

            //transform.rotation = Quaternion.Euler (0,45, 0);
        }
       



   
    // Update is called once per frame
    void Update () {
        // Es la rotacion de Angulos de Euler en grados.
        transform.Rotate (new Vector3 (0 * Time.deltaTime, 4, 0), Space.Self);


    }
}





----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
C#   PARA MOVIMIENTOS OBJETOS.....muebederecha......DAR MOVIMIENTO


----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------








using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class muebederecha : MonoBehaviour {

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
        transform.position += transform.right * 180.82f * Time.deltaTime; }   
    }








----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
C#   PARA MOVIMIENTOS OBJETOS...muebelado....DAR MOVIMIENTO


----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------













using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class muebelado : MonoBehaviour {

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



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

        transform.position += transform.forward * 10.2f * Time.deltaTime; }

       
    }


----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
LOS MAS DIFICILES DE APLICAR Y DE COMPRENDER SON LOS DE DISPARAR Y EL DE QUE TE SIGAN ENEMIGOS.....LO IRE AMPLIANDO Y EXPLICANDO POCO A POCO PORQUE LO ACAVO DE APRENDER Y AUN E DE PRACTICAR MUCHISIMO LOS DOS TEMAS.

🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻

tutorial enemigos persiguiendo a player

tutorial efecto reventar y abrir una caja fuerte

🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺

No hay comentarios:

Publicar un comentario