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

Walking state

And the transition between the Ground and the Walking:

Transition

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.

\$\endgroup\$
  • \$\begingroup\$ You don't need to put the solution in the question. An answer is sufficient. I took the liberty to rollback your question to its previous state. Feel free to edit it if you feel something is missing. \$\endgroup\$ – S. Tarık Çetin Jul 23 '18 at 5:06
  • \$\begingroup\$ @S.TarıkÇetin I can't I forgot what was the edit before and lost it. I will not do it again next time. \$\endgroup\$ – BenBen Shmil Jul 23 '18 at 10:55
  • \$\begingroup\$ you can click at the "edited x hours ago" text below your post, above my name. it will show you the edit history. \$\endgroup\$ – S. Tarık Çetin Jul 23 '18 at 15:36
0
\$\begingroup\$
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);

            float dist = Vector3.Distance(npcs[1].position, console.position);
            if ( dist < 4f)
            {
                anim[1].SetBool("Walk", false);
            }

                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].SetBool("Aiming", true);
            anim[1].SetBool("Walk", true);
        }
    }
}

I have two soldiers(NPCS).

SOLUTION

In the Animator window of the Soldiers(NPCS) I added a Walk state with two transitions between the Grounded and Walk. And also added a new parameter name Walk type of bool:

Two transitions

The first transition is from the Grounded to the Walk and the parameter Walk is set to true. The second transition the one in the screenshot is from the Walk to the Grounded and the Walk parameter is now set to false.

In the script above I'm calling first to the Walk , true that make the Walk state to start the Walking animation. Then when the Soldier(NPCS1) distance from the console is less then 4 I start the second transition Walk , false.

The Soldier is slowly change from Walking to Idle animation.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.