void NavAnimSetup( ) { float speed; float angle; if (enemySight.playerInSight) { speed = 0f; angle = FindAngle(transform.forward, player.position - transform.position, transform.up); } else { speed = Vector3.Project(nav.desiredVelocity, transform.forward).magnitude; angle = FindAngle(transform.forward, nav.desiredVelocity, transform.up); if (Mathf.Abs(angle) < deadZone) { transform.LookAt(transform.position + nav.desiredVelocity); angle = 0f; } } animSetup.Setup(speed, angle); }
void NavAnimSetup() { // Create the parameters to pass to the helper function. float speed; float angle; // If the player is in sight... if (enemySight.playerInSight) { // ... the enemy should stop... speed = 0f; // ... and the angle to turn through is towards the player. angle = FindAngle(transform.forward, player.position - transform.position, transform.up); } else { // Otherwise the speed is a projection of desired velocity on to the forward vector... speed = Vector3.Project(nav.desiredVelocity, transform.forward).magnitude; // ... and the angle is the angle between forward and the desired velocity. angle = FindAngle(transform.forward, nav.desiredVelocity, transform.up); // If the angle is within the deadZone... if (Mathf.Abs(angle) < deadZone) { // ... set the direction to be along the desired direction and set the angle to be zero. transform.LookAt(transform.position + nav.desiredVelocity); angle = 0f; } } // Call the Setup function of the helper class with the given parameters. animSetup.Setup(speed, angle); }
void NavAnimSetup() { float speed; float angle; //enemyがユーザを見つけたら if (enemySight.playerInSight) { //enemyga停止し,ユーザとの角度を計算する speed = 0; angle = FindAngle(transform.forward, player.position - transform.position, transform.up); } else { //enemyがユーザを見つけていない、 //ベクトルの投影でスピードを決める(間違った方向に行かないため) //magnitudeはベクトルの長さ speed = Vector3.Project(nav.desiredVelocity, transform.forward).magnitude;//desiredVelocityは回避行動による潜在的な移動も含むエージェントの期待速度(読み取り専用) //向かう先とforwardの角度を計算 angle = FindAngle(transform.forward, nav.desiredVelocity, transform.up); //enemyの回転に余裕の時間を設定したため、enemyは正しい方向までに回転後、止まらずさらに回転する //結果enmeyは蛇行する //これを防ぐため,enemyの顔向きと正しい方向間の角度がdeazoneより小さい時、navで回転をコントロール if (Mathf.Abs(angle) < deadZone) { //enemyの前進方向をnavでコントロール(animatorではない) transform.LookAt(transform.position + nav.desiredVelocity); //角度を0 angle = 0; } } //speedとangleをanimSetupに渡す animSetup.Setup(speed, angle); }
void NavAnimatorSetup() //给AnimatorSetup传递参数 { float speed; //速度参数 float angle; if (enemySight.playerInSight) //如果玩家在敌人的视野当中 { speed = 0; //玩家不需要移动 angle = FindAngle(transform.forward, player.position - transform.position, transform.up); } else//玩家不在敌人的视野当中,计算寻路代理组件的需求速度在敌人正前方的投影作为速度 { speed = Vector3.Project(nav.desiredVelocity, transform.forward).magnitude;//投影函数 //nav.desiredVelocity:寻路组件的需求速度 //transform.forward:正前方的投影 //magnitude:只取投影的长度作为速度 angle = FindAngle(transform.forward, nav.desiredVelocity, transform.up); if (Mathf.Abs(angle) < deadZone) { transform.LookAt(transform.position + nav.desiredVelocity);//直接达到目标的旋转 angle = 0; } } animSetup.Setup(speed, angle); }
void AnimSetup() { //float speed = Vector3.Project(nav.desiredVelocity, transform.forward).magnitude; //float angle = FindAngle(transform.forward, nav.desiredVelocity, transform.up); float speed = Vector3.Project(unitController.moveDirection, transform.forward).magnitude; float angle = FindAngle(transform.forward, unitController.moveDirection, transform.up); if (Mathf.Abs(angle) < deadZone) { transform.LookAt(transform.position + unitController.moveDirection); angle = 0f; } animSetup.Setup(5000, angle); }
// --------------------------------------------------------------------------------- // METHOD: NavAnimSetup() // Description: Here we create the speed and angle values which will be passed to the // agent's animator controller in the _animatorSetup.Setup() method. // If the angle the agent wishes to turn is less than our pre-defined // DeadZone, then simply set it to zero // --------------------------------------------------------------------------------- private void NavAnimSetup() { float speed; float angle; speed = Vector3.Project(_navAgent.desiredVelocity, transform.forward).magnitude; // Project the desired velocity onto the actual forward vector and get the magnitude angle = FindAngle(transform.forward, _navAgent.desiredVelocity, transform.up); // Calculate the angle if (Mathf.Abs(angle) < DeadZone) { transform.LookAt(transform.position + _navAgent.desiredVelocity); angle = 0f; } _animatorSetup.Setup(speed, angle); }
void NavAnimSetup() { // Create the parameters to pass to the helper function. //Creamos los parametros que pasara a la funcion de ayuda(script AnimatorSetup). float speed; float angle; // If the player is in sight... // Si el jugador esta a la vista... if (enemySight.playerInSight) { // ... the enemy should stop... //... el enemigo deberia detenerse... speed = 0f; //EN EL VIDEO EXPLICA EN ESTE PUNTO QUE LA IZQUIERDA ES NEGATIVA Y DERECHA ES POSITIVA A PARTIR DEL VECTOR FORWARD QUE INDICA ADELANTE Y PASAMOS A CREAR LA FUNCION ANGLE MAS ABAJO. // ... and the angle to turn through is towards the player. //... y el angulo a traves del cual gira es decir hacia el jugador. angle = FindAngle(transform.forward, player.position - transform.position, transform.up); } else { // Otherwise the speed is a projection of desired velocity on to the forward vector... // de otro modo la variable velocidad es una proyeccion de velocidad deseada en el vector adelante... //(esto evitara que salga corriendo si no esta mirando en la direccion correcta.) speed = Vector3.Project(nav.desiredVelocity, transform.forward).magnitude; // ... and the angle is the angle between forward and the desired velocity. //... y el angulo es un angulo entre adelante y la velocidad deseada. angle = FindAngle(transform.forward, nav.desiredVelocity, transform.up); // If the angle is within the deadZone... // si el angulo esta dentro de la Zona muerta... if (Mathf.Abs(angle) < deadZone) { // ... set the direction to be along the desired direction and set the angle to be zero. //... establecemos la direccion a lo largo de la direccion deseada y estebleceremos el angulo en cero transform.LookAt(transform.position + nav.desiredVelocity); angle = 0f; } } // Call the Setup function of the helper class with the given parameters. // Llama a la funcion Setup de la clase ayudante(estamas arriba en este script) enviandole los parametros. animSetup.Setup(speed, angle); }
void NavAnimationUpdate() { speed = Vector3.Project(nav.desiredVelocity, transform.forward).magnitude; // angle is the angle between forward and the desired velocity. angle = FindAngle(transform.forward, nav.desiredVelocity, transform.up); // If the angle is within the deadZone // set the direction to be along the desired direction and set the angle to be zero. if (Mathf.Abs(angle) < deadZone) { transform.LookAt(transform.position + nav.desiredVelocity); angle = 0f; } animSetup.Setup(speed, angle); }
// Update is called once per frame void Update() { float realTolerance = tolerance; if (moving) { realTolerance *= 0.5f; } float minDistance = distance - realTolerance; float maxDistance = distance + realTolerance; float fastWalkDistance = distance + fastWalkDistanceMultiplier * realTolerance; Vector3 myPos = transform.position - Vector3.Project(transform.position, transform.up); Vector3 targetPos = target.position - Vector3.Project(target.position, transform.up); //Vector3 myPos = new Vector3(transform.position.x, 0f, transform.position.z); //Vector3 targetPos = new Vector3(target.position.x, 0f, target.position.z); float dist = Vector3.Distance(myPos, targetPos); float angle = FindAngle(transform.forward, targetPos - myPos, transform.up); if (angle > Mathf.PI) { angle -= Mathf.PI; } Debug.Log(myPos + " " + targetPos); //Debug.Log("angle: " + angle); Debug.Log("dist: " + dist); Vector3 difference = targetPos - myPos; moving = false; //float speed = 0f; speed = speed * 0.7f; if (speed < 0.1f) { speed = 0.0f; } //Debug.Log(transform.forward); if (dist > maxDistance) { float spd = walkingSpeed; if (dist > fastWalkDistance) { spd = fastWalkingSpeed; } speed = spd * Vector3.Project(difference, transform.forward).magnitude; moving = true; } if (dist < minDistance) { speed = -walkingSpeed * 3.0f * Vector3.Project(difference, transform.forward).magnitude; moving = true; } if (Mathf.Abs(angle) < angleTolerance) { transform.LookAt(transform.position + difference); angle = 0f; } if (Mathf.Abs(speed) < speedTolerance) { speed = 0f; } Debug.Log("speed " + speed); // Call the Setup function of the helper class with the given parameters. animSetup.Setup(speed, angle); }