I am trying to make a 3D sphere appear to roll down a slope.
For reasons I don’t think I need to go into, I am not using any other 3D objects, gravity or rigid bodies etc.
Basically the sphere is one 3D obect among a whole lot of sprites.
The sphere was created in Blender so the axes are arse about - to make it move in the y direction in Unity I need to manipulate the sphere’s z axis.
The point is that I can make the ball either roll in forward direction or slide down the slope, but I can’t figure out how to do both at the same time.
gameObject.transform.RotateAround(m_collider.bounds.center, Vector3.back, 5.0f);
gameObject.transform.Translate(GetTransVector());
If I comment out the first line, the sphere slides down the slope nicely.
If I comment out the second line the sphere rotates about its centre nicely.
But both together then the sphere rotates in a circle on the spot.
Any suggestions?
private Vector3 GetTransVector()
{
if (gameObject.transform.localPosition.x >= (m_gameObjectRecentBall1.transform.localPosition.x))
{
m_bStartRoll = false;
DoShow (false);
gameObject.transform.localPosition = m_vect3OriginalPos;
m_vect3DeltaTrans.x = m_fDeltaX;
m_vect3DeltaTrans.y = 0.001f;
m_vect3DeltaTrans.z = m_fDeltaY;
m_parameters1.New (BingoMessageIDEnum.MSGID_NEW_BALL_SPRITE, m_spriteBall);
SendMessage ("LastThreeBalls", BingoMessageIDEnum.MSGID_NEW_BALL_SPRITE, m_parameters1);
}
else if (gameObject.transform.localPosition.x >= (m_gameObjectRecentBall1.transform.localPosition.x - 0.95f))
m_vect3DeltaTrans.z = 0.0f;
return m_vect3DeltaTrans;
}
protected override void Update()
{
if (!IsHidden() && m_bStartRoll)
{
gameObject.transform.RotateAround(m_collider.bounds.center, Vector3.back, 5.0f);
gameObject.transform.Translate(GetTransVector());
}
}
The rest of the code if needed:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RollerBallManager: McCormickMonoBehaviourBase
{
private GameObject m_gameObjectRecentBall1 = null;
private Vector3 m_vect3OriginalPos = new Vector3();
private float m_fDeltaY = 0.004f, m_fDeltaX = 0.02f;
private Vector3 m_vect3DeltaTrans = new Vector3(0, 0, 0);
private bool m_bStartRoll = false;
private List<Texture2D> m_arrayTextureBalls = new List<Texture2D>();
private Sprite m_spriteBall = null;
private SphereCollider m_collider = null;
protected override void Start()
{
string strTexturePath = "Textures/Ball";
Texture2D texture = null;
for (int nI = 1; nI <= 75; nI++)
{
texture = Resources.Load<Texture2D>(strTexturePath + nI.ToString("00"));
m_arrayTextureBalls.Add(texture);
}
m_gameObjectRecentBall1 = GameObject.Find("RecentBall1");
m_vect3OriginalPos = gameObject.transform.localPosition;
m_vect3DeltaTrans.x = m_fDeltaX;
m_vect3DeltaTrans.y = 0.0f;
m_vect3DeltaTrans.z = m_fDeltaY;
m_collider = GetComponent<SphereCollider>();
DoShow(false);
}
private Vector3 GetTransVector()
{
if (gameObject.transform.localPosition.x >= (m_gameObjectRecentBall1.transform.localPosition.x))
{
m_bStartRoll = false;
DoShow (false);
gameObject.transform.localPosition = m_vect3OriginalPos;
m_vect3DeltaTrans.x = m_fDeltaX;
m_vect3DeltaTrans.y = 0.001f;
m_vect3DeltaTrans.z = m_fDeltaY;
m_parameters1.New (BingoMessageIDEnum.MSGID_NEW_BALL_SPRITE, m_spriteBall);
SendMessage ("LastThreeBalls", BingoMessageIDEnum.MSGID_NEW_BALL_SPRITE, m_parameters1);
}
else if (gameObject.transform.localPosition.x >= (m_gameObjectRecentBall1.transform.localPosition.x - 0.95f))
m_vect3DeltaTrans.z = 0.0f;
return m_vect3DeltaTrans;
}
protected override void Update()
{
if (!IsHidden() && m_bStartRoll)
{
gameObject.transform.RotateAround(m_collider.bounds.center, Vector3.back, 5.0f);
gameObject.transform.Translate(GetTransVector());
}
}
private Texture2D GetBallTexture(int nBallNum)
{
Texture2D textBall = m_arrayTextureBalls[nBallNum - 1];
return textBall;
}
protected override void DoReceiveMessage(Parameters p)
{
if (p.m_nMsgID == BingoMessageIDEnum.MSGID_START_ROLL)
{
m_spriteBall = (Sprite)p.m_objParam1;
Texture2D textBall = GetBallTexture((int)p.m_objParam2);
Material material = GetComponent<Renderer>().material;
material.SetTexture("_MainTex", textBall);
m_bStartRoll = true;
DoShow(true);
}
base.DoReceiveMessage(p);
}
}