content
1. Wonder: Angry Birds: How to Make
2. Make study notes so that you can check them next time
Operation: 1:03 Introduction to springjoint formation
1. The bird adds a spring joint Spring Joint 2D-Let it pull with a spring feeling
Operation: 3:05 - Maximum drag distance
1. Create a new sub-object under the right, name it rightPos, and drag it to about this position
1. The calculation idea of the unitized vector:
First, the purpose
1. Wonder: Angry Birds: How to Make
2. Make study notes so that you can check them next time
2. Reference
1. SIKI Academy
Login - SiKi Academy - Life is endless, learning is endless!
good: URL to learn
3. Attention
1. Version
- Unity2017.2.0f3
- VS2019
- UnityHUB 2.5.6
Operation: 1:03 Introduction to springjoint formation
1. Add a rigid body
1. The bird adds a spring joint Spring Joint 2D-Let it pull with a spring feeling
Component documentation address:
Spring Joint 2D - Unity Manual
Operation: 2:04 Bird's Drag
1. Birds add collision body
1. Bird add code: Bird
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 【Author:xzy;Time:2021-12-28】
/// Function:小鸟身上的代码
/// </summary>
public class Bird : MonoBehaviour
{
/// <summary>bool:是否点击</summary>
private bool bIsClick = false;
/// <summary>
///【Author:xzy;Time:2021-12-28】
/// Function: 鼠标按下
/// </summary>
private void OnMouseDown()
{
bIsClick = true;
}
/// <summary>
///【Author:xzy;Time:2021-12-28】
/// Function: 鼠标抬起
/// </summary>
private void OnMouseUp()
{
bIsClick = false;
}
private void Update()
{
if (bIsClick)
{
//鼠标按下,进行位置的跟随,屏幕坐标转换为世界坐标
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position += new Vector3(0, 0, -Camera.main.transform.position.z);
}
}
}
1. Operation effect: success
Operation: 3:05 - Maximum drag distance
1. Create a new sub-object under the right, name it rightPos, and drag it to about this position
1. The calculation idea of the unitized vector:
A represents the bird, B represents the right slingshot;
The A vector minus the B vector is the unitized vector: generally in order to know the direction
1. Code: Bird
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 【Author:xzy;Time:2021-12-28】
/// Function:小鸟身上的代码
/// </summary>
public class Bird : MonoBehaviour
{
/// <summary>bool:是否点击</summary>
private bool bIsClick = false;
/// <summary>Transform:弹弓右边的位置,目的:方便计算鸟和弹弓的距离</summary>
public Transform rightPos;
/// <summary>float:计算鸟和弹弓的距离的最大值</summary>
public float fMaxdis=3;
/// <summary>
///【Author:xzy;Time:2021-12-28】
/// Function: 鼠标按下
/// </summary>
private void OnMouseDown()
{
bIsClick = true;
}
/// <summary>
///【Author:xzy;Time:2021-12-28】
/// Function: 鼠标抬起
/// </summary>
private void OnMouseUp()
{
bIsClick = false;
}
private void Update()
{
if (bIsClick)
{
//鼠标按下,进行位置的跟随,屏幕坐标转换为世界坐标
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position += new Vector3(0, 0, -Camera.main.transform.position.z);
//进行位置限制
if (Vector3.Distance(transform.position, rightPos.position) > fMaxdis)
{
Vector3 pos = (transform.position - rightPos.position).normalized; //单位化向量:获取方向,鸟的向量减去右边弹弓的位置的向量
pos *= fMaxdis; //最大长度的向量
transform.position = pos + rightPos.position;
}
}
}
}
1. Bild code settings
1. Running result: success
Operation: 4:06 Bird's Flight
1. Operation effect: success
1. Code: Bird
- Key point: Press the bird to turn on the dynamics of its physical components, from dynamic to kinematic
- Key point: Release the bird and turn off the dynamics of its physical components + timer: wake up the logic of the bird flying out
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 【Author:xzy;Time:2021-12-28】
/// Function:小鸟身上的代码
/// </summary>
public class Bird : MonoBehaviour
{
/// <summary>bool:是否点击</summary>
private bool bIsClick = false;
/// <summary>Transform:弹弓右边的位置,目的:方便计算鸟和弹弓的距离</summary>
public Transform rightPos;
/// <summary>float:计算鸟和弹弓的距离的最大值</summary>
public float fMaxdis = 3;
/// <summary>SpringJoint2D:鸟身上弹簧的组件</summary>
private SpringJoint2D sp;
/// <summary>Rigidbody2D:鸟身上的物理组件</summary>
private Rigidbody2D rg;
private void Awake()
{
sp = this.GetComponent<SpringJoint2D>();//获取小鸟的弹簧组件
rg = this.GetComponent<Rigidbody2D>();//获取小鸟身上的物理组件
}
/// <summary>
///【Author:xzy;Time:2021-12-28】
/// Function: 鼠标按下
/// </summary>
private void OnMouseDown()
{
bIsClick = true;
rg.isKinematic = true;//按下小鸟,让其物理组件的动力学开启,从dynamic到kinematic
}
/// <summary>
///【Author:xzy;Time:2021-12-28】
/// Function: 鼠标抬起
/// </summary>
private void OnMouseUp()
{
bIsClick = false;
rg.isKinematic = false;//松开小鸟,让其物理组件的动力学关闭
Invoke("Fly", 0.1f);//定时器:唤醒 小鸟飞出的逻辑
}
private void Update()
{
if (bIsClick)
{
//鼠标按下,进行位置的跟随,屏幕坐标转换为世界坐标
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position += new Vector3(0, 0, -Camera.main.transform.position.z);
//进行位置限制
if (Vector3.Distance(transform.position, rightPos.position) > fMaxdis)
{
Vector3 pos = (transform.position - rightPos.position).normalized; //单位化向量:获取方向,鸟的向量减去右边弹弓的位置的向量
pos *= fMaxdis; //最大长度的向量
transform.position = pos + rightPos.position;
}
}
}
/// <summary>
///【Author:xzy;Time:2021-12-29】
/// Function: 处理松开小鸟飞出的逻辑
/// </summary>
void Fly()
{
sp.enabled = false;//松开小鸟,取消掉弹簧组件,小鸟就会飞出
}
}