всем привет написал такой код при клике ходить через NavMeshAgent
я только изучаю так что строго не судить )
я только изучаю так что строго не судить )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Move_anim : MonoBehaviour {
NavMeshAgent nav;
GameObject Enemy;
Animator animator;
[SerializeField]float dist;
[SerializeField]float time;
float radius=10f;
float normalizedTime;
float length;
int dts;
void Start () {
Enemy = null;
animator=GetComponent<Animator>();
nav=GetComponent<NavMeshAgent>();
nav.updateRotation = false;
normalizedTime = animator.GetCurrentAnimatorStateInfo (0).normalizedTime%1;
length = animator.GetCurrentAnimatorStateInfo(0).length;
if (nav == null)
return;
}
void Update () {
dts =(int)dist;
if (Enemy != null) {
range.text = Enemy.name + " " + dts.ToString ();
}
if (animator.GetCurrentAnimatorStateInfo(0).IsName("atack")&&normalizedTime < length) {
nav.Stop ();
} else {
animator.SetBool("enemyClicked",false);
nav.Resume();
}
animator_anim ();
Move_click();
Move_attack();
}
void animator_anim()
{
if (animator != null) {
if(nav.velocity.magnitude>0.1f)
animator.SetFloat("V",nav.velocity.magnitude);
else
animator.SetFloat("V",0);
}
}
void Move_click()
{
if (Input.GetMouseButton (0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit,100))
{
if(hit.transform.tag=="Plan"){
nav.destination=hit.point;
klick=false;
}
if(hit.transform.tag=="Enemy"){
Enemy=hit.collider.gameObject;
nav.SetDestination(Enemy.transform.position);
klick=true;
}
}
if(nav.velocity.magnitude !=0){
Vector3 dir = hit.point - transform.position;
dir.y = 0;
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(dir),15f*Time.deltaTime);
}
}
}
void Move_attack()
{
if (Enemy == null)
return;
dist = Vector3.Distance (Enemy.transform.position, transform.position);
if (dist > radius) {
}
if (dist < radius & dist >2f) {
animator.SetBool("enemyClicked",false);
}
if (dist < 2f&&klick) {
animator.SetBool("enemyClicked",true);
}
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Move_anim : MonoBehaviour {
NavMeshAgent nav;
GameObject Enemy;
Animator animator;
[SerializeField]float dist;
[SerializeField]float time;
float radius=10f;
float normalizedTime;
float length;
int dts;
void Start () {
Enemy = null;
animator=GetComponent<Animator>();
nav=GetComponent<NavMeshAgent>();
nav.updateRotation = false;
normalizedTime = animator.GetCurrentAnimatorStateInfo (0).normalizedTime%1;
length = animator.GetCurrentAnimatorStateInfo(0).length;
if (nav == null)
return;
}
void Update () {
dts =(int)dist;
if (Enemy != null) {
range.text = Enemy.name + " " + dts.ToString ();
}
if (animator.GetCurrentAnimatorStateInfo(0).IsName("atack")&&normalizedTime < length) {
nav.Stop ();
} else {
animator.SetBool("enemyClicked",false);
nav.Resume();
}
animator_anim ();
Move_click();
Move_attack();
}
void animator_anim()
{
if (animator != null) {
if(nav.velocity.magnitude>0.1f)
animator.SetFloat("V",nav.velocity.magnitude);
else
animator.SetFloat("V",0);
}
}
void Move_click()
{
if (Input.GetMouseButton (0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit,100))
{
if(hit.transform.tag=="Plan"){
nav.destination=hit.point;
klick=false;
}
if(hit.transform.tag=="Enemy"){
Enemy=hit.collider.gameObject;
nav.SetDestination(Enemy.transform.position);
klick=true;
}
}
if(nav.velocity.magnitude !=0){
Vector3 dir = hit.point - transform.position;
dir.y = 0;
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(dir),15f*Time.deltaTime);
}
}
}
void Move_attack()
{
if (Enemy == null)
return;
dist = Vector3.Distance (Enemy.transform.position, transform.position);
if (dist > radius) {
}
if (dist < radius & dist >2f) {
animator.SetBool("enemyClicked",false);
}
if (dist < 2f&&klick) {
animator.SetBool("enemyClicked",true);
}
}
}
Последнее редактирование модератором: