Files
metaballs/Assets/metaball/Scripts/BouncingBall.cs
celisej567 76e4be1dc1 A lot of diffrerent things
Now containers will have different meshes.
Added Bouncing Ball in context menu.
Added Metaballs namespace.
Added QuakeFastSqrt function instead default sqrt in .compute shader.

Fixed some bugs.

Bug: actuall you can't use more then one Conteiner, because for some reason only one work at one time. It will show, but not updates.
2023-03-23 19:18:07 +03:00

81 lines
2.8 KiB
C#

using UnityEngine;
using System.Collections;
namespace MetaBalls
{
public class BouncingBall : MetaBall
{
public float speed;
private Container container;
private Vector3 direction;
private Vector3 lastPos;
override public void OnDrawGizmos()
{
base.OnDrawGizmos();
}
public override void Start()
{
base.Start();
this.direction = Random.onUnitSphere;
this.container = this.GetComponentInParent<Container>();
}
override public void Update()
{
base.Update();
if (Application.isPlaying)
{
if (transform.position != lastPos && speed != 0)
{
lastPos = transform.position;
this.updatePosition(Time.deltaTime);
}
}
}
public void updatePosition(float dt)
{
float posX = this.transform.position.x, posY = this.transform.position.y, posZ = this.transform.position.z;
Vector3 containerPosition = this.container.transform.position;
Vector3 containerScale = this.container.transform.localScale;
if (posX + this.radius + this.container.safeZone > containerPosition.x + containerScale.x / 2)
{
posX -= 0.01f;
this.direction = Vector3.Reflect(this.direction, Vector3.left);
}
else if (posX - this.radius - this.container.safeZone < containerPosition.x - containerScale.x / 2)
{
posX += 0.01f;
this.direction = Vector3.Reflect(this.direction, Vector3.right);
}
if (posY + this.radius + this.container.safeZone > containerPosition.y + containerScale.y / 2)
{
posY -= 0.01f;
this.direction = Vector3.Reflect(this.direction, Vector3.down);
}
else if (posY - this.radius - this.container.safeZone < containerPosition.y - containerScale.y / 2)
{
posY += 0.01f;
this.direction = Vector3.Reflect(this.direction, Vector3.up);
}
if (posZ + this.radius + this.container.safeZone > containerPosition.z + containerScale.z / 2)
{
posZ -= 0.01f;
this.direction = Vector3.Reflect(this.direction, Vector3.back);
}
else if (posZ - this.radius - this.container.safeZone < containerPosition.z - containerScale.z / 2)
{
posZ += 0.01f;
this.direction = Vector3.Reflect(this.direction, Vector3.forward);
}
this.transform.position = new Vector3(posX, posY, posZ) + this.direction * speed * dt;
}
}
}