- Home /
Trigger move object only when player isn't looking at object
Hi!
So I've gotten as far as getting a trigger that moves a target when a Player enters it. Which is great!
public Transform OBJ;
public Vector3 OBJTargetOffset;
public float raiseTime = 2;
Vector3 OBJStart;
Vector3 OBJTarget;
public GameObject player;
void Start()
{
OBJStart = OBJ.position;
OBJTarget = OBJStart + OBJTargetOffset;
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
StartCoroutine(RaiseOBJ(raiseTime));
}
}
IEnumerator RaiseOBJ(float time)
{
float elapsedTime = 0;
while(elapsedTime < time)
{
OBJ.position = Vector3.Lerp(OBJStart, OBJTarget, elapsedTime / time);
elapsedTime += Time.deltaTime;
yield return null;
}
OBJ.position = OBJTarget;
}
void Update () {
}
}
But for a little more flexibility I'd like to find out how to have the trigger only move the target when it isn't (or is) in the Players FOV.
Something with
Transform cameraTransform = null;
cameraTransform = GameObject.FindWithTag("MainCamera").transform;
And a ray
Vector3 rayDirection = cameraTransform.TransformDirection(Vector3.forward);
Vector3 rayStart = cameraTransform.position + rayDirection;
Would do the job, but I just can't seem to get it.
Thanks in advance!
Hyper
So I've been messing around a bit and haven't found anything good yet.
Not sure why this does't work
void OnTriggerEnter(Collider other)
{
if (renderer.isVisible && other.tag == "Player")
{
StartCoroutine(RaiseOBJ(raiseTime));
}
}
With this code, nothing happens.
Answer by Hyperactive · Jun 02, 2014 at 10:05 AM
Using
renderer.IsVisible
Might do the trick!
Going to try and combine it with my script now.
Hyper
Your answer
Follow this Question
Related Questions
Use OnTriggerEnter during a transform ! 0 Answers
Help With Player Decrease Enemy Health!,Need Help with Character 1 Answer
RTS Controller? 0 Answers
Player have to stop when hit a corner 0 Answers
enemy Raycast cone detection 0 Answers