Friday, 10 July 2015

countdown timer in unity c# mm:ss format

Create "GameObject ==> UI ==> Text".
Attach below script to Canvas

Code :-
=====

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class timerScript : MonoBehaviour {

public Text TimerText;
public float countdownTime = 60 ;
// Update is called once per frame
void Update () {
countdownTime -= Time.deltaTime;
int minutes = Mathf.FloorToInt(countdownTime / 60F);
int seconds = Mathf.FloorToInt(countdownTime - minutes * 60);
string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
TimerText.text = niceTime;
}
}



Wednesday, 8 July 2015

enemy agent waypoint petrol & attack unity3d c#

1. You should attach hero.cs script to Hero and Patrol.cs to Enemy

2. Meanwhile you should create way points manually or by using WaypointCircuit.cs script  

3. Below i attached screenshots of InspectorView. Based on that you can arrange your enemy and hero characters.


// hero.cs
using UnityEngine;
using System.Collections;

public class hero : MonoBehaviour {
    public NavMeshAgent agent;
    //public NavMeshAgent agent2;
    Vector3 worldDeltaPosition;
    Vector3 smoothDeltaPosition = Vector3.zero;
    Vector3 velocity = Vector3.zero;
    public static bool eattack;
    public static bool erun;
    // Use this for initialization
    void Start () {

    }
    
    // Update is called once per frame
    void Update () {
        worldDeltaPosition = agent.nextPosition - transform.position;

        
        // Low-pass filter the deltaMove
        float smooth = Mathf.Min(1.0fTime.deltaTime/0.15f);
        smoothDeltaPosition = Vector3.Lerp (smoothDeltaPositionworldDeltaPositionsmooth);
        
        // Update velocity if time advances
        if (Time.deltaTime > 1e-5f)
            velocity = smoothDeltaPosition / Time.deltaTime;



        if (worldDeltaPosition.magnitude > agent.radius || worldDeltaPosition.magnitude < 2
        {
            erun = false;
            return;
        }
        else 
        {
            erun = true;
            agent.nextPosition = transform.position + 0.9f * smoothDeltaPosition;
        }

        if (worldDeltaPosition.magnitude > 2
        {
            eattack = false;
            return;
        }
        else 
        {
            eattack = true;
        }
    }


}






//Patrol.cs
using UnityEngine;
using System.Collections;


public class Patrol : MonoBehaviour {
    
    public Transform[] points;
    private int destPoint = 0;
    private NavMeshAgent agent;
    //public NavMeshAgent agent2;
    Animator m_Animator;
    public Transform target;
    void Start () {
        agent = GetComponent<NavMeshAgent>();
        //agent.updatePosition = false;
        // Disabling auto-braking allows for continuous movement
        // between points (iethe agent doesn't slow down as it
        // approaches a destination point).
        agent.autoBraking = false;
        m_Animator = GetComponent<Animator>();
        GotoNextPoint();

    }
    
    
    void GotoNextPoint() {
        // Returns if no points have been set up
        if (points.Length == 0)
            return;
        
        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;
        
        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Length;
    }
    
    
    void Update () {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (agent.remainingDistance < 0.5f)
            GotoNextPoint();


        if (hero.erun == true) {
            m_Animator.SetBool ("run"true);
            agent.speed = 3;
            transform.LookAt(target);
        } else {
            m_Animator.SetBool ("run"false);
            agent.speed = 2;

        }


        if (hero.eattack == true) {
            m_Animator.SetBool ("attack"true);
            agent.speed = 0;
            //agent.updatePosition = false;
            //transform.LookAt(Vector3.zero);
        } else {
            m_Animator.SetBool ("attack"false);
            //agent.speed = 2;
            //agent.updatePosition = true;
        }


    }


}


Enemy Character Inspector Screenshot :-
++++++++++++++++++++++++++++++++++++++



















Hero Character Inspector Screenshot :-
++++++++++++++++++++++++++++++++++++++



















Waypoint Arrangement Scene View:-
+++++++++++++++++++++++++++++++++
















WayPointCircuit Arrangement :-
++++++++++++++++++++++++++++++++