Tuesday, 15 September 2015

Click to find the hit object RaycastHit unity

using UnityEngine;
using System.Collections;
    
    public class ClickScript : MonoBehaviour {
        
        void Update () 
        {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            if (Input.GetButtonDown ("Fire2")) 
            {
                if (Physics.Raycast(ray, out hit, 100))
                {
                Debug.Log("hit.tranform = "+hit.transform);
                    if (hit.collider.CompareTag("a"))
                    {
                        //Debug.Log("hit.tranform = "+hit.transform);
                        //Debug.Log("hit.tranform = "+hit.transform.tag);
                    }
                    
                    else
                    {
                        //Debug.Log("hit.point = "+hit.point);
                    }
                }
            }
            
            
        }
        
        
    }



int to string, string to int conversion c# unity

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

public class ConversionScript : MonoBehaviour {

    int inumber = 12;
    float fnumber = 24.5f;
    string sname = "Satya";

    int TestNumber;
    float TestFnumber;
    string TestName;


    public InputField ScoreField;
    int score;
    public Text scoreText;
    void Start () {

        TestName = inumber.ToString(); // int to string conversion
        TestNumber = int.Parse(sname); // string to int conversion

        TestNumber =  (int)fnumber; // float to int conversion
        TestFnumber = inumber; // int to float conversion

        TestName = fnumber.ToString(); // float to string conversion

    }

    void Update()
    {
        score = int.Parse(ScoreField.text);
        Debug.Log ("Input field Score :"+score);

        scoreText.text = score.ToString();
    }

}






Saturday, 12 September 2015

Simple Pendulum Movement unity




By using below script you can move the pendulum (mobile swipes)
















Script :-


using UnityEngine;
using System.Collections;

public class TouchControl : MonoBehaviour {

    public Rigidbody ball; // Drag your ball here
    private Vector2 fp; // first finger position
    private Vector2 lp; // last finger position

    void Start() {
        ball = GetComponent<Rigidbody>();
    }
    
    void FixedUpdate () {
        foreach(Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                fp = touch.position;
                lp = touch.position;
            }
            if (touch.phase == TouchPhase.Moved )
            {
                lp = touch.position;
            }
            if(touch.phase == TouchPhase.Ended)
            {
                if((fp.x - lp.x) > 80) // left swipe
                {
                    ball.AddForce(transform.forward * 150f);
                }
                else if((fp.x - lp.x) < -80) // right swipe
                {
                    ball.AddForce(transform.forward * -150f);
                    
                }
                
            }
        }
    }
}

double tap, single tap, swipe all direction unity c#

using UnityEngine;
using System.Collections;

public class touch : MonoBehaviour {

    public Transform player; // Drag your player here
    private Vector2 fp; // first finger position
    private Vector2 lp; // last finger position
    private float angle;
    private float swipeDistanceX;
    private float swipeDistanceY;
    
    void Update () {

        for (var i = 0; i < Input.touchCount; ++i)
        {
            if (Input.GetTouch(i).phase == TouchPhase.Began) 
            {
                if(Input.GetTouch(i).tapCount == 2)
                {
                    Debug.Log("Double tap..");
                }
                if(Input.GetTouch(i).tapCount == 1)
                {
                    Debug.Log("Single tap..");
                }
            }
        }

        foreach(Touch touch in Input.touches)
        {

            if (touch.phase == TouchPhase.Began)
            {
                fp = touch.position;
                lp = touch.position;
            }
            if (touch.phase == TouchPhase.Moved )
            {
                lp = touch.position;
                swipeDistanceX = Mathf.Abs((lp.x-fp.x));
                swipeDistanceY = Mathf.Abs((lp.y-fp.y));
            }
            if(touch.phase == TouchPhase.Ended)
            {
                angle = Mathf.Atan2((lp.x-fp.x),(lp.y-fp.y))*57.2957795f;
                
                if(angle > 60 && angle < 120 && swipeDistanceX > 40    )
                {
                    Debug.Log ("right swipe...");
                    player.Rotate(0,45,0);
                }
                if(angle > 150 || angle < -150 && swipeDistanceY > 40)
                {
                    Debug.Log ("down  swipe...");
                    player.position+=new Vector3(0,-2,0);
                }
                if(angle < -60 && angle > -120 && swipeDistanceX > 40)
                {
                    Debug.Log ("left  swipe...");
                    player.Rotate(0,-45,0);
                }
                if(angle > -30 && angle < 30 && swipeDistanceY > 40)
                {
                    Debug.Log ("up  swipe...");
                    player.position+=new Vector3(0,2,0);
                }
            }
        }
    }
}

mobile swipe (touch) controls (left, right, up, down) in unity




using UnityEngine;
using System.Collections;

public class TouchControl : MonoBehaviour {

    private Vector2 fp; // first finger position
    private Vector2 lp; // last finger position

 
    void FixedUpdate () {
        foreach(Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                fp = touch.position;
                lp = touch.position;
            }
            if (touch.phase == TouchPhase.Moved )
            {
                lp = touch.position;
            }
            if(touch.phase == TouchPhase.Ended)
            {
                if((fp.x - lp.x) > 80) // left swipe
                {
                    Debug.Log("left swipe here...");
                }
                else if((fp.x - lp.x) < -80) // right swipe
                {
                    Debug.Log("right swipe here...");
                }
                else if((fp.y - lp.y) < -80 ) // up swipe
                {
                    Debug.Log("up swipe here...");
                }
                else if((fp.y - lp.y) > 80 ) // down swipe
                {
                    Debug.Log("down swipe here...");
                }
            }
        }
    }
}

Wednesday, 9 September 2015

Screen.lockCursor Cursor.visible in unity

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {
    bool isPlaying = false;
    void Start ()
    {
        Cursor.visible = false; 

    }
    
    void Update () 
    {
        if(Input.GetKey(KeyCode.Escape))
           {
            isPlaying = !isPlaying;
            
                if(isPlaying){
                    Screen.lockCursor = false; //the cursor is unlocked from center
                    Cursor.visible = true;  //cursor is visible
                }
                else{
                    UnityEngine.Screen.lockCursor = true; //the cursor is locked to center
                    Cursor.visible = false; //cursor is invisibe
                }
            }
    }


}