Cours Unity contact(at)guillaumelevieux.com :: « Home
JMIN 1 21/01/2016


using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Transform))]
public class Test : MonoBehaviour {

    Transform monTransform;

    // Use this for initialization
    void Start() {
        monTransform = GetComponent<Transform>();
        if (monTransform == null)
            Debug.Log("Erreur pas de transform");
    }

    // Update is called once per frame
    void Update() {
        monTransform.Rotate(0, 360 * Time.deltaTime, 0);
    }
}


Basic Rotate


using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody))]
public class Test2 : MonoBehaviour {

    	Rigidbody rb;

	// Use this for initialization
	void Start () {
		rb = GetComponent<Rigidbody>();
	}

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

	    	float distToCube = (transform.position - Camera.main.transform.position).magnitude;

	    	if (Input.GetButton("Fire1") && distToCube < 3.5)
	       		rb.AddForce(Camera.main.transform.forward*40);

	    	if (Input.GetButtonDown("Fire2"))
	       		Debug.Log("Distance au cube :" + distToCube);
	}
}


Basic Push


using UnityEngine;
using System.Collections;

public class Shoot : MonoBehaviour {

    //Variables public
    public Transform prefabBullet;
    public float timeShoot = 0.3f;

    //Variables A NOUS
    float timeElapsedShoot = 0;

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

        timeElapsedShoot = timeElapsedShoot + Time.deltaTime;
        if (Input.GetButton("Fire1") && timeElapsedShoot >= timeShoot)
        {
            timeElapsedShoot = 0;
            Transform b = GameObject.Instantiate(prefabBullet) as Transform;
            b.position = Camera.main.transform.position + Camera.main.transform.forward * 3;
            b.GetComponent<Rigidbody>().AddForce(Camera.main.transform.forward * 10, ForceMode.Impulse);
            b.rotation = Camera.main.transform.rotation;
        }
    }
}


Basic Shoot


using UnityEngine;
using System.Collections;

public class AutoKill : MonoBehaviour {

    public Transform prefabExplosionEffect;
    public float deltaTimeKill = 3;

    float timeElapsedKill = 0;
    bool dejaEuCollision = false;

    // Collision
    public void OnCollisionEnter(Collision col)  {
        dejaEuCollision = true;
        Debug.Log("Bullet collide");
        
    }
	
	// Update is called once per frame
	void Update () {
        if (dejaEuCollision)
        {
            timeElapsedKill = timeElapsedKill + Time.deltaTime; 
        }

        if (timeElapsedKill >= deltaTimeKill)
        {
            Destroy(this.gameObject);
            GameObject.Instantiate(prefabExplosionEffect, transform.position, Quaternion.identity);
        }
            
    }
}



Auto Kill


using UnityEngine;
using System.Collections;

public class Mine : MonoBehaviour {

    	public Transform ExplosionPrefab;
    
	public void OnTriggerEnter(Collider col)
    	{
        		if(col.gameObject.tag == "Player")
        		{
            		Debug.Log("Coucou");
	            	GameObject.Instantiate(ExplosionPrefab,this.transform.position,Quaternion.identity);
	            	col.GetComponent<Rigidbody>().AddExplosionForce(300, col.transform.position, 10,1,ForceMode.Impulse);
       	 	}
    	}
}



Mine


using UnityEngine;
using System.Collections;

public class OPenDoorDist : MonoBehaviour {

    public Transform porte;

    public void Start()
    {
        GetComponent<Renderer>().enabled = false;
    }

    public void OnTriggerEnter(Collider col)
    {
        porte.GetComponent<Animator>().SetBool("Open", true);
    }

    public void OnTriggerExit(Collider col)
    {
        porte.GetComponent<Animator>().SetBool("Open", false);
    }
}



Ouverture de porte a distance



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

public class DialogueManager : MonoBehaviour {

    [Serializable]
    public class Dialogue
    {
        public String Line;
        public Transform SourceObj;
        public float Distance = 3;
    }
     
    public Dialogue[] dialogues;
    public Text Text;

	
    // Update is called once per frame
    void Update () {
        Text.text = "";
        for(int i=0;i< dialogues.Length;i++)
        {
            if(Vector3.Distance(dialogues[i].SourceObj.position,Camera.main.transform.position) < dialogues[i].Distance)
            {
                Text.text = dialogues[i].Line;
            }
        }
    }
}



Gestionnaire de dialogues



using UnityEngine;
using System.Collections;

public class Follower : MonoBehaviour {
    bool hasTarget = false;
	
	// Update is called once per frame
	void Update () {
        NavMeshAgent na = GetComponent<NavMeshAgent>();
        if (!hasTarget)
        {
           na.destination = Camera.main.transform.position;
            hasTarget = true;
        }

        if (na.velocity.magnitude < 0.5 || 
            Vector3.Distance(na.destination,Camera.main.transform.position) > 3)
            hasTarget = false;
    }
}



NavMesh Agent Follower




<< précédent  0  1  2  3  4  5  6  7  8  9  10  suivant >>