boom.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boom : MonoBehaviour
{
public void destroy()
{
Destroy(gameObject);
}
}
bird.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bird : MonoBehaviour
{
private bool IsClick;
public float MaxDis;
public Transform LeftPos;
public Transform RightPos;
public LineRenderer LLine;
public LineRenderer RLine;
private Component[] sps,rgs;
private SpringJoint2D sp;
private Rigidbody2D rg;
private void Awake()
{
sps = GetComponents<SpringJoint2D>();
rgs = GetComponents<Rigidbody2D>();
}
private void OnMouseDown()
{
IsClick = true;
foreach(Rigidbody2D rg in rgs)
{
rg.isKinematic = true;
}
}
private void OnMouseUp()
{
IsClick = false;
foreach (Rigidbody2D rg in rgs)
{
rg.isKinematic = false;
}
Invoke("Fly",0.2f);
}
private void Update()
{
if (IsClick == true)
{
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position -= new Vector3(0, 0,Camera.main.transform.position.z);
if (Vector3.Distance(RightPos.position,transform.position) > MaxDis)
{
Vector3 Pos = (transform.position - RightPos.position).normalized;
Pos = Pos * MaxDis;
transform.position = Pos + RightPos.position;
}
WLine();
}
}
void Fly()
{
foreach (SpringJoint2D sp in sps)
{
sp.enabled = false;
}
}
void WLine()
{
LLine.SetPosition(0, LeftPos.position);
LLine.SetPosition(1, transform.position);
RLine.SetPosition(0, RightPos.position);
RLine.SetPosition(1, transform.position);
}
}
pig.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pig : MonoBehaviour
{
public float MaxV = 10;
public float MinV = 5;
private SpriteRenderer sp;
public Sprite hurted;
public GameObject boom;
public GameObject score_3000;
private void Awake()
{
sp = GetComponent<SpriteRenderer>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log(collision.relativeVelocity.magnitude);
if (collision.relativeVelocity.magnitude >= 10)
{
Destroy(gameObject);
Instantiate(boom, transform.position, Quaternion.identity);
GameObject go = Instantiate(score_3000, transform.position + new Vector3(0,0.5f,0), Quaternion.identity);
Destroy(go, 1.5f);
}
else if (collision.relativeVelocity.magnitude > 5 && collision.relativeVelocity.magnitude < 10)
{
sp.sprite = hurted;
}
}
}
|