I am working on this game for school and I got an object (a cube) and attached a script that which lets the object switch between moving horizontally and vertically. That part works perfectly but for some reason I can’t get it to reset its position once I hit the toggle button.
Here is a diagram of what I want it to do, hope it helps:
[19252-cube+orientations.png|19252]
And here is the code I am working on. First the class that handles animating the 3d object class:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class AnimateObject : MonoBehaviour
{
//we must attach an object
public GameObject _someObject;
bool slideObjectLeft, slideObjectRight,
moveObjectUp, moveObjectDown;
public float speed;
/// <summary>
/// Initialize game components here
/// </summary>
void Awake()
{
_someObject = GameObject.FindGameObjectWithTag("PlayerObject");
slideObjectLeft = false;
slideObjectRight = true; //starts moving it by default
moveObjectUp = true; //starts moving it by default
moveObjectDown = false;
}
public void MoveHorizontally(Vector3 newPos)
{
newPos = _someObject.transform.position;
if (slideObjectRight)
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
if (newPos.x > 1.0f)
{
slideObjectRight = false;
slideObjectLeft = true;
}
}
if (slideObjectLeft)
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
if (newPos.x < -1.0f)
{
slideObjectRight = true;
slideObjectLeft = false;
}
}
}
public void MoveVertically(Vector3 newPos)
{
newPos = _someObject.transform.position;
if (moveObjectUp)
{
transform.Translate(Vector3.up* speed * Time.deltaTime);
if (newPos.y > 2.0f)
{
moveObjectUp = false;
moveObjectDown = true;
}
}
if (moveObjectDown)
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
if (newPos.y < 1.0f)
{
moveObjectUp = true;
moveObjectDown = false;
}
}
}
}//end of line
Lastly, the GUI class that handles toggling the animations:
using UnityEngine;
using System.Collections;
/// <summary>
/// This class handles the GUIs that will be displayed on the screen
/// </summary>
[ExecuteInEditMode]
public class TaskWindow : MonoBehaviour {
public enum myTasks { task1, task2, finish }
public static myTasks currentTask = myTasks.task1;
AnimateObject cs_animateObject;
public Rect GUIRectWindow;
public Rect lbl_msg, btn_Begin;
int taskCounter;
void Awake()
{
cs_animateObject = GameObject.FindGameObjectWithTag("PlayerCube").GetComponent<AnimateObject>();
taskCounter = 0;
}
void OnGUI()
{
// Make a background box
GUI.Box(GUIRectWindow, "");
switch (currentTask)
{
case myTasks.task1:
drawLabel(lbl_msg, "Moving Horizontally. Click 'Next' when ready");
cs_animateObject.MoveHorizontally(new Vector3(-1, 1, 0));
break;
case myTasks.task2:
drawLabel(lbl_msg, "Moving Vertically. Click 'Next' when ready");
cs_animateObject.MoveVertically(new Vector3(0, 1, 0));
break;
case myTasks.finish:
Application.Quit();
break;
}
if (drawButon(btn_Begin, "Next"))
{
taskCounter++;
currentTask = (myTask)taskCounter; //iterates through the enum list
}
}
bool drawButon(Rect rect, string buttonName)
{
if (GUI.Button(new Rect(GUIRectWindow.x + rect.x, GUIRectWindow.y + rect.y, rect.width, rect.height), buttonName))
{
return true;
}
return false;
}
}
@robertbu: Thanks for the help it worked!
– mmangual_83