实现了小鸟的抬头低头效果,碰撞检测

上级 d4882963
%YAML 1.1 %YAML 1.1
%TAG !u! tag:unity3d.com,2011: %TAG !u! tag:unity3d.com,2011:
--- !u!1 &6243242003816384578
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 5667231155455462974}
- component: {fileID: 7174408626539812788}
m_Layer: 0
m_Name: Score
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &5667231155455462974
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6243242003816384578}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.34, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 6470098503324179898}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!61 &7174408626539812788
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6243242003816384578}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 1
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0.09667066, y: -0.00000047683716}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
oldSize: {x: 0, y: 0}
newSize: {x: 0, y: 0}
adaptiveTilingThreshold: 0
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 0.27936375, y: 1.4221201}
m_EdgeRadius: 0
--- !u!1 &6470098503324179893 --- !u!1 &6470098503324179893
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -29,6 +86,7 @@ Transform: ...@@ -29,6 +86,7 @@ Transform:
m_Children: m_Children:
- {fileID: 6470098504182819943} - {fileID: 6470098504182819943}
- {fileID: 6470098505145496451} - {fileID: 6470098505145496451}
- {fileID: 5667231155455462974}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
...@@ -42,6 +100,7 @@ GameObject: ...@@ -42,6 +100,7 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 6470098504182819943} - component: {fileID: 6470098504182819943}
- component: {fileID: 6470098504182819940} - component: {fileID: 6470098504182819940}
- component: {fileID: 7394891180029338623}
m_Layer: 0 m_Layer: 0
m_Name: pipeTop m_Name: pipeTop
m_TagString: Untagged m_TagString: Untagged
...@@ -113,6 +172,32 @@ SpriteRenderer: ...@@ -113,6 +172,32 @@ SpriteRenderer:
m_WasSpriteAssigned: 1 m_WasSpriteAssigned: 1
m_MaskInteraction: 0 m_MaskInteraction: 0
m_SpriteSortPoint: 0 m_SpriteSortPoint: 0
--- !u!61 &7394891180029338623
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6470098504182819942}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 1
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0.5, y: 0.5}
oldSize: {x: 0.7, y: 4.53}
newSize: {x: 0.7, y: 4.53}
adaptiveTilingThreshold: 0.5
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 0.7, y: 4.53}
m_EdgeRadius: 0
--- !u!1 &6470098505145496450 --- !u!1 &6470098505145496450
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -123,6 +208,7 @@ GameObject: ...@@ -123,6 +208,7 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 6470098505145496451} - component: {fileID: 6470098505145496451}
- component: {fileID: 6470098505145496448} - component: {fileID: 6470098505145496448}
- component: {fileID: 7585981152488610433}
m_Layer: 0 m_Layer: 0
m_Name: pipeBottom m_Name: pipeBottom
m_TagString: Untagged m_TagString: Untagged
...@@ -194,3 +280,29 @@ SpriteRenderer: ...@@ -194,3 +280,29 @@ SpriteRenderer:
m_WasSpriteAssigned: 1 m_WasSpriteAssigned: 1
m_MaskInteraction: 0 m_MaskInteraction: 0
m_SpriteSortPoint: 0 m_SpriteSortPoint: 0
--- !u!61 &7585981152488610433
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6470098505145496450}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 1
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0.5, y: 0.5}
oldSize: {x: 0.7, y: 4.53}
newSize: {x: 0.7, y: 4.53}
adaptiveTilingThreshold: 0.5
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 0.7, y: 4.53}
m_EdgeRadius: 0
...@@ -188,6 +188,16 @@ PrefabInstance: ...@@ -188,6 +188,16 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 6470098504182819942, guid: f8f796c45dbd38a47aa327783411195c,
type: 3}
propertyPath: m_Name
value: PipeTop
objectReference: {fileID: 0}
- target: {fileID: 6470098505145496450, guid: f8f796c45dbd38a47aa327783411195c,
type: 3}
propertyPath: m_Name
value: PipeBottom
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: f8f796c45dbd38a47aa327783411195c, type: 3} m_SourcePrefab: {fileID: 100100000, guid: f8f796c45dbd38a47aa327783411195c, type: 3}
--- !u!4 &91773835 stripped --- !u!4 &91773835 stripped
...@@ -706,8 +716,75 @@ PrefabInstance: ...@@ -706,8 +716,75 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 6470098504182819942, guid: f8f796c45dbd38a47aa327783411195c,
type: 3}
propertyPath: m_Name
value: PipeTop
objectReference: {fileID: 0}
- target: {fileID: 6470098505145496450, guid: f8f796c45dbd38a47aa327783411195c,
type: 3}
propertyPath: m_Name
value: PipeBottom
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: f8f796c45dbd38a47aa327783411195c, type: 3} m_SourcePrefab: {fileID: 100100000, guid: f8f796c45dbd38a47aa327783411195c, type: 3}
--- !u!1 &520998070
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 520998072}
- component: {fileID: 520998071}
m_Layer: 0
m_Name: GroundCollider
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!61 &520998071
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 520998070}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 1
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0.017357469, y: -0.47608316}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
oldSize: {x: 0, y: 0}
newSize: {x: 0, y: 0}
adaptiveTilingThreshold: 0
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 5.2130203, y: 2.2132251}
m_EdgeRadius: 0
--- !u!4 &520998072
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 520998070}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: -3.07, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!4 &641561914 stripped --- !u!4 &641561914 stripped
Transform: Transform:
m_CorrespondingSourceObject: {fileID: 6470098503324179898, guid: f8f796c45dbd38a47aa327783411195c, m_CorrespondingSourceObject: {fileID: 6470098503324179898, guid: f8f796c45dbd38a47aa327783411195c,
...@@ -781,6 +858,16 @@ PrefabInstance: ...@@ -781,6 +858,16 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 6470098504182819942, guid: f8f796c45dbd38a47aa327783411195c,
type: 3}
propertyPath: m_Name
value: PipeTop
objectReference: {fileID: 0}
- target: {fileID: 6470098505145496450, guid: f8f796c45dbd38a47aa327783411195c,
type: 3}
propertyPath: m_Name
value: PipeBottom
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: f8f796c45dbd38a47aa327783411195c, type: 3} m_SourcePrefab: {fileID: 100100000, guid: f8f796c45dbd38a47aa327783411195c, type: 3}
--- !u!1 &851467229 --- !u!1 &851467229
...@@ -796,6 +883,7 @@ GameObject: ...@@ -796,6 +883,7 @@ GameObject:
- component: {fileID: 851467230} - component: {fileID: 851467230}
- component: {fileID: 851467233} - component: {fileID: 851467233}
- component: {fileID: 851467234} - component: {fileID: 851467234}
- component: {fileID: 851467235}
m_Layer: 0 m_Layer: 0
m_Name: Bird m_Name: Bird
m_TagString: Untagged m_TagString: Untagged
...@@ -860,7 +948,7 @@ SpriteRenderer: ...@@ -860,7 +948,7 @@ SpriteRenderer:
m_LightmapParameters: {fileID: 0} m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 174451655 m_SortingLayerID: 174451655
m_SortingLayer: 2 m_SortingLayer: 2
m_SortingOrder: 0 m_SortingOrder: 1
m_Sprite: {fileID: -6583155732020471796, guid: 5d16aab893e4b54409e4e28c1b4735d2, m_Sprite: {fileID: -6583155732020471796, guid: 5d16aab893e4b54409e4e28c1b4735d2,
type: 3} type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1} m_Color: {r: 1, g: 1, b: 1, a: 1}
...@@ -901,8 +989,9 @@ MonoBehaviour: ...@@ -901,8 +989,9 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
floatScall: 0.3 floatScall: 0.3
floatRate: 1 floatRate: 1
gravityScall: 2.3 downSpeed: -6
force: 330 jumpSpeed: 6
reduceSpeed: 20
--- !u!50 &851467234 --- !u!50 &851467234
Rigidbody2D: Rigidbody2D:
serializedVersion: 4 serializedVersion: 4
...@@ -924,6 +1013,43 @@ Rigidbody2D: ...@@ -924,6 +1013,43 @@ Rigidbody2D:
m_SleepingMode: 1 m_SleepingMode: 1
m_CollisionDetection: 0 m_CollisionDetection: 0
m_Constraints: 0 m_Constraints: 0
--- !u!60 &851467235
PolygonCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 851467229}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 1
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0.5, y: 0.5}
oldSize: {x: 0.8, y: 0.6}
newSize: {x: 0.8, y: 0.6}
adaptiveTilingThreshold: 0.5
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
m_Points:
m_Paths:
- - {x: 0.08030242, y: 0.15283234}
- {x: -0.036418624, y: 0.15283242}
- {x: -0.06597688, y: 0.13745669}
- {x: -0.09509341, y: 0.12150282}
- {x: -0.20030239, y: -0.0032947473}
- {x: -0.19672102, y: -0.049537703}
- {x: -0.074930325, y: -0.15924858}
- {x: 0.029255845, y: -0.15745671}
- {x: 0.16104655, y: -0.11953767}
- {x: 0.18702343, y: -0.09971093}
- {x: 0.21567449, y: -0.028959516}
- {x: 0.16209309, y: 0.06745669}
--- !u!1 &1061721880 --- !u!1 &1061721880
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
......
using System.Collections; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
...@@ -8,9 +9,18 @@ public class Bird : MonoBehaviour ...@@ -8,9 +9,18 @@ public class Bird : MonoBehaviour
private Vector3 startPos;//小鸟的初始位置 private Vector3 startPos;//小鸟的初始位置
public float floatScall=1;//浮动范围 public float floatScall=1;//浮动范围
public float floatRate = 1;//浮动频率 public float floatRate = 1;//浮动频率
public float gravityScall = 1;//小鸟重力
private float currentSpeed;//当前小鸟的速度
public float downSpeed = -5;//正常向下的速度
public float jumpSpeed=6;//起跳时的速度
public float reduceSpeed=20f;//减速度
private Quaternion upRotation=Quaternion.Euler(0,0,25);//向上的旋转
private Quaternion downRotation=Quaternion.Euler(0,0,-90);//向下的旋转
private Rigidbody2D rig;//刚体 private Rigidbody2D rig;//刚体
public float force=50;//力
public event Action<Collider2D> onTrigger;//事件:当碰撞触发
private void Start() private void Start()
{ {
startPos = transform.position;//记录小鸟的初始位置 startPos = transform.position;//记录小鸟的初始位置
...@@ -23,23 +33,37 @@ public class Bird : MonoBehaviour ...@@ -23,23 +33,37 @@ public class Bird : MonoBehaviour
transform.position =startPos+ Vector3.up * Mathf.Sin(Time.time* floatRate) *floatScall; transform.position =startPos+ Vector3.up * Mathf.Sin(Time.time* floatRate) *floatScall;
} }
public void UseGravity()
{
rig.gravityScale = gravityScall;//设置重力值
}
//小鸟飞和跳,被控制器调用 //小鸟飞和跳,被控制器调用
public void Flay() public void Flay()
{ {
if (Input.GetMouseButtonDown(0)) //计算速度
if (currentSpeed>downSpeed)//如果当前速度大于正常向下速度,就让当前速度减小
{
currentSpeed -= reduceSpeed * Time.deltaTime;
}
//使用速度移动
transform.Translate(Vector3.up * currentSpeed * Time.deltaTime, Space.World);
//设置旋转
if (currentSpeed>0)
{
transform.rotation = Quaternion.Lerp(transform.rotation , upRotation , 10f*Time.deltaTime);
}
else if(currentSpeed <-5.5f)
{ {
Jump(); transform.rotation= Quaternion.RotateTowards(transform.rotation,downRotation,720*Time.deltaTime);
} }
} }
public void Jump() public void Jump()
{ {
rig.velocity = Vector2.zero;//刚体速度设置为0 currentSpeed = jumpSpeed;
rig.AddForce(Vector2.up * force);//加一个向上的力 }
//当触发器进入的时候
private void OnTriggerEnter2D(Collider2D collision)//collision 是对方的碰撞器组件
{
onTrigger?.Invoke(collision); // if (onTrigger!=null) { onTrigger(collision); }
} }
} }
...@@ -8,7 +8,41 @@ public class GameController : MonoBehaviour ...@@ -8,7 +8,41 @@ public class GameController : MonoBehaviour
public MoveGroup moveGroup; public MoveGroup moveGroup;
public Bird bird; public Bird bird;
public GameState state = GameState.empty;//当前游戏状态 public GameState state = GameState.empty;//当前游戏状态
private int score=0;//分数
bool stop = false;//小鸟停止下落
private void Start()
{
bird.onTrigger += BirdOnTrigger;
}
private void BirdOnTrigger(Collider2D other)
{
if (other.name=="Score")
{
print("加分");
score += 1;
}
else if (other.name.StartsWith("Pipe"))
{
print("游戏结束");
Over();
}
else if ( other.name == "GroundCollider")
{
if (state!= GameState.gameOver)//如果游戏之前没有结束,就先结束
{
Over();
}
stop = true;
}
}
private void Over()
{
moveGroup.GetComponent<Rigidbody2D>().velocity = Vector2.zero;//水管和地板停止移动
state = GameState.gameOver;//切换游戏状态为结束状态
}
private void Update() private void Update()
{ {
...@@ -45,7 +79,6 @@ public class GameController : MonoBehaviour ...@@ -45,7 +79,6 @@ public class GameController : MonoBehaviour
{ {
moveGroup.pipGroup.SetParent(moveGroup.transform);//把水管设置为移动组的子物体 moveGroup.pipGroup.SetParent(moveGroup.transform);//把水管设置为移动组的子物体
bird.GetComponent<Animator>().enabled = true;//开启动画 bird.GetComponent<Animator>().enabled = true;//开启动画
bird.UseGravity();//小鸟使用重力
bird.Jump(); bird.Jump();
state = GameState.palying; state = GameState.palying;
} }
...@@ -53,13 +86,20 @@ public class GameController : MonoBehaviour ...@@ -53,13 +86,20 @@ public class GameController : MonoBehaviour
//玩的状态 //玩的状态
private void Play() private void Play()
{ {
bird. Flay(); bird. Flay();
//跳
if (Input.GetMouseButtonDown(0))
{
bird.Jump();
}
} }
private void GameOver() private void GameOver()
{ {
if (!stop)
{
bird.Flay();
}
} }
} }
...@@ -71,6 +111,8 @@ public enum GameState ...@@ -71,6 +111,8 @@ public enum GameState
gameOver//游戏结束状态 gameOver//游戏结束状态
} }
/*public 公开的哪儿都能访问 /*public 公开的哪儿都能访问
private 私有的只能在本类内部可以访问 private 私有的只能在本类内部可以访问
protected 收保护的,只能在本类内部和子类内部可以访问 protected 收保护的,只能在本类内部和子类内部可以访问
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册