Skip to content

Instantly share code, notes, and snippets.

@mhebrard
Created December 7, 2016 15:00
Show Gist options
  • Save mhebrard/cc60786992660202b395010a64ccfba4 to your computer and use it in GitHub Desktop.
Save mhebrard/cc60786992660202b395010a64ccfba4 to your computer and use it in GitHub Desktop.
Unity - Move the camera
**Set up the camera**
* Import the VR Samples Asset ( https://www.assetstore.unity3d.com/en/#!/content/51519 )
* Open the scene InteractiveItem ( Assets/VRSampleScenes/Scenes/Examples/InteractiveItem )
* Selelct the MainCamera Object and copy it ( right click - copy )
* Open your scene and paste it ( right click- paste )
* Create one Empty object ( name it CameraWrapper )
* Drag the Main Camera as child of CameraWrapper
* On Camera Wrapper, Add a Componenent : the script MoveToward ( code bellow )
**Set up a target object**
* Create an object the Camera will pane to. I call it Target. (just a cube will do)
* On Target, be sure to have a Component Collider
* On Target, Add a Componenent : the script VRInteractiveItem ( from VR Samples Asset )
* On Target, Add a Componenent : the script DragObject ( code bellow )
* On Target, Add a Componenent : the script FadeNLoad ( code bellow )
* On Target, In DragObject Component, Drag and Drop Target in Interactibe Item attribute
* On Target, In DragObject Component, Drag and Drop Target in Target attribute
* On Target, In DragObject Component, Drag and Drop CameraWrapper in Source Item attribute
* On Target, In FadeNLoad Component, Drag and Drop Target in Interactibe Item attribute
* On Target, In FadeNLoad Component, Drag and Drop MainCamera in Camera To Fade attribute **(Not the wrapper, the camera itself)**
* On Target, In FadeNLoad Component, Write the name of a scene in Scene to Load attribute
**Action**
* Play your scene
* Point the target object
* Click on the remote main button
* You must move toward the target object then fade out to black, then fade in in your new scene
using UnityEngine;
using System.Collections;
using VRStandardAssets.Utils;
namespace VR.ick
{
// On click event on the Interactive Item
// The Source Item move to the Target
// using MoveToward function
public class DragObject : MonoBehaviour
{
[SerializeField] private VRInteractiveItem m_InteractiveItem;
[SerializeField] private Transform m_TargetItem;
[SerializeField] private MoveToward m_SourceItem;
private void OnEnable()
{
m_InteractiveItem.OnOver += HandleOver;
m_InteractiveItem.OnOut += HandleOut;
m_InteractiveItem.OnClick += HandleClick;
m_InteractiveItem.OnDoubleClick += HandleDoubleClick;
}
private void OnDisable()
{
m_InteractiveItem.OnOver -= HandleOver;
m_InteractiveItem.OnOut -= HandleOut;
m_InteractiveItem.OnClick -= HandleClick;
m_InteractiveItem.OnDoubleClick -= HandleDoubleClick;
}
private void HandleOver()
{
}
private void HandleOut()
{
}
private void HandleClick()
{
m_SourceItem.target = m_TargetItem;
m_SourceItem.come = true;
}
private void HandleDoubleClick()
{
}
}
}
using UnityEngine;
using System.Collections;
using VRStandardAssets.Utils;
using UnityEngine.SceneManagement;
namespace VR.ick
{
// on click on the interactive Item
// main camera fade out
// and Next Scene is loaded
public class FadeNLoad : MonoBehaviour
{
[SerializeField] private VRInteractiveItem m_InteractiveItem;
[SerializeField] private VRCameraFade m_CameraToFade;
[SerializeField] private string m_SceneToLoad;
private void OnEnable()
{
m_InteractiveItem.OnOver += HandleOver;
m_InteractiveItem.OnOut += HandleOut;
m_InteractiveItem.OnClick += HandleClick;
m_InteractiveItem.OnDoubleClick += HandleDoubleClick;
}
private void OnDisable()
{
m_InteractiveItem.OnOver -= HandleOver;
m_InteractiveItem.OnOut -= HandleOut;
m_InteractiveItem.OnClick -= HandleClick;
m_InteractiveItem.OnDoubleClick -= HandleDoubleClick;
}
private void HandleOver()
{
}
private void HandleOut()
{
}
private void HandleClick()
{
StartCoroutine (CallFade());
}
private void HandleDoubleClick()
{
}
private IEnumerator CallFade()
{
// If the camera is already fading, ignore.
if (m_CameraToFade.IsFading)
yield break;
// Wait for the camera to fade out.
yield return StartCoroutine(m_CameraToFade.BeginFadeOut(true));
// Load the level.
SceneManager.LoadScene(m_SceneToLoad, LoadSceneMode.Single);
}
}
}
using UnityEngine;
using System.Collections;
public class MoveToward : MonoBehaviour {
[SerializeField] public Transform target;
[SerializeField] public float speed;
[SerializeField] public bool come;
void Update () {
if (come) { //a object drag the cam
float step = speed * Time.deltaTime;
Vector3 nextPosition = Vector3.MoveTowards(transform.position, target.position, step);
// if cam is arrived, stop moving
if (nextPosition == transform.position) {
come = false;
} else {
transform.position = nextPosition;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment