unity

用户头像
来自佳木斯大学-杨会哲发布于:2021-10-12 20:04:29

```

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.AI;

public class PlayerContorl : MonoBehaviour
{
private Transform transform;
private NavMeshAgent agent;
private Animator anim;
private float speed;
private float coolTime;
private float attckTime;

void Awake()
{
this.anim = GetComponent<Animator>();
this.transform = GetComponent<Transform>();
this.agent = GetComponent<NavMeshAgent>();
this.speed = 0.0f;
this.coolTime = 0.9f;
this.attckTime = 0.9f;
}
void Start()
{

}

void Update()
{
PlayerMove();
anim.SetBool("Attack", false);
cdTime();
AttackAnimi();

}

//玩家移动控制
void PlayerMove()
{
if (Input.GetKey(KeyCode.LeftShift)&&(Input.GetKeyDown(KeyCode.A)|| Input.GetKey(KeyCode.D)|| Input.GetKey(KeyCode.S)|| Input.GetKey(KeyCode.W)))
{
speed = 4.0f;
MoveAnimSiwtch(speed);
Move(speed);
}
else if(Input.GetKeyDown(KeyCode.A) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.W))
{
MoveAnimSiwtch(2.0f);
Move(2.0f);
}
else
{
MoveAnimSiwtch(0.0f);

}
}

void Move(float speed)
{
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.back * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
}

//玩家移动动画切换器
void MoveAnimSiwtch(float speed)
{
anim.SetFloat("Speed", speed);
}


//TODO: 攻击具体数据于触发器
void AttackData()
{

}

void cdTime()
{
if(attckTime < coolTime)
{
anim.SetBool("Attack", true);
attckTime += Time.deltaTime;
}
if (attckTime > coolTime)
{
attckTime = coolTime;
}

}
void AttackAnimi()
{
if (Input.GetKey(KeyCode.Mouse0) && attckTime == coolTime)
{
//攻击间隔;
//Debug.Log("你点击了攻击键");

attckTime = 0.0f;
}
}

//普通攻击
void Attack()
{

}


void AttackRange()
{

}

//TODO:死亡处理与动画

void DeathAnima()
{

}


}

```

点赞 (0) 回复
发布回复
点击图片