Tuesday, 15 September 2015

mouse scroll to zoom unity c#

using UnityEngine;
using System.Collections;

public class zoom : MonoBehaviour {

    private float zoomSpeed = 2.0f;
    
    void Update () {
        
        float scroll = Input.GetAxis("Mouse ScrollWheel");
        transform.Translate(0scroll * zoomSpeedscroll * zoomSpeedSpace.World);
    }
}



rotate a 2d arrow with mouse unity c#

About Arrow :-

  1. Create Empty Game Object.. Rename it  like 'Arrow'.
  2. Select 'Arrow'. Add Component ( Sprite Renderer ) to Inspector.
  3. Import 'Arrow.png' to Project. Select arrow.png and change Texture Type to Sprite (2D and UI)
  4. Change Pivot  to Bottom Left (According to my Sprite). You can change your pivot according to your Sprite (image). you should keep your pivot point to arrow last end.
  5. To arrange custom pivot point. You should click sprite editor and change point manually as like picture below. 

Code :-


using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {

    
    void Update () {
        var mousePos = Input.mousePosition;
        mousePos.z = 10.0f//The distance from the camera to the player object
        Vector3 lookPos  = Camera.main.ScreenToWorldPoint(mousePos);
        lookPos = lookPos - transform.position;
        float angle = Mathf.Atan2(lookPos.ylookPos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angleVector3.forward);

        Debug.Log ("== "+Input.GetAxis("Mouse X"));

    }
    
}


You should change your pivot point using sprite editor.






model rotate with the mouse drag unity c#

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {

    int speed = 12;
    float friction = 0.5f;
    float lerpSpeed = 1.5f;
    float xDeg;
    float yDeg;
    Quaternion fromRotation;
    Quaternion toRotation;
    void Start () {

    }
    
    void Update () {
        if(Input.GetMouseButton(0)) {

            xDeg -= Input.GetAxis("Mouse X") * speed * friction;
            yDeg += Input.GetAxis("Mouse Y") * speed * friction;
            fromRotation = transform.rotation;
            toRotation = Quaternion.Euler(yDeg,xDeg,0);
            transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime  * lerpSpeed);
            Debug.Log ("== "+toRotation);
        }
    }
}



how to display 3d text dynamically at unity

using UnityEngine;
using System.Collections;

public class PlayerNameScript : MonoBehaviour {



    string playerName = "SatyaDev";
    // Use this for initialization
    void Start () {

        photonView = this.GetComponent<PhotonView>();
    }
    
    // Update is called once per frame
    void Update () {
        
        GetComponent<TextMesh> ().text = playerName;

    }
    
}




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(rayout hit100))
                {
                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);
                    
                }
                
            }
        }
    }
}