- Home /
how to make Directional Blocks?
You know Minecraft right? If so, look, I try instant objects but they put their faces according to the angle the player is at like Minecraft when you put a kiln block, this always has the face where the player sees that's what I want to do sometimes it works with transform .rotation but this is not very precise I want it to be only 0, 90, 270, 180 in advance thank you very much for the help.
RaycastHit hit;
if (Physics.Raycast(camera.position, camera.forward, out hit, rayDistance))
{
//Debug.Log(hit.transform.name);
var select = hit.transform;
if (select.CompareTag(GameObjectTag))
{
var selectRenderer = select.GetComponent<Renderer>();
if (selectRenderer != null)
{
selectRenderer.material = highlightGameObjectMaterial;
if (Input.GetKey(KeyCode.Y))
{
Vector3 spawnSpot = hit.collider.transform.position + hit.normal;
//Instantiate(well, spawnSpot, Quaternion.identity);
//Intantiate(well,spawnSpot,Quaternion.Euler);
//Instantiate(well,spawnSpot,Quaternion.Euler(transform.forward));
//Instantiate(well, transform.position + (transform.forward), transform.rotation);
Instantiate(well, spawnSpot, transform.rotation);
}
}
selectedGameObject = select;
}
Answer by Gustav2302 · Sep 04, 2021 at 11:52 AM
When you instantiate a gameobject the third argument sets the rotation and right now you have set the rotation to be the same as that of the gameobject that holds the script (which is the same gameobject that holds the camera, i presume). Instead of using 'transform.rotation' (the rotation of the current gameobject), you should instead use 'Quaternion.Euler(x, y, z)', where you define the rotation with the x, y, and z values. So line 21 should look something like this:
Instantiate(well, spawnSpot, Quaternion.Euler(x, y, z));
@Gustav2302 yes, I understand, but QuaternionEuler (0.90.0); I already tried and I am only going to instantiate only pure objects with that angle which is (0,90,0) = 90 and I want it to depend on where the player is seen as if they were the cardinal points 0, 90, 270, 180 beforehand thanks bro is appreciated
I see. Then it's just:
Instantiate(well, spawnSpot, Quaternion.Euler(0, Mathf.RoundToInt(transform.rotation.eulerAngles.y / 90) * 90, 0));
Edit: Spelling error
@Gustav2302 Sorry for the inconvenience bro, but Mathf 'does not contain a definition for' RountToInt ' create a variable?
Your answer
![](https://koobas.hobune.stream/wayback/20220613042549im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
transform.forward not updating? 3 Answers
Kinect controlled flight issue 1 Answer
Collision Not Working 0 Answers
How do i make my person move forward in the direction of the cursor 2 Answers