Saturday, 17 January 2015

Contoh Script rotasi pada UNity3D

Rotasi.cs
using UnityEngine;
using System.Collections;

public class rotasi : MonoBehaviour {
    
                private Transform mBajuTransform = null;
                private Transform mBaju2Transform = null;
                private Transform mBaju3Transform = null;
                private Transform mBaju4Transform = null;
    // Use this for initialization
    void Start ()
    {
                                GameObject baju = GameObject.Find("baju");
        if (baju != null)
        {
            Debug.Log("Baju found");
            mBajuTransform = baju.transform;
        }
                                GameObject baju2 = GameObject.Find("baju2");
        if (baju2 != null)
        {
            Debug.Log("Baju found");
            mBaju2Transform = baju2.transform;
        }
                                GameObject baju3 = GameObject.Find("baju3");
        if (baju3 != null)
        {
            Debug.Log("Baju found");
            mBaju3Transform = baju3.transform;
        }
                                GameObject baju4 = GameObject.Find("baju4");
        if (baju4 != null)
        {
            Debug.Log("Baju found");
            mBaju4Transform = baju4.transform;
        }
    }
    
    // Update is called once per frame
    void Update ()
    {
    }
    
    void OnGUI()
    {
                                if (mBajuTransform == null) return;
                                if (mBaju2Transform == null) return;
                                if (mBaju3Transform == null) return;
                                if (mBaju4Transform == null) return;
                               
        if (GUI.Button(new Rect(730,470,105,40), "<size=16>Rotate Left</size>"))
        {
                                                Quaternion oldRotation = mBajuTransform.localRotation;
                                                Quaternion oldRotation2 = mBaju2Transform.localRotation;
                                                Quaternion oldRotation3 = mBaju3Transform.localRotation;
                                                Quaternion oldRotation4 = mBaju4Transform.localRotation;
                                               
                                                mBajuTransform.localRotation = Quaternion.AngleAxis(10, Vector3.forward) * oldRotation; 
                                                mBaju2Transform.localRotation = Quaternion.AngleAxis(10, Vector3.forward) * oldRotation; 
                                                mBaju3Transform.localRotation = Quaternion.AngleAxis(10, Vector3.forward) * oldRotation; 
                                                mBaju4Transform.localRotation = Quaternion.AngleAxis(10, Vector3.forward) * oldRotation;
        }
        if (GUI.Button(new Rect(840,470,105,40), "<size=16>Rotate Right</size>"))
        {
                                                Quaternion oldRotation = mBajuTransform.localRotation;
                                                Quaternion oldRotation2 = mBaju2Transform.localRotation;
                                                Quaternion oldRotation3 = mBaju3Transform.localRotation;
                                                Quaternion oldRotation4 = mBaju4Transform.localRotation;
           
                                                mBajuTransform.localRotation = Quaternion.AngleAxis(10, -Vector3.forward) * oldRotation;   
                                                mBaju2Transform.localRotation = Quaternion.AngleAxis(10, -Vector3.forward) * oldRotation;   
                                                mBaju3Transform.localRotation = Quaternion.AngleAxis(10, -Vector3.forward) * oldRotation; 
                                                mBaju4Transform.localRotation = Quaternion.AngleAxis(10, -Vector3.forward) * oldRotation; 
                                               
        }
        if (GUI.Button(new Rect(840,40,90,40), "<size=16>Zoom Out</size>"))
        {
            Vector3 localPos = mBajuTransform.localPosition;
                                                Vector3 localPos2 = mBaju2Transform.localPosition;
                                                Vector3 localPos3 = mBaju3Transform.localPosition;
                                                Vector3 localPos4 = mBaju4Transform.localPosition;
            localPos.y += 0.1f;
           
                                                mBajuTransform.localPosition = localPos;
                                                mBaju2Transform.localPosition = localPos;
                                                mBaju3Transform.localPosition = localPos;
                                                mBaju4Transform.localPosition = localPos;
        }
        if (GUI.Button(new Rect(840,100,90,40), "<size=16>Zoom In</size>"))
        {
            Vector3 localPos = mBajuTransform.localPosition;
                                                Vector3 localPos2 = mBaju2Transform.localPosition;
                                                Vector3 localPos3 = mBaju3Transform.localPosition;
                                                Vector3 localPos4 = mBaju4Transform.localPosition;
            localPos.y -= 0.1f;
           
                                                mBajuTransform.localPosition = localPos;
                                                mBaju2Transform.localPosition = localPos;
                                                mBaju3Transform.localPosition = localPos;
                                                mBaju4Transform.localPosition = localPos;
        }
    }
}

Download script Link1 / Link2

No comments:

Post a Comment