using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class SpaceshipCutscene : MonoBehaviour
{
public Transform player;
public Transform npc;
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)
{
npc.rotation = Quaternion.RotateTowards(npc.rotation, player.rotation, Time.deltaTime * rotationSpeed);
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.name == "Horizontal_Doors_Kit")
{
FPSCamera.enabled = false;
mainCamera.enabled = true;
moveNpc = true;
anim.SetTrigger("SoldierAimingTrigger");
}
}
}
At this line:
npc.rotation = Quaternion.RotateTowards(npc.rotation, player.rotation, Time.deltaTime * rotationSpeed);
On screenshot on the right the player (FPSController) And the FPSController Inspector Rotation:
But you can see in the same screenshot that the soldier(Space_Soldier_A (2)) is rotating slowly to the other side and stop rotating in the end facing the spaceship on the back.
I want the soldier to rotate slowly and stop when facing the player(FPSController)
This is a screenshot of the soldier in the end when he stop rotating: He is facing the opposite side of the player:
What I tried now is to create a new script attached to the npc(Soldier) I want to rotate slowly facing the player(FPSController):
What I didn't try ? Everything. This is the latest try:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
public Transform player;
public float rotationSpeed = 3f;
private Vector3 direction;
private Quaternion toRotation;
// Use this for initialization
void Start ()
{
//direction = player.position - transform.position;
//toRotation = Quaternion.FromToRotation(transform.forward, direction);
}
// Update is called once per frame
void Update ()
{
if (SpaceshipCutscene.moveNpc == true)
{
Quaternion q = Quaternion.LookRotation(transform.position - player.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, rotationSpeed * Time.deltaTime);
//Vector3 dir = player.position - transform.position;
//transform.rotation = Quaternion.LookRotation(dir, Vector3.up);
//transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, rotationSpeed * Time.time);
}
}
}
Again it's rotating the soldier slowly the Space_Soldier_A (2) but to the other opposite direction and not to the player direction not facing the player.
I found that if I only make in the Update simple:
transform.LookAt(player);
It will facing looking at the player but this is without slowly rotating. It seems like every rotation i'm trying to do with a speed rotating the soldier to the opposite direction.
In the first script I changed it: And did the moveNpc to be public static:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class SpaceshipCutscene : MonoBehaviour
{
public Transform player;
public Transform npc;
public Camera FPSCamera;
public Camera mainCamera;
public Animator anim;
public float rotationSpeed = 3f;
public static bool moveNpc = false;
// Use this for initialization
void Start()
{
}
private void Update()
{
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.name == "Horizontal_Doors_Kit")
{
FPSCamera.enabled = false;
mainCamera.enabled = true;
moveNpc = true;
anim.SetTrigger("SoldierAimingTrigger");
}
}
}
I can't figure out yet why the soldier is rotating slowly to the opposite direction. Maybe the Cinemachine and virtual camera making the problem ? Maybe since the player(FPSController) have a Rigidbody component it's making the problem ?
This is a screenshot of the player FPSController inspector: