Hi, How can you create a box whereby you can change xz values of a GameObject in runtime?
Hi, How can you create a box whereby you can change xz values of a GameObject in runtime?
Answer by fredz0004 · Nov 23, 2015 at 07:34 AM
I don't see any code, so I would imagine you are a beginner. No worries. You will need to create a simple script, and attach it to the cube. In the inspector you would just modify those value as you wish. The reason why this is in update() is because it is called on every frame.
The position of a game object is accessed through "Transform".
http://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html http://docs.unity3d.com/ScriptReference/Transform.html
using UnityEngine; using System.Collections;
public class Cube : MonoBehaviour {
// Variables
public float positionX;
public float positionY;
public float positionZ;
void Update() {
gameObject.transform.position(positionX, positionY, positionZ);
}
}
I hope this helps!
Edit 1: Ok what you are looking for then is GUI class. You would probably need a TextArea or a TextField for entering text. And then maybe a button if you want it to only send when you press a button.
http://docs.unity3d.com/ScriptReference/GUI.html
A sample script is below:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float xPositionPlayer;
public string xPositionString;
void OnGUI() {
xPositionString = GUI.TextField(new Rect(10, 10, 200, 20), xPositionString, 25);
if (GUI.Button(new Rect(10, 10, 50, 30), "New X position"))
Debug.Log("Send X parameter to the player");
xPositionPlayer = float.Parse(xPositionString);
}
}
Note that the TextField is a string, and thus why it needs to be parsed to a float. Just pass the xPositionPlayer to player, and also you might need to play around with the Rect values. Also FYI if you really need to get good with GUI stuff there are several tutorials on YT.
Answer by Aaron Lieberfreund · Nov 23, 2015 at 07:40 AM
Sorry, let me explain it better. I don't want to modify the xyz values. What I want to do is create a box in the game view whereby the player can enter a value whilst playing and go to that coordinate.
What exactly do you mean by "creating a box"? Are you talking about cubes, a plane? You need to be more specific. Any gameObject has the transform.position by the way. I think you are talking about having a text input on the screen for the player to move around the world. But I'm not sure your descriptions are vague.
Yes. A text input on the screen whereby a player can modify the transform values.
I updated my answer, for future reference just make a comment ins$$anonymous$$d of an answer. And try to be very descriptive on your question, I know sometimes is hard to describe things when you are a beginner.