Here is the entire code and the editor windows in unity
The idea is that a bingo ball rolls down the slope, hits the ball on the first hexagon and causes it to bounce up to the middle hexagon…and so on. And all I am doing is passing the numbered ball textures along the chain.
I am open to suggestions for simplifying it.
m_vectStartPos contains the localPosition of the sphere on top of the shortest hexagon and m_vectEndPos contains the position of the sphere on top of the middle hexagon.
OR
m_vectStartPos contains the localPosition of the sphere on top of the middle hexagon and m_vectEndPos contains the position of the sphere on top of the tallest hexagon.
m_vectOrigPos contains the localPosition of one of those spheres hanging in mid air.
The script is re-useable in both cases - I saw no sense in writing two scripts with all but identical code.
All that is important in the parabola coding is that you have a start point and an end point.
The script below is attached to those two spheres hanging in mid air so to speak - I put them over there because I was trying OnTriggerEnter etc and I was having problems unless I moved them away from the spheres on top of the hexagons. I am no longer using OnTriggerEnter etc but I have left it as is.
So when the roller ball hits the sphere sitting on the first hexagon its script (not included in this post) does the following:
- Hides the itself.
- Changes its numbered texture to the new one from the roller ball.
- Sends its previous numbered texture to the first ‘jump’ ball hanging in mid air.
Now the script attached to the jump ball is triggered and it does the following:
- Moves itself to the same position as the ball on the first hexagon (m_vectStartPos).
- Moves itself in a parabola up to the top of the second hexagon until its X values is >= m_vectEndPos.x.
- Hides itself and moves itself back into the mid air position (m_vectOrigPos)
- Sends it numbered texture to the ball sitting on top of the second hexagon.
Now the script attached to the ball sitting on top of the second hexagon is triggered and it:
- Changes its numbered texture to the one sent by the jump ball.
- Shows itself.
public class RecentBallJumpManager : McCormickMonoBehaviourBase
{
private bool m_bMove = false;
private float m_fX = 0.0f, m_fY = 0.0f;
private Vector3 m_vectOrigPos, m_vectStartPos, m_vectEndPos, m_vectMoveTo;
private Material m_materialRecentBall = null;
private GameObject m_gameobjRecentBall = null;
private void GetDetails(string strGameObjectName)
{
GameObject gameobj1 = null, gameobj2 = null;
if (gameObject.name == "RecentBall1Jump")
{
gameobj1 = FindGO("RecentBall1");
gameobj2 = FindGO("RecentBall2");
}
else if (gameObject.name == "RecentBall2Jump")
{
gameobj1 = FindGO("RecentBall2");
gameobj2 = FindGO("RecentBall3");
}
m_gameobjRecentBall = gameobj1;
if ((gameobj1 != null) && (gameobj2 != null))
{
m_vectStartPos = gameobj1.transform.localPosition;
m_vectEndPos = gameobj2.transform.localPosition;
m_vectOrigPos = transform.localPosition;
}
}
// Use this for initialization
protected override void Start()
{
base.Start();
m_materialRecentBall = GetComponent<Renderer>().material;
GetDetails(gameObject.name);
DoShow(false);
}
private GameObject GetRecentBall(string strRecentBallName)
{
GameObject gameobj = null;
if (strRecentBallName == "RecentBall1Jump")
gameobj = FindGO("RecentBall2");
else if (strRecentBallName == "RecentBall2Jump")
gameobj = FindGO("RecentBall3");
return gameobj;
}
protected override void DoReceiveMessage(Parameters p)
{
if (p.m_nMsgID == BingoMessageIDEnum.MSGID_SET_BALL)
{
Texture2D textureCurrent = (Texture2D)m_materialRecentBall.GetTexture("_MainTex"),
textureNew = (Texture2D)p.m_objParam1;
if (textureNew != null)
{
m_materialRecentBall.SetTexture("_MainTex", textureNew);
DoShow(true);
m_bMove = true;
transform.localPosition = m_vectStartPos;
m_fX = m_vectStartPos.x;
}
if (textureCurrent != null)
{
m_parameters1.New(BingoMessageIDEnum.MSGID_SET_BALL, textureCurrent, gameObject.name);
GameObject gameobj = GetRecentBall(gameObject.name);
if (gameobj != null)
SendMessage(gameobj, m_parameters1);
}
}
base.DoReceiveMessage(p);
}
private float QuadraticY(float fX)
{
float fY = 0.0f;
if (gameObject.name == "RecentBall2")
fY = (-0.7f * fX * fX);
else
fY = (-2.0f * fX * fX) + (1.5f * fX) + m_vectStartPos.y;
return fY;
}
private float GetY(float fX)
{
float fY = 0.0f;
if (fX >= m_vectEndPos.x)
{
fY = -1000.0f;
}
else
{
fY = QuadraticY(fX);
fX += 0.01f;
}
return fY + m_vectStartPos.y;
}
private void EndMove()
{
m_bMove = false;
DoShow(false);
transform.localPosition = m_vectOrigPos;
Texture2D textureCurrent = (Texture2D)m_materialRecentBall.GetTexture("_MainTex");
m_parameters1.New(BingoMessageIDEnum.MSGID_SET_BALL, textureCurrent, gameObject.name);
SendMessage(GetRecentBall(gameObject.name), m_parameters1);
}
protected override void Update()
{
if (m_bMove)
{
m_fY = GetY(m_fX);
if (m_fY <= -1000.0f)
EndMove();
else
{
m_vectMoveTo.x = m_fX;
m_vectMoveTo.y = m_fY;
m_vectMoveTo.z = m_vectStartPos.z;
transform.localPosition = m_vectMoveTo;
m_fX += 0.01f;
}
}
}
}