Hi,
I have 2 characters and one car. I want to do when the car reaches a certain z position like -0.02 or 0.02. the characters need to start walking. Before that character will be stopped at that place. here is my full code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.VFX;
public class CharaterNavigationCntroller : MonoBehaviour
{
public float movementSpeed = 0.5f;
public float rotationSpeed = 120f;
public float stopDistance = 1f;
public Vector3 destination;
public Animator animator;
public bool reachedDestination;
private Vector3 lastPosition;
Vector3 velocity;
public Transform car;
//0.34936 last point: 0.4864168
private void Awake()
{
movementSpeed = Random.Range(0.1f,0.2f);
animator = GetComponent<Animator>();
}
void Update()
{
if (car.transform.position.z >= 0.34936f)
{
animator.SetBool("IsWalking", true);
if (transform.position != destination)
{
Vector3 destinationDirection = destination - transform.position;
destinationDirection.y = 0;
float destinationDistance = destinationDirection.magnitude;
if (destinationDistance >= stopDistance)
{
reachedDestination = false;
Quaternion targetRoation = Quaternion.LookRotation(destinationDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRoation, rotationSpeed * Time.deltaTime);
transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
}
else
{
reachedDestination = true;
}
velocity = (transform.position - lastPosition) / Time.deltaTime;
velocity.y = 0;
var velocityMagnitude = velocity.magnitude;
velocity = velocity.normalized;
var fwdDotProduct = Vector3.Dot(transform.forward, velocity);
var rightDotProduct = Vector3.Dot(transform.right, velocity);
animator.SetFloat("Hornizontal", rightDotProduct);
animator.SetFloat("Forward", fwdDotProduct);
}
//lastPosition = transform.position;
else
{
reachedDestination = true;
}
}
else
{
animator.SetBool("IsWalking", false);
}
}
public void SetDestination(Vector3 destination)
{
this.destination = destination;
reachedDestination = false;
}
}
Does anyone have an idea how can I approach this idea in unity?
“All characters are made in Fuse and mixamo software”.