Unity3D
วันศุกร์ที่ 2 ธันวาคม พ.ศ. 2559
Shooting โค้ดยิงกระสุนนน
using UnityEngine;
using System.Collections;
public class Shootiing : MonoBehaviour {
public GameObject bulletPrefab;
public float bulletImpulse = 100.0f;
public GameObject CameraObject;
// Use this for initialization
void Start () {
}
public void FireOut()
{
GameObject theBullet = (GameObject)Instantiate (bulletPrefab , CameraObject.transform.position , CameraObject.transform.rotation);
theBullet.GetComponent<Rigidbody> ().AddForce ( CameraObject.transform.forward * bulletImpulse , ForceMode.Impulse);
}
public void NotFireOut()
{
}
// Update is called once per frame
void Update () {
if(Input.GetButtonDown("Fire1"))
{
FireOut ();
}
}
}
EnemyScript VR มองแล้วยยิงศัตรูอัตโนมัติ
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using VRStandardAssets.Utils;
namespace VRStandardAssets.Menu
{
public class EnamyScript : MonoBehaviour {
public event Action<EnamyScript> OnEnamySelected; // This event is triggered when the selection of the button has finished.
[SerializeField] private VRCameraFade m_CameraFade; // This fades the scene out when a new scene is about to be loaded.
[SerializeField] private SelectionRadial m_SelectionRadial; // This controls when the selection is complete.
[SerializeField] private VRInteractiveItem m_InteractiveItem; // The interactive item for where the user should click to load the level.
// ************************** SUA modified add **********************************
public delegate void ActivateEnemyDelegate(int paramIndex);
public ActivateEnemyDelegate activateEnamyDelegate;
[SerializeField] private int paramIndex = -1;
public Shootiing shooting;
private bool m_GazeOver; // Whether the user is looking at the VRInteractiveItem currently.
private void OnEnable ()
{
m_InteractiveItem.OnOver += HandleOver;
m_InteractiveItem.OnOut += HandleOut;
m_SelectionRadial.OnSelectionComplete += HandleSelectionComplete;
// Additional Modify
m_InteractiveItem.OnOver += m_SelectionRadial.HandleDown;
m_InteractiveItem.OnOut += m_SelectionRadial.HandleUp;
}
private void OnDisable ()
{
m_InteractiveItem.OnOver -= HandleOver;
m_InteractiveItem.OnOut -= HandleOut;
m_SelectionRadial.OnSelectionComplete -= HandleSelectionComplete;
// Additional Modify
m_InteractiveItem.OnOver += m_SelectionRadial.HandleDown;
m_InteractiveItem.OnOut += m_SelectionRadial.HandleUp;
}
private void HandleOver()
{
shooting.FireOut ();
m_GazeOver = true;
}
private void HandleOut()
{
shooting.NotFireOut();
m_GazeOver = false;
}
private void HandleSelectionComplete()
{
// If the user is looking at the rendering of the scene when the radial's selection finishes, activate the button.
if(m_GazeOver)
StartCoroutine (ActivateWithEnamy());
}
private IEnumerator ActivateWithEnamy()
{
// If the camera is already fading, ignore.
if (m_CameraFade.IsFading)
yield break;
// If anything is subscribed to the OnButtonSelected event, call it.
if (OnEnamySelected != null)
OnEnamySelected(this);
Debug.Log ("Param Index : " + paramIndex);
}
}
}
การทำกล้องหมุน 360 องศา โดยใช้เมาส์
using UnityEngine;
using System.Collections;
public class FirstPersonController : MonoBehaviour {
public float mouseSensitivity = 2.0f;
public float upDownRenge = 60.0f;
private float verticalRotation = 0.0f;
public GameObject leftCamera;
public GameObject rightCamera;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float rotationLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity ;
transform.Rotate (0,rotationLeftRight ,0);
verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity ;
verticalRotation = Mathf.Clamp (verticalRotation , -upDownRenge , upDownRenge);
leftCamera.transform.localRotation = Quaternion.Euler (verticalRotation , 0 , 0);
rightCamera.transform.localRotation = Quaternion.Euler (verticalRotation , 0 , 0);
}
}
using System.Collections;
public class FirstPersonController : MonoBehaviour {
public float mouseSensitivity = 2.0f;
public float upDownRenge = 60.0f;
private float verticalRotation = 0.0f;
public GameObject leftCamera;
public GameObject rightCamera;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float rotationLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity ;
transform.Rotate (0,rotationLeftRight ,0);
verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity ;
verticalRotation = Mathf.Clamp (verticalRotation , -upDownRenge , upDownRenge);
leftCamera.transform.localRotation = Quaternion.Euler (verticalRotation , 0 , 0);
rightCamera.transform.localRotation = Quaternion.Euler (verticalRotation , 0 , 0);
}
}
วันศุกร์ที่ 25 พฤศจิกายน พ.ศ. 2559
Go to new position with transform.localPosition แบบเป๊ะๆ
การใช้
currentMenuPanel.transform.localPosition = new Vector3 (oldPosition+3503.0f , -54.0f , 0.0f);
วันพฤหัสบดีที่ 24 พฤศจิกายน พ.ศ. 2559
Unity 3D : สร้าง class extension ให้ unity
public static class Extensions
{
public static void SetTransparency(this UnityEngine.UI.Image p_image, float p_transparency)
{
if (p_image != null)
{
UnityEngine.Color __alpha = p_image.color;
__alpha.a = p_transparency;
p_image.color = __alpha;
}
}
}
ใส่ โค้ดนี้ลงไปใน script ที่ต้องการใช้ extension
วิธีการใช้งาน
menuButtons [0].gameObject.GetComponent<Image> ().SetTransparency (0);
วันพุธที่ 23 พฤศจิกายน พ.ศ. 2559
Unity 3D : rotation same as another
rotation same as another
using UnityEngine;
using System.Collections;
public class SameRotation : MonoBehaviour {
public Camera camera;
void Start () {
}
void Update () {
transform.localRotation = camera.transform.localRotation;
}
}
Unity 3D : Go to new position with Corutine
Go to new position with Corutine
using System.Collections;
public class GoToOtherPosition : MonoBehaviour {
public Vector3 d;
int i = 0;
void Start () {
StartCoroutine ( MoveTowardsTarget() );
}
IEnumerator MoveTowardsTarget() {
for(int i = 0 ; i <= 30 ; i++)
{
//yield return new WaitForSeconds(0.005f);
yield return null;
transform.position = new Vector3 (i, 0, 0);
}
}
}
สมัครสมาชิก:
บทความ (Atom)