Adjusting an object's position

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;
    }
}

1 Answer

1

There are some problems here, plus I’m confused about some things. First the top script will not compile. ‘_cubeObject’ and ‘moveBlockUp’ are not defined. It looks to me that you are in the middle of hacking on this code when you posted it, but that makes it difficult someone trying to help you. Second, OnGUI gets called multiple times per frame. How many times varies based on the available events. So your MoveHorizontally() and MoveVertically() are not going to move consistently in all conditions. You can hack a fix by only running your switch statement when the GUI event is of EventType.Repaint, but the better fix is to move the switch code into Update(). Probably that update would be in the first script not the second one.

I’m confused about the Vector3 parameter you are passing to MoveHorizontally() and MoveVertically(). In the first line of each function, you overwrite this value. So either you need to remove the parameter, or ???.

As for resetting the position, I don’t see any code that should do that reset. One options is to add a method like:

public void ResetPos(Vector3 newPos) {
   _someObject.transform.position = newPos;
}

You would call this just above line 55 in the second script.


I don’t know your final purpose here, but if it were me I’d encapsulate all the code for movement into a single script that is attached to the object that is to move. I would expose a single public function for switching between horizontal and vertical movement.

@robertbu: Thanks for the help it worked!