Unity Official Tutorial OF PICKING UP COLLECTABLES --- Player Movement & Collision Detection
2016-04-30 23:17
381 查看
You will learn
1. Control player move using Rigidbody2d;2. Collision Detection using OnTriggerEnter2D function;
3. And some important ATTTIONS when collison detection.
Script of PlayerController.cs
ATTENTION PLEASE:1. We donot use
OnTrigger in 2D world, which is a
3D world function, just using OnTrigger2Dof2D world instead, attenion please - so doesthe parameter of Collider2D.
2. When collision detection, the gameobject with"some collider" mustcheckable IsTrigger in Inspector panel, if you do wanna
do collision; if not, OnTriggerEnter2D (Collider2D other) will be not workable. See the following demo:
REMEMBER THAT:
1. The wall and "pickups" are all added with " Circle Collider",but if you do wannapick up the "pickups", you mustcheckIs Trigger in Inspector, which
help you MAKE OnTriggerEnter2D workable;
2. If you do not uncheck the "Is Trigger of" Wall's collider, UFO will not run out of wall; otherwise the UFO run out of wall after second movement....
using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { private Rigidbody2D rigidbody2d; public float speed;// speed of UFO // Use this for initialization void Start () { rigidbody2d = GetComponent<Rigidbody2D> (); } void FixedUpdate () { float h = Input.GetAxis ("Horizontal"); float v = Input.GetAxis ("Vertical"); movement (h, v); } void movement(float h, float v){ Vector2 movement = new Vector2 (h, v); rigidbody2d.AddForce (movement * speed * Time.deltaTime); //Vector3 pos = new Vector3 (h, v, 0); //GetComponent<Rigidbody2D>().transform.Translate (pos); } // We donot use OnTrigger in 2D world, which is a 3D world function, // just using OnTrigger2D of 2D world instead, attenion please - so does Collider2D. void OnTriggerEnter2D (Collider2D other){ //Debug.Log ("other name: " + other.gameObject.name ); if ( other.gameObject.CompareTag("PickUp") /*other.gameObject.tag == "PickUp" */) { other.gameObject.SetActive (false); //Destroy (other.gameObject); //score += amount; } } }
相关文章推荐
- 使用Unity3D 5.3.4 模仿2D游戏 FlappyBird(四)
- unity5.3.4 cloth系统使用方法(1)--小白入门
- Unity3d之Mecanim(新版动画系统)
- Unity3D 关于运动模型
- Raknet For Unity
- 【Unity灯光与渲染技术】LightProbes灯光探测器实践
- 【Unity灯光与渲染技术】unity5新版的光照系统
- Unity3d之Animation(动画系统)
- Unity工程调试Bug解决整理
- Unity3D占用内存太大的解决方法
- Unity3d生命周期
- 使用Unity3D 5.3.4 模仿2D游戏 FlappyBird(三)
- Unity中使用几何着色器制作类似于纪念碑谷的海(Unity几何着色器的一个例子)
- Unity5 更改鼠标样式
- 在Unity中StartCoroutine/yield return这个模式到底是怎么应用的?其中的原理是什么?
- unity, 查看.anim中的动画曲线(和帧)
- Unity3D NGUI刮刮卡效果
- 使用Unity3D 5.3.4 模仿2D游戏 FlappyBird(二)
- unity3d 实现残影特效
- unity3D 涂涂乐使用shader实现上色效果