- Home /
rotate object when player not looking
Hi,
I want to make the object rotate when the player is not looking at it, i already have the script but it's not working
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookingAtPlayer: MonoBehaviour
{
public Transform player;
void OnBecamevisible()
{
}
void OnBecameinvisible()
{
Vector3 playerPosition = new Vector3 (player.transform.position.x, transform.position.y, player.transform.position.z);
transform.LookAt (playerPosition);
}
}
reference
https://youtu.be/3GG3RMiFPOA?t=1m7s
https://youtu.be/9utuLKGOghM?t=24m8s
Answer by The0Warrior · Apr 09, 2018 at 11:06 PM
I think i fix it, i just had to identify the gameobject itself too
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookingAtPlayer: MonoBehaviour
{
MeshFilter meshFilter;
public GameObject mesh;
public Transform player;
void OnBecameVisible()
{
}
void OnBecameInvisible()
{
Vector3 playerPosition = new Vector3 (player.transform.position.x, transform.position.y, player.transform.position.z);
transform.LookAt (playerPosition);
}
}
if you have any better answer please add them too
Your answer
Follow this Question
Related Questions
what are the basic rules off making a lan server 1 Answer
How can I slow down to stop(HumanoidIdle) character when getting close to a object using timeline ? 0 Answers
How do I play gameobjects in my scene from one script / Script is not working 0 Answers
How can i make a text changed event in EditorGUILayout.TextField ? 1 Answer
When moving object, should i use transform.position or a animation clip i recorded ? 0 Answers