I'm currently developing a game in Unity 3D with c#. I developed some levels, and now I want to make some levels with auto moving cubes(the thematic of the game is cubes). I searched a lot on the internet but I don't find nothing wich satisfy me. Can someone help me? I really need help. Sorry if there are some grammare errors.
Asked
Active
Viewed 1,697 times
0
-
1What do you mean by slide "left and right"? Are they moving from one given position to the other and back? – Max Play Jul 04 '17 at 18:37
-
I don't now if exactly that. I want to make a cube move left and right, smoothly, and never stop. Can you please help me? I can use .AddForce? – MattDiamond Jul 04 '17 at 18:40
-
1Could you please specify what your intentions are? There are numerous ways to do what you want, but if you don't specify what you need, no one will help you. Maybe you could create a scribble and upload it, so it is easier to understand your target. – Max Play Jul 04 '17 at 18:42
-
Well, I will try to explain better. I have a 3d game made with Unity3d and C#.I have maked some levels and now I want to make a level when there are some cubes that can go from left to right. So start from a point and then when a certain distance or a certain point is hitted, the cube will go back to the starting point and it need to make that infinite times. – MattDiamond Jul 04 '17 at 18:44
1 Answers
1
Create a new Script an name it SimpleTranslator.cs then copy and paste the below code.
using UnityEngine;
namespace TransformUtility
{
public class SimpleTranslator : MonoBehaviour
{
[Tooltip("The local target position towards we translate this gameObject. A red line is drawn.")]
public Vector3 m_localTargetPosition = new Vector3(0, 0, 5);
public float speed = 1;
public bool pingPong;
public bool translateOnAwake = true;
public new AudioSource audio;
Vector3 m_initialPosition, m_targetPosition;
Transform m_transform;
void Awake()
{
m_transform = transform;
m_initialPosition = m_transform.position;
SetTargetPosition(m_localTargetPosition);
enabled = translateOnAwake;
}
void FixedUpdate()
{
if (audio && !audio.isPlaying)
audio.Play();
m_transform.position = Vector3.MoveTowards(m_transform.position, m_targetPosition, speed * Time.deltaTime);
if (m_transform.position == m_targetPosition)
{
if (pingPong)
{
SwitchDirection();
}
else
{
enabled = false;
if (audio)
audio.Stop();
}
}
}
public bool isTranslating
{
get
{
return enabled;
}
}
public void SwitchDirection()
{
enabled = true;
if (m_transform.position == m_initialPosition)
{
SetTargetPosition(m_localTargetPosition);
}
else
{
m_targetPosition = m_initialPosition;
}
}
public void MoveToTargetPosition()
{
enabled = true;
SetTargetPosition(m_localTargetPosition);
}
public void MoveToInitialPosition()
{
m_targetPosition = m_initialPosition;
enabled = true;
}
public bool isInInitialPosition
{
get
{
return m_transform.position == m_initialPosition;
}
}
public bool isInTargetPosition
{
get
{
return m_transform.position == m_initialPosition + m_transform.TransformDirection(m_localTargetPosition);
}
}
private void SetTargetPosition(Vector3 localPosition)
{
m_targetPosition = m_initialPosition + transform.TransformDirection(localPosition);
#if UNITY_EDITOR
m_endPositionDebug = m_targetPosition;
#endif
}
#if UNITY_EDITOR
Vector3 m_endPositionDebug;
void OnDrawGizmos()
{
if (!Application.isPlaying)
{
Debug.DrawRay(transform.position, transform.TransformDirection(m_localTargetPosition), Color.red);
}
else
{
Debug.DrawLine(m_initialPosition, m_endPositionDebug, Color.red);
}
}
#endif
}
}

COBO
- 377
- 1
- 5
-
And that script I should put to the cube wich I want to make move? – MattDiamond Jul 04 '17 at 19:07
-
-
Thanks for the reply. But my cube go only in one direction, it does not come back and the go another time to the previous direction. How I make something like this? – MattDiamond Jul 04 '17 at 19:12
-
-
I use that scrip in a lot of projects. Select the cube with the script attached an check the Ping Pong property in the Editor – COBO Jul 04 '17 at 19:16
-
-
-
-