- Home /
How do I make a block move to another blocks position
WOW lots of different attempts on how to do this(previous questions) this question might work. I have a block(call it block1) and I want it to appear in the other block(block2) position when I look at it no matter where it is(with the first person view).
I think I understand your question, however if you fully explain your needs it will be easier to help you.
Answer by vk007 · May 26, 2015 at 06:24 AM
Use a raycast from the player camera to determine what the player is looking at, and then call a sendMessage() on that object. Then intercept the send message in a script on your Block1 so that it moves to Block2.
Because you asked for a script (though I highly recommend also learning these things for yourself, and in general the answers are not a place to get other people to write scripts for you):
In your camera controller put
function Update()
{
var hit: RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit)) //returns a hit object
{
//tell the hit object it was looked at
hit.transform.SendMessage("Look", SendMessageOptions.DontRequireReceiver); //so we don't get errrors if the message is not caught
}
}
And then in your box object have something like:
var box2: GameObject; //Put box2 into the inspector for this variable
function Look()
{
transform.postion = box2.transform.position; //move the current object, Box 1 to box 2
}
And that should do it.
Added example code, it's not tested but should be roughly right.
Your answer
Follow this Question
Related Questions
How to make a block have highlighted lines like it does in minecraft 2 Answers
Change scripted gui text postion 0 Answers
Button on joystick = move forward 1 Answer
move to a position not being looked at 1 Answer
Vector3.lerp doesn't work 1 Answer