unity人物旋转移动代码_Unity3d人物移动/旋转/技能释放

这篇博客详细介绍了在Unity3D中如何实现角色的移动和旋转,包括键盘和鼠标控制,以及射击、技能释放等功能。通过使用Animator组件、Raycast和碰撞检测,实现了角色的交互与操作。此外,还涉及到了技能冷却、角色死亡事件处理等游戏逻辑。
摘要由CSDN通过智能技术生成

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Player: MonoBehaviour {

public Animator ani;

public Transform pos;

public LineRenderer lineRenderer;

public UIButton shootBtn;//射击按钮

public UIButton skillBtn_1, skillBtn_2, skillBtn_3;//1技能按钮

public Camera ca;

public int enemyCount = 0;//当前击杀的怪物数量

public float survivalTime = 0;//存活时间

public double hp = 20;//人物血量

public double maxHp = 20;//人物最大血量

public GameManager gameManager;

public bool isDie = false;

public static Player instance;

//public UISprite skillMaskSprite_1;//1技能遮罩图片

//public UILabel skillTimeText_1;//1技能冷却时间文本

//public float collingTime = 5;//冷却时间

private void Awake()

{

instance = this;

}

// Use this for initialization

void Start () {

maxHp = hp = SaveDate.instance.currentAcc.hp;

ani = GetComponent();

pos = transform.Find("GunBarrelEnd");

// lineRenderer = GameObject.Find("LineRenderer").GetComponent();

//lineRenderer.gameObject.SetActive(false);

//shootBtn = GameObject.Find("UI Root").transform.Find("ShootButton").GetComponent();

// shootBtn.onClick.Add(new EventDelegate(ShootBtnEvent));

//skillBtn_1 = GameObject.Find("UI Root").transform.Find("skill_1").GetComponent();

//  skillBtn_2 = GameObject.Find("UI Root").transform.Find("skill_2").GetComponent();

// skillBtn_3 = GameObject.Find("UI Root").transform.Find("skill_3").GetComponent();

// skillBtn_1.onClick.Add(new EventDelegate(Skill_1));

//  skillBtn_2.onClick.Add(new EventDelegate(Skill_1));

//skillBtn_3.onClick.Add(new EventDelegate(Skill_5));

gameManager = GameObject.Find("Main Camera").GetComponent();

ca = GameObject.Find("Main Camera").GetComponent();

}

// Update is called once per frame

void Update () {

if (!isDie)

{

if (Rocker.ins.isPress)

{

PlayerMouseMove();

}

else

{

PlayerKeyMove();

}

//Skill_2();

//Skill_4();

//PlayerShoot();

//gameManager.taskManager.taskCtrl.Timer();

}

}

//键盘移动人物事件

void PlayerKeyMove() {

float h = Input.GetAxis("Horizontal");//获取水平轴

float v = Input.GetAxis("Vertical");//获取垂直轴

transform.Translate(new Vector3(h, 0, v) * Time.deltaTime * 3);

//print(h + "--v--" + v);

//当h或v不等于0处于运动状态

if (h != 0 || v != 0)

{

// ani.SetBool("IsMove", true);

}

else

{

//ani.SetBool("IsMove", false);

}

if (Input.GetKey(KeyCode.Q))

{

transform.Rotate(Vector3.up, -3.0f);

}

if (Input.GetKey(KeyCode.E))

{

transform.Rotate(Vector3.up, 3.0f);

}

//transform.Rotate(Vector3.left, Input.GetAxis("Horizontal"));

//transform.Rotate(Vector3.right, Input.GetAxis("Vertical"));

}

//鼠标摇杆移动人物事件

public void PlayerMouseMove() {

if (Rocker.ins.dir != Vector3.zero)

{

ani.SetBool("IsMove", true);

//四元数  控制物体旋转,可以避免万向锁

Quaternion que = Quaternion.LookRotation(Rocker.ins.dir);

//人物旋转

transform.rotation = Quaternion.Lerp(transform.rotation, que,0.3f);

//人物移动

transform.Translate(Rocker.ins.dir * Time.deltaTime * 15, Space.World);

//相机旋转

//ca.transform.rotation = transform.rotation;

//ca.transform.LookAt(transform.position);

}

else {

//    ani.SetBool("IsMove", false);

}

}

//射击的方法

void PlayerShoot() {

if (Input.GetMouseButton(0))

{

//lineRenderer.gameObject.SetActive(true);

//StartCoroutine(Des());

//实例化射线

Ray ray = new Ray(pos.position, pos.forward);

//  lineRenderer.SetPosition(0, pos.position);//设置射线渲染器第一个点坐标

RaycastHit hit;//接受射线碰撞的物体信息

//表示如果碰撞到物体

if (Physics.Raycast(ray, out hit))

{

//hit.point射线撞击点

//  lineRenderer.SetPosition(1, hit.point);

}

else

{

// lineRenderer.SetPosition(1, pos.position + pos.forward * 10);

}

}

if (Input.GetMouseButtonUp(0)) {

//lineRenderer.gameObject.SetActive(false);

}

}

IEnumerator Des() {

yield return new WaitForSeconds(0.2f);

//lineRenderer.gameObject.SetActive(false);

}

public void ShootBtnEvent() {

//激光射线特效

//lineRenderer.gameObject.SetActive(true);

StartCoroutine(Des());

Ray ray = new Ray(pos.position, pos.forward);

//  lineRenderer.SetPosition(0, pos.position);

RaycastHit hit;

if (Physics.Raycast(ray, out hit))

{

// lineRenderer.SetPosition(1, hit.point);

if (hit.collider.tag == "enemy")

{

//hit.collider.GetComponent().hit = 1;

//hit.collider.GetComponent().Behit();

//hit.collider.GetComponent().hit = 0;

}

}

else {

// lineRenderer.SetPosition(1, pos.position + pos.forward * 50);

}

}

public GameObject effectObj;

public GameObject effect1Obj;

public void Skill_1()

{

//自恢复特效

//实例化出来一个特效

//GameObject effect = Instantiate(effectObj);

//给定特效位置

//effect.transform.position = transform.position;

//Destroy(effect,1);

}

//定义一个层级

public LayerMask lm, lm2;

public void Skill_2() {

//闪现技能

//射线检测位置

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit hit;

if (Physics.Raycast(ray,out hit,50,lm))

{

if (Input.GetMouseButtonDown(0))

{

//实例化出来一个特效

//GameObject effect = Instantiate(effectObj);

//给定特效位置

// effect.transform.position = hit.point;

//transform.position = hit.point;

// Destroy(effect, 1);

}

}

}

IEnumerator Wait(Vector3 pos)

{

yield return new WaitForSeconds(3);

transform.position = pos;

}

public void Skill_4() {

//传送技能的实现

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit hit;

if (Physics.Raycast(ray, out hit, 100, lm)) {

if (Input.GetMouseButtonDown(1))

{

//生成特效

//特效位置,1生成到人物自身位置,2生成到传送点特效

//等待5s把鼠标点击位置赋值给人物

//实例化出来一个特效

//GameObject effect = Instantiate(effect1Obj);

//给定特效位置

// effect.transform.position = transform.position;

//实例化出来一个特效

//GameObject effect1 = Instantiate(effect1Obj);

//给定特效位置

// effect1.transform.position = hit.point;

// StartCoroutine(Wait(hit.point));

//  Destroy(effect,3);

//Destroy(effect1,3);

}

}

}

//技能范围显示及移动

public GameObject big;

public GameObject small;

public void Skill_5() {

big.SetActive(true);

small.SetActive(true);

StartCoroutine(SmallMove());

}

//技能显示层级

public LayerMask lm3;

//技能范围小圆移动事件

IEnumerator SmallMove() {

while (true)

{

//射线检测

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit hit;

if (Physics.Raycast(ray,out hit,20,lm3))

{

//获取人物和鼠标点之间的差值

Vector3 offset = hit.point-transform.position;

if (offset.magnitude>2)

{

offset = offset.normalized * 2;

}

small.transform.position = transform.position+ offset;

if (Input.GetMouseButtonDown(0))

{

//开启技能冷却协程

//skillBtn_3.GetComponent().Skill();

//隐藏图片

//big.SetActive(false);

// small.SetActive(false);

//实例化特效

// GameObject effect = Instantiate(effect1Obj);

//effect.transform.position = small.transform.position;

KillSuper();

// Destroy(effect,3);

break;

}

}

yield return null;

}

}

//技能释放---范围攻击

public LayerMask lm5;

void KillSuper()

{

//范围检测

Collider[] co = Physics.OverlapSphere(small.transform.position, 2, lm5);

for (int i = 0; i < co.Length; i++)

{

if (co[i].tag == "enemy")

{

//  co[i].GetComponent().hit = Random.Range(1,4);

// co[i].GetComponent().Behit();

}

}

}

//人物死亡

public void PlayerDieEvent() {

isDie = true;

//播放死亡画面

//ani.SetTrigger("die");

//弹出结束画面

}

}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值