TO SQUASH: Started nuke ball.
This commit is contained in:
@@ -23,14 +23,14 @@ public class FruitBall : MonoBehaviour
|
||||
{
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
animator = GetComponent<Animator>();
|
||||
speed = new Vector2(Mathf.Sign(Random.Range(-1f, 1f)) * 0.0001f,
|
||||
Mathf.Sign(Random.Range(-1f, 1f)) * 0.0001f);
|
||||
speed = new Vector2(Mathf.Sign(Random.Range(-1f, 1f)) * 0.005f,
|
||||
Mathf.Sign(Random.Range(-1f, 1f)) * 0.005f);
|
||||
|
||||
audioSource.PlayOneShot(spawnSound);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
void FixedUpdate()
|
||||
{
|
||||
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
|
||||
|
||||
@@ -61,7 +61,7 @@ public class FruitBall : MonoBehaviour
|
||||
collision.gameObject.CompareTag("FruitBall"))
|
||||
{
|
||||
speedIncreaseTimer = 2.0f; // Reset the timer to 2 seconds
|
||||
speedMultiplier = 50.0f; // Set the speed multiplier to 7x
|
||||
speedMultiplier = 20.0f; // Set the speed multiplier to 7x
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class NukeBall : MonoBehaviour
|
||||
{
|
||||
private Animator animator;
|
||||
|
||||
private static float gravity = 0.5f;
|
||||
|
||||
public AudioClip spawnSound;
|
||||
|
||||
public AudioClip bounceSound;
|
||||
|
||||
public AudioClip hitSound;
|
||||
|
||||
public AudioClip criticalSound;
|
||||
|
||||
public AudioClip explosionSound;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private Vector2 speed;
|
||||
|
||||
private int criticalState = 0;
|
||||
|
||||
private float criticalTime = 1.0f;
|
||||
|
||||
private float criticalTimer = 0.0f;
|
||||
|
||||
private float height;
|
||||
|
||||
private float verticalVelocity = 0.0f;
|
||||
|
||||
private Collider2D selfCollider;
|
||||
|
||||
private bool isBouncingToHeight = false;
|
||||
|
||||
private float bounceStartY = 0.0f;
|
||||
|
||||
private float fallTargetY = 0.0f;
|
||||
|
||||
private float bounceTimer = 0.0f;
|
||||
|
||||
private float bounceDuration = 1.00f;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
animator = GetComponent<Animator>();
|
||||
selfCollider = GetComponent<Collider2D>();
|
||||
speed = new Vector2(Random.Range(-0.09f, 0.09f), 0.0f);
|
||||
height = GetComponent<Transform>().position.y;
|
||||
fallTargetY = height;
|
||||
|
||||
UpdateFallTargetFromCurrentPosition();
|
||||
|
||||
audioSource.PlayOneShot(spawnSound);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
|
||||
|
||||
if (stateInfo.IsName("NuclearBallBounce")) {
|
||||
transform.Translate(speed);
|
||||
|
||||
Vector3 currentPosition = transform.position;
|
||||
|
||||
if (isBouncingToHeight)
|
||||
{
|
||||
bounceTimer += Time.fixedDeltaTime;
|
||||
|
||||
float t = Mathf.Clamp01(bounceTimer / bounceDuration);
|
||||
float easedT = 1.0f - Mathf.Pow(1.0f - t, 2.0f);
|
||||
|
||||
currentPosition.y = Mathf.Lerp(bounceStartY, height, easedT);
|
||||
|
||||
if (t >= 1.0f)
|
||||
{
|
||||
isBouncingToHeight = false;
|
||||
bounceTimer = 0.0f;
|
||||
verticalVelocity = 0.0f;
|
||||
currentPosition.y = height;
|
||||
UpdateFallTargetFromCurrentPosition();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bounceTimer += Time.fixedDeltaTime;
|
||||
|
||||
float t = Mathf.Clamp01(bounceTimer / bounceDuration);
|
||||
float easedT = t * t;
|
||||
|
||||
currentPosition.y = Mathf.Lerp(height, fallTargetY, easedT);
|
||||
}
|
||||
|
||||
transform.position = currentPosition;
|
||||
}
|
||||
|
||||
// When in critical state 2, play the critical sound every half a second.
|
||||
if (criticalState == 2)
|
||||
{
|
||||
criticalTimer = 0.5f;
|
||||
}
|
||||
|
||||
// If in a critical state, increment the critical timer and play the
|
||||
// critical sound if the timer exceeds the critical time.
|
||||
if (criticalState >= 1)
|
||||
{
|
||||
criticalTimer += Time.deltaTime;
|
||||
|
||||
if (criticalTimer >= criticalTime)
|
||||
{
|
||||
audioSource.PlayOneShot(criticalSound);
|
||||
}
|
||||
|
||||
criticalTimer = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void OnCollisionEnter2D(Collision2D collision)
|
||||
{
|
||||
// NukeBall should bounce off walls only. It cannot collide with other balls, not even other NukeBalls.
|
||||
if (collision.gameObject.CompareTag("Wall"))
|
||||
{
|
||||
ContactPoint2D contact = collision.GetContact(0);
|
||||
speed = Vector2.Reflect(speed, contact.normal);
|
||||
speed.y = 0.0f;
|
||||
|
||||
audioSource.PlayOneShot(bounceSound);
|
||||
|
||||
// A floor hit (normal pointing upward) starts an ease-out bounce back to initial height.
|
||||
if (contact.normal.y > 0.5f)
|
||||
{
|
||||
isBouncingToHeight = true;
|
||||
bounceTimer = 0.0f;
|
||||
bounceStartY = contact.point.y;
|
||||
fallTargetY = bounceStartY;
|
||||
verticalVelocity = 0.0f;
|
||||
|
||||
Vector3 currentPosition = transform.position;
|
||||
currentPosition.y = bounceStartY;
|
||||
transform.position = currentPosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateFallTargetFromCurrentPosition()
|
||||
{
|
||||
fallTargetY = transform.position.y;
|
||||
|
||||
RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, Vector2.down, 100.0f);
|
||||
float closestDistance = float.MaxValue;
|
||||
|
||||
foreach (RaycastHit2D hit in hits)
|
||||
{
|
||||
if (hit.collider == null || hit.collider == selfCollider)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!hit.collider.CompareTag("Wall"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hit.normal.y <= 0.5f)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hit.distance < closestDistance)
|
||||
{
|
||||
closestDistance = hit.distance;
|
||||
fallTargetY = hit.point.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94b425bc5ee3b5fe5b375cb72355531c
|
||||
@@ -35,13 +35,13 @@ public class OozeBall : MonoBehaviour
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
animator = GetComponent<Animator>();
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
speed = new Vector2(Random.Range(-0.002f, 0.002f), Random.Range(-0.002f, 0.002f));
|
||||
speed = new Vector2(Random.Range(-0.05f, 0.05f), Random.Range(-0.05f, 0.05f));
|
||||
|
||||
audioSource.PlayOneShot(spawnSound);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
void FixedUpdate()
|
||||
{
|
||||
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user