2
\$\begingroup\$
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:

FPSController

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:

Soldier

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:

FPSController

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

I'm not really into Unity, but your Quaternion.RotateTowards function seems to be some kind of SLERP (Spherical Interpolation) which makes the npc's rotation to become identical to the player's rotation. This results in making the npc face in the same direction, that the player faces. To make them face each other you may try "rotate towards" the negative of the player's rotation (i.e. -player.rotation).

EDIT:

Just a small edit. I've seen, Unity integrates a SLERP function in it's Quaternion Class (see here).

So if your

transform.lookAt(player)

works, you could probably use it to achieve the effect you want. You'll want to do something like this:

Quaternion destOrientation;
destOrientation.lookAt(player.position);
npc.orientation = Quaternion.Slerp(npc.orientation, destOrientation, deltaTime);

Just be careful with the deltaTime, due to both Quaternions changing over time this may yield unexpected results (a fixed timestep, although, may be appropiate).

\$\endgroup\$
3
  • \$\begingroup\$ This two lines are working: var q = Quaternion.Inverse(player.rotation); transform.rotation = Quaternion.RotateTowards(transform.rotation, q, Time.deltaTime * rotationSpeed); I'm still not sure why the rotation of the soldier is opposite. Could be a problem with the soldier model it self ? But inversing the rotation is working. Strange but working. \$\endgroup\$
    – Daniel Lip
    Jun 20, 2018 at 13:10
  • 1
    \$\begingroup\$ @BenziAvrumi Did you check the axis of the model and make sure they are the same as for the space ? \$\endgroup\$
    – medukrin
    Jun 21, 2018 at 8:45
  • \$\begingroup\$ I found a working solution for what I needed: pastebin.com/idhKC9Pu \$\endgroup\$
    – Daniel Lip
    Jun 21, 2018 at 20:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .