- Home /
Camera move to location tagged object
Hello guys,
I am learning the basics of programming (Im a visual artist) and currently I am trying to make a demo with the Oculus Rift. I am trying to create a hands free movement in an environment just as this demo (https://www.youtube.com/watch?v=Ad-Aa3ATbyE) Where at 0:30 seconds the person is looking at an object (or tag) and after a while the camera wil move to that location. This is what I am trying to recreate. I started with the Find Closest Enemy example, but I am not sure if I am on the right track.
If anything is unclear, let me know and I will try to clear things up.
Help would be appreciated!
/EDIT/ Here is the code that I have so far:
using UnityEngine;
using System.Collections;
// Find the name of the closest enemy
GameObject FindClosestEnemy() {
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Enemy");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos) {
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
}
Answer by Rob1508 · Jul 31, 2015 at 12:28 PM
I think the best solution would be to cast a ray from your camera into the scene. If you put your "lookable" objects onto one layer, you can use this as your mask layer for the raycast. The reason for this is that the ray just hits objects that you can move to and does not get disturbed by other objects that might be between your camera and a lookable object.
Once you have a hit you can do some visual effects like highlighting and so on. After a short period of time, lets say 1 second, you can check if the ray is still hitting that particular object. If this is true, move your camera towards that object.
I've created a sample scene to demonstrate this. The only thing you have to do is import Unity's standard assets to get a FPS Controller for the mouse look script that is attached.
https://dl.dropboxusercontent.com/u/59805598/Lookable.zip
Cheers! Rob
Thank you so much for the scene! Really helpful and I will dig through everything so I will understand it better. Thanks again!
Thanks again for the scene, I got it working with the Oculus and it helps a lot with understanding rays and the layers.
Your answer
Follow this Question
Related Questions
Drag Script Not Working 1 Answer
Move from one object to another 0 Answers
Can't Return the Tag of a Child Object 4 Answers
Destroy objects by clicking on them 1 Answer
How can i find a player prefab and add them to a list?????? 1 Answer