Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Main : MonoBehaviour {
- //Constants
- protected const float GAZE_LENGTH = 100;
- protected const float MAX_GAZE_DURATION = 2f;
- //Data
- protected GameObject mObject;
- protected float mGazeTime;
- void Start () {
- //Initialize
- mObject = null;
- mGazeTime = 0;
- }
- void Update () {
- //Raycast from eye/camera, check if it hit something
- RaycastHit hit;
- if (Physics.Raycast(new Ray(transform.position, transform.forward), out hit, GAZE_LENGTH)) {
- //If there's an object that was seen in the previous frame
- if (mObject != null) {
- //If the same as current object
- if (mObject == hit.collider.gameObject) {
- //Increase gaze time
- mGazeTime += Time.deltaTime;
- if (mGazeTime > MAX_GAZE_DURATION) mGazeTime = MAX_GAZE_DURATION;
- //Recolor
- float color = (MAX_GAZE_DURATION - mGazeTime) / MAX_GAZE_DURATION;
- ColorObject(mObject, new Color(1f, color, color));
- } else {
- //Reset old object since it's different
- ColorObject(mObject, Color.white);
- mGazeTime = 0;
- }
- }
- //Save the object to variable
- mObject = hit.collider.gameObject;
- //If object exists and mouse is clicked
- if (mObject != null && Input.GetMouseButtonUp(0)) {
- //Move object elsewhere
- mObject.transform.position = new Vector3(Random.Range(-8, 8f), Random.Range(-2f, 2f), Random.Range(0, 3f));
- }
- } else {
- //If there was an object
- if (mObject != null) {
- //Reset
- ColorObject(mObject, Color.white);
- mGazeTime = 0;
- }
- //No object was seen
- mObject = null;
- }
- }
- protected void ColorObject(GameObject gameObject, Color color) {
- //Skip if no object
- if (gameObject == null) return;
- //Set material color
- Renderer objectRenderer = gameObject.GetComponent<Renderer>();
- if (objectRenderer != null) objectRenderer.material.color = color;
- }
- }
Add Comment
Please, Sign In to add comment