using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class SpaceshipCutscene : MonoBehaviour
{
public Transform player;
public Transform[] npcs;
public Transform console;
public Camera FPSCamera;
public Camera mainCamera;
public Animator[] anim;
public float rotationSpeed = 3f;
private bool moveNpc = false;
// Use this for initialization
void Start()
{
}
private void Update()
{
if (moveNpc)
{
// Soldier 2 rotating and looking at player
Vector3 dir = player.position - npcs[0].position;
dir.y = 0; // keep the direction strictly horizontal
Quaternion rot = Quaternion.LookRotation(dir);
// slerp to the desired rotation over time
npcs[0].rotation = Quaternion.Slerp(npcs[0].rotation, rot, rotationSpeed * Time.deltaTime);
Vector3 dirToComputer = console.transform.position - npcs[1].position;
dirToComputer.y = 0;
Quaternion rot1 = Quaternion.LookRotation(dirToComputer);
npcs[1].rotation = Quaternion.Slerp(npcs[1].rotation, rot1, rotationSpeed * Time.deltaTime);
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "SpaceshipCutscene")
{
FPSCamera.enabled = false;
mainCamera.enabled = true;
moveNpc = true;
anim[0].SetTrigger("SoldierAimingTrigger");
anim[1].SetTrigger("SoldierWalkingTrigger");
}
}
}
I have two soldiers(NPCS).
The first one npcs[0] is rotating facing the player and at the same time also playing animation.
The second npcs1 should ve rotating as well facing the console and then using animation start walking to the console and to stop in front of the console. But instead when he is reaching the console he start walking around the console nonstop.
The screenshot showing the animator state for the walking:
And the transition between the Ground and the Walking:
I'm using Cinemachine and using virutal camera: CM vcam1 to Follow and Look At the first soldier. The first soldier part is working fine the problem is with the second soldier.
UPDATE:
Instead trigger I changed the parameter to bool inside the OnTriggerExit I did:
anim[1].SetBool("Walk", true);
Then in the Update if moveNpc is true I did:
if (Vector3.Distance(npcs[1].position, console.position) < 4)
{
anim[1].SetBool("Walk", false);
}
else
{
Vector3 dirToComputer = console.transform.position - npcs[1].position;
dirToComputer.y = 0;
Quaternion rot1 = Quaternion.LookRotation(dirToComputer);
npcs[1].rotation = Quaternion.Slerp(npcs[1].rotation, rot1, rotationSpeed * Time.deltaTime);
}
But if I'm doing < 5 the soldier will not move at all. If I'm doing < 4 or 3 or 2 the soldier will move forward to the console but instead to stop he will keep move through the console then will walk in circles around the console nonstop.