博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D 5.0简单的射线检测实现跳跃功能
阅读量:7009 次
发布时间:2019-06-28

本文共 1632 字,大约阅读时间需要 5 分钟。

 

这里是一个简单的跳跃,5.0和其他版本貌似不一样,并且,再起跳功能做的不完全。

不过一个基本的思路在这里。

1.首先,射线检测,这里是利用一个空对象,放到主角对象的下面

2.然后调节射线的位置,在主角对象的下面一点(这点很重要,差不多放在脚下,这样才能和地面接触,不然就永远也和地面接触不了)

LineCast 两点之间产生射线,去和某个碰撞器发生碰撞,触发了碰撞器,返回一个真

先定义要碰撞的碰撞器的层

然后,在绑定主角的脚本文件上开始写脚本:

using UnityEngine;using System.Collections;public class move : MonoBehaviour {	// Use this for initialization	private bool grounded = false;	private Transform groundcheck;	private bool jump = false; //角色的跳起	private Rigidbody2D hero;	public float jumpy = 360f;	public AudioClip jumpclips; //跳跃音频	void Start () {		groundcheck = transform.Find ("groundcheck");		//hero = transform.Find ("pk_0");		}		// Update is called once per frame	void Update () {		if(Input.GetKey(KeyCode.W)){						gameObject.transform.Translate(Vector3.up*5*Time.deltaTime);		}		if(Input.GetKey(KeyCode.S)){			gameObject.transform.Translate(Vector3.down*5*Time.deltaTime);		}		if(Input.GetKey(KeyCode.A)){			gameObject.transform.Translate(Vector3.left*5*Time.deltaTime);		}		if(Input.GetKey(KeyCode.D)){			gameObject.transform.Translate(Vector3.right*5*Time.deltaTime);		}		//与地面接触为真,太能够进行跳跃		grounded = Physics2D.Linecast (transform.position, groundcheck.transform.position,1 << LayerMask.NameToLayer("Ground"));		if(grounded && Input.GetKeyDown(KeyCode.J)){			jump = true;			//设置角色的起跳功能			if(jump == true){					AudioSource.PlayClipAtPoint(jumpclips,gameObject.transform.position);				gameObject.GetComponent
().AddForce(new Vector2(0f,jumpy)); //hero.AddForce(new Vector2(0f,1000f)); jump = false; grounded = false; } } Debug.DrawLine (transform.position, groundcheck.transform.position,Color.red,1f); }}

  

 

转载地址:http://myttl.baihongyu.com/

你可能感兴趣的文章
linux下的静态库和动态库分析
查看>>
2.1 Latches--锁存器 和 FlipFlops--触发器 part1
查看>>
fio 命令入门到跑路(千万不能在系统所在的分区测试硬盘性能)
查看>>
zabbix自动报警邮件正文变成附件问题解决
查看>>
第一篇博客
查看>>
豆瓣阿北:用户价值大于产品体验,通过产品做运营
查看>>
单播(unicast)、组播(multicast)、广播(broadcast)的区别
查看>>
我的友情链接
查看>>
利用clonezilla克隆、还原CentOS整个系统
查看>>
解决127.0.0.1 localhost 劫持问题
查看>>
关于硬盘的一切!
查看>>
人工智能落地之路:从概念验证到产品
查看>>
winscp连接虚拟机Linux被拒绝的问题解决方案
查看>>
教程-Delphi设置功能表
查看>>
node常用库或中间件
查看>>
关系运算图模板
查看>>
Java中的多线程,线程池
查看>>
发布系统背景和saltstack的基本操作
查看>>
软件下载站
查看>>
appium - 连接设备
查看>>