First of all, greetings Unity community.
This is my first post here.
I’m a total beginner. I tried making a showroom, adapting the scripts from the “Stealth Project Tutorial” + what I could gather from the Documentation.
I want to make an Art-Gallery, where NPCs walk around staring at pictures.
The NPCs walk through the assigned wayPoints. When they hit the picture collider, they stay for a couple of seconds looking at the picture.
I’ve got everything working so far, except for them looking at the different pictures.
I’ve tried different solutions suggested here (unityAnswers), but so far nothing has worked.
I’d appreciate any help. Here is my code so far (you can ignore the Patrolling() function, since it was taken from the Stealth Project and should be correct):
public class NPCAI : MonoBehaviour
{
public bool pictureInSight;
public int StartAdmiration;
public int StopAdmiration;
public float angleBetween = 0.5f;
public float patrolSpeed = 2f;
public float patrolWaitTime = 1f;
public Transform[] patrolWayPoints;
//I've tried it with Transform and Gameobject, neither works
public Transform trans4;
public GameObject target1;
public GameObject target2;
public GameObject target3;
public GameObject target4;
private NavMeshAgent nav;
private float patrolTimer;
private int wayPointIndex;
private int targetIndex;
void Awake()
{
nav = GetComponent<NavMeshAgent>();
}
void Update()
{
if (pictureInSight == false)
{
Patrolling();
}
}
void Patrolling()
{
nav.speed = patrolSpeed;
if(nav.remainingDistance < nav.stoppingDistance)
{
patrolTimer += Time.deltaTime;
if(patrolTimer >= patrolWaitTime)
{
// ... increment the wayPointIndex.
if(wayPointIndex == patrolWayPoints.Length - 1)
wayPointIndex = 0;
else
wayPointIndex++;
// Reset the timer.
patrolTimer = 0;
}
}
else
patrolTimer = 0;
nav.destination = patrolWayPoints[wayPointIndex].position;
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject == target1)
{
pictureInSight = true;
Debug.Log ("Collision with target1");
//This was my first try.
//The result is, that it rotates my NPC at a weird angle.[watch photo1]
/*transform.LookAt(trans1);*/
//I found this solution in the Forums. [watch photo2]
//The result, looks in a random direction, but NPC doesn't have a weird angle anymore
//if I do the same with x and z it makes no difference
var targetPos1 = target1.transform.position;
targetPos1.y = transform.position.y;
transform.LookAt(targetPos1);
Vector3 destination = target1.transform.position - transform.position;
angleBetween = Vector3.Angle(transform.forward, destination);
int admirationTime = (Random.Range(StartAdmiration, StopAdmiration));
Debug.Log ("Waiting time is");
Debug.Log(admirationTime);
Invoke("ResetValues", admirationTime);
}
if (other.gameObject == target2)
{
pictureInSight = true;
Debug.Log ("Collision with picture");
var targetPos2 = target2.transform.position;
targetPos2.y = transform.position.y;
targetPos2.z = transform.position.z;
transform.LookAt(targetPos2);
Vector3 destination = target2.transform.position - transform.position;
angleBetween = Vector3.Angle(transform.forward, destination);
int admirationTime = (Random.Range(StartAdmiration, StopAdmiration));
Debug.Log ("Waiting time is");
Debug.Log(admirationTime);
Invoke("ResetValues", admirationTime);
}
if (other.gameObject == target3)
{
pictureInSight = true;
Debug.Log ("Collision with picture");
transform.LookAt(target3.transform.position);
Vector3 destination = target3.transform.position - transform.position;
angleBetween = Vector3.Angle(transform.forward, destination);
int admirationTime = (Random.Range(StartAdmiration, StopAdmiration));
Debug.Log ("Waiting time is");
Debug.Log(admirationTime);
Invoke("ResetValues", admirationTime);
}
if (other.gameObject == target4)
{
pictureInSight = true;
Debug.Log ("Collision with target4");
transform.LookAt(new Vector3(trans4.position.x, transform.position.y, trans4.position.z));
Vector3 destination = target4.transform.position - transform.position;
angleBetween = Vector3.Angle(transform.forward, destination);
int admirationTime = (Random.Range(StartAdmiration, StopAdmiration));
Debug.Log ("Waiting time is");
Debug.Log(admirationTime);
Invoke("ResetValues", admirationTime);
}
}
void ResetValues()
{
pictureInSight = false;
Patrolling ();
//transform.LookAt (Vector3.zero);
}
}
PS: I know I should make arrays instead of drag&drop each target, but I tried and failed miserably. Thank you in advance.
Best Regards.