- Home /
Character moving
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".
There are two approaches to it: You can either use colliders and triggers to detect if the car is at the position, or simply query the cars position in each persons update function
I try with this script.
```if (car.transform.position.z == 0.34936 || car.transform.position.z >= 0.4864168) { animator.SetBool("IsWalking", true); } else { animator.SetBool("IsWalking", false); } ``` But When I reach to that position the character is not walking. Also, I try with the in first position then the character is only walking. he does not stop at that place.
Did you debug it, aka does the "if" actually trigger? As a sidenote: its never good to check whether a float is exactly a value (z == 0.34936), as they very rarely are truly the same value. $$anonymous$$aybe the if is not actually triggered that way
But if I try it's working but it's not stopped even if the car is not that position.
https://www.youtube.com/watch?v=HgW-rAxnEqc&ab_channel=CBShowtunes
I am using this script 23.10
Answer by haliler921 · Oct 01, 2020 at 12:49 PM
After placing the cube at a point you want the car to reach, close the mesh renderer of the cube. Don't forget to add colliders to your car and your cube! To detect the collision inside the cube (car vs cube collision) write the required codes Note: If your car is a solid substance, ie your player dies when it hits the player, the code you need to write to detect the collision is the one with OnCollisionEnter. But if it's not a solid substance then your car is the OnTriggerEnter code. After checking if there is a collision, the code you need to write inside your players has this collision happened? if move on, if not, keep stopping
If you see the comment code. you will get an idea. Before I press the start button the car is in that place then the player starts to walk otherwise character in Idle position.
Once I start the game and my car will reach that position the character is still walking.
Line 40 Line 40 animator.SetFloat ("Hornizontal", rightDotProduct); shouldn't be
animator.SetFloat ("Horizontal", rightDotProduct); must be
Your answer
Follow this Question
Related Questions
Another question about Skyroads-like moving 0 Answers
Timed actions 1 Answer
Moving objects when timescale is high, which .time to use ? 2 Answers
CharacterController - moving while jumping (movement vector values reset?) 1 Answer
if (controller.isGrounded == false) (for more than) public int time = 1.0F; // do stuff 2 Answers