# Move a cube in 'x' direction 5 units everytime i press a button, Help me !

Hii,

This is getting on my nerve. I want to do a simple thing -

In the scene, there is a wall [take it as a cube] and a floor[take it as a plane]

Now on this a box is kept [attach rigidbody with 0 gravity to this box [take it as a cube] ]

Now what i need is-
If i click a button Ongui, this box moves 5 units in the direction of the wall, but it should not intersect or cross the wall.

I want to make 2 more buttons to scale and rotate, but again cube should not intersect this wall in these 3 conditions.

Can anybody help me with some basic code for this ?
I have been trying to find solution for this since 4 days.

I would set a bool to true if the gui button is click, find the cube with gameObject.Find and translate it 5 units in the direction of the wall

so something like :

OnGUI(){

if(GUI.button(…)){
moveCube=true;
}
}

UpDate(){

if(moveCube){

Cube=gameObject.Find(“theCube”);
Cube.transform.Translate(Vector3(5,0,0));

}
}

with moveCube as a boolean variable, Cube a GameObject variable and your gameobject in the scene called theCube…

dunno if this will work, kind of late here now, if it’s not answered tomorrow I can have a beter look… but I’m not a pro coder so

nopes, translate wont work, it will let the cube intersect the wall, something else needed to be done

Here’s an idea:

If the block moves 5 units at a time, and the block is 20 units away from the wall, you could do something like this:

``````OnMouseDown(1)
{
if(i <= 4) {
// not sure how to code the translation, but this would be where you'd move the box 5 units.
transform.translate.x + 5
i++
}
}
``````

That way you can’t move the box more than 4 times, and therefore it wont pass the wall! I don’t know if that would work in your case, but thats one way of making sure the box doesn’t make it that far.

then do a OnCollision where it sets the cube back one unity if it collides with the wall or use Physics.OverlapSphere or use one of the Rigidbody.AddForce’s to move the cube?

@jister this is a good idea, can u help me to code the button to move it , this way i think it will be done

any help !!

Need to move green cube towards red cube, attached rigidbody to green cube

i have added this code to my cube but its not moving

``````using UnityEngine;
using System.Collections;

public class move : MonoBehaviour {

void OnGUI()
{
if (GUI.Button(new Rect(60, 200, 60, 20), "Move"))
{
Debug.Log("clicked");
}

}

}
``````

IF you are using two cubes Please attach Rigidbody with both add tag to each . then please try this code.

``````var moveCube:boolean;

function OnGUI()
{
if(GUI.Button(Rect(0,50,100,100),"Test"))
{
moveCube=true;
}
}

function Update()
{
if(moveCube)
{

Cube=gameObject.Find("Cube");

Cube.transform.Translate(Vector3(0,-5,0));

moveCube=false;

}
}

function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag == "Cube")
{
Debug.Log("Testing here");
}
}
``````

try setting it with the boolean in OnGUI and the adding the force in Update, i can’t try it now, my PC is sweating from rendering a HighPoly scene atm. will try it as soon as i have time.

If you are wanting to move an object a specific distance, forces are not going to be an ideal solution. Forces also require force to be applied over several frames, rather than just on the one-click like youve coded.

Translate is generally what you want.

If its moving too far, that means the cube isnt 1x1x1 or its scale isnt.

Like JamesLeeNZ said generally you’d use translate which is accurate. force isn’t, it’s base on physics…

anyway see if this is what you want

``````using UnityEngine;
using System.Collections;

public class moveCube : MonoBehaviour {

private bool MoveTheCube = false;
public Rigidbody TheCube;
public Vector3 TheForce;

void OnGUI(){

if (GUI.Button(new Rect(60, 200, 60, 20), "Move")){

MoveTheCube = true;

}
print (MoveTheCube);

}

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if(MoveTheCube){

MovingTheCube();

}

}

void MovingTheCube(){

MoveTheCube = false;

}
}
``````

I made TheForce Public because i don’t know what your goal is of moving the cube. set the vector you wanna move on to 100 or more to get any movement…

it should look something like

``````public class CubeBehaviour : MonoBehaviour
{
void OnGUI()
{
if(GUI.Button(new Rect(1,1,100,20),"Move Cube"))
{

Vector3 l_tempPosition = transform.position;
Vector3 l_tempOffset =	getNextPosition(l_tempPosition,transform.TransformDirection(Vector3.forward),4);
transform.position = l_tempPosition + l_tempOffset;
}
}

internal Vector3 getNextPosition(Vector3 origin,Vector3 direction,float offset)
{
RaycastHit l_collisionInfo = new RaycastHit();
int layerMask  = ~(1 << 2);
if (Physics.Raycast ( origin,direction,out l_collisionInfo,offset+gameObject.collider.bounds.extents.z ,layerMask))
{
if(gameObject.collider.bounds.extents.z+offset<l_collisionInfo.distance)
{
return direction * offset;
}
else
{
return direction * (l_collisionInfo.distance-gameObject.collider.bounds.extents.z);
}
}
return direction*offset;
}
}
``````