- Home /
How do you instantiate a prefab with a rotation based on the side of a cube you click on?
Specifically, I'm trying to make the little black outline on the face of a cube, as in Minecraft when you mouse over a block.
I can get it to instantiate perfectly in place on the top of a cube, but for the sides, it won't rotate properly to match the cube.
My code is this so far:
//=================================
//Declareth thine variables!
var grassBlock : Transform;
var stoneBlock : Transform;
var blockOutline : Transform;
var blockSelected : float = 1;
var range : float = 4;
function Update ()
{
//==============================
//Block outline
var blockOutlineRay : RaycastHit;
var blockRotation = Quaternion.FromToRotation(Vector3.forward,
blockOutlineRay.normal);
if(Physics.Raycast(transform.position,
transform.forward,
blockOutlineRay,
range))
{
var blockOutline : Transform = Instantiate(blockOutline,
blockOutlineRay.collider.transform.position + (blockOutlineRay.normal.normalized * .25),
blockRotation);
blockOutline.tag = "Clone";
Destroy(GameObject.FindWithTag("Clone"), .000000025);
}
Destroy(GameObject.FindWithTag("Clone"), .0000005);
//===============================
//Block Selector
if(Input.GetKeyDown(KeyCode.Q))
{
blockSelected -= 1;
if(blockSelected == 0)
{
blockSelected = 2;
}
}
if(Input.GetKeyDown(KeyCode.E))
{
blockSelected += 1;
if(blockSelected == 3)
{
blockSelected = 1;
}
}
//===============================
//Mouse Click
if(Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
var hit : RaycastHit;
var lookingDirection = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position,
lookingDirection,
hit,
range))
{
if(Input.GetMouseButtonDown(1))
{
if(blockSelected == 1)
{
var grassBlock : Transform = Instantiate(grassBlock,
hit.collider.transform.position + (hit.normal.normalized * .5),
Quaternion.identity);
grassBlock.tag = "GrassBlock";
}
if(blockSelected == 2)
{
var stoneBlock : Transform = Instantiate(stoneBlock,
hit.collider.transform.position + (hit.normal.normalized * .5),
Quaternion.identity);
stoneBlock.tag = "StoneBlock";
}
}
else
{
Destroy(hit.transform.gameObject);
}
}
}
}
Oh right the code would help, huh? lol Basically I just need an indicator of what side of a cube you are looking at. As I have it now, if the raycast points at the top of a cube, it'll show symbol on the top face of the cube. Point at one of the side faces, the symbol shows up on the side, but it's not rotated to match the face like it's supposed to.
http://www.youtube.com/watch?v=4UdEFmxRmNE
This is the first video I found. About $$anonymous$$ute and a half in, you see them walking around in the snow and a little black box shows up on the ground.
On line 17, you use Vector3.forward. Does this mean the face of blockOutline when it has a rotation of (0,0,0) is towards positive 'Z'?
No, it's towards the Y. $$anonymous$$aybe I should try Vector3.up?
Edit: That didn't work either.
This script goes on the player camera, FPS style. So lookingDirection is just checking which direction is forward to the player. The //$$anonymous$$ouse Click section is working just fine, its the //Block Outline section that sucks, specifically the blockRotation variable.
I'm sorry, I mean that you are calculating blockRotation before you make the Raycast() call.
Answer by robertbu · Jul 24, 2013 at 01:49 AM
It looks like the major problem with your code is that you are calculating blockRotation before you call Physics.Raycast(). The normal you are aligning to does not get set until after call Physics.Raycast(). Also if you are using a standard Unity plane for your block outline, you need to remove the mesh collider, or place it on a separate and use a layer mask for your Raycast().
Here is a bit of test code based off your code. It takes a standard plane and aligns it with the normal of the object under the mouse position. Start a new scene, put one or more cubes in the scene, attach this script to an empty game object, create a standard plane with some texture, initialize the blockOutline variable with the plane.
var blockOutline : Transform;
var range : float = 14.0;
function Update ()
{
var blockOutlineRay : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, blockOutlineRay, range)) {
blockOutline.renderer.enabled = true;
var blockRotation = Quaternion.FromToRotation(Vector3.up,
blockOutlineRay.normal);
blockOutline.position = blockOutlineRay.transform.position + blockOutlineRay.normal * .5;
blockOutline.rotation = blockRotation;
}
else {
blockOutline.renderer.enabled = false;
}
}
I don't know what you are doing with your code, but I wonder if you cannot just hide/show and move the plane as I've done in this code here rather than instantiate and destroy.
Your answer
Follow this Question
Related Questions
Cannot instantiate rotation on Prefab 1 Answer
How to instantiate on custom rotation? 1 Answer
Quarternion Rotation 1 Answer
Rotate object in the direction where it is going 1 Answer
instant rotate an object 3 Answers