weird problem with hit/ raycast.

using UnityEngine;
using System.Collections;

public class ConstructionSpacestation : MonoBehaviour
{
    public GameObject Builder;
    private Transform actualBlock;
    
    void Update()
    {
        
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 2000) )
            {
                
                Transform actualBlock = hit.transform;
                Debug.Log(actualBlock);

                actualBlock.position = hit.point + hit.normal * 0.5f;
                actualBlock.transform.position = new Vector3(
                    Mathf.Round(actualBlock.position.x),
                    Mathf.Round(actualBlock.position.y),
                    Mathf.Round(actualBlock.position.z)
                );

                GameObject Placement = Instantiate(Builder, actualBlock.position, Quaternion.identity) as GameObject;
                                   
            }
            else
            {
                Debug.Log("nothing");
            }
        }
    }
}

I hacked together this script to instantiate a cube right next to another. But it is spitting them out at odd places, and also deleating cubes from behind.
Can you explain what Is going on? is it the hit.point that is screwing up?!

2 Answers

2

When you say

Transform actualBlock = hit.transform;

You am making the block that you are pointing at the actualBlock.

Then you say

actualBlock.position = hit.point + hit.normal * 0.5f;
                actualBlock.transform.position = new Vector3(
                    Mathf.Round(actualBlock.position.x),
                    Mathf.Round(actualBlock.position.y),
                    Mathf.Round(actualBlock.position.z)
                );

Which moves the original block you are pointing at before you ever Instantiated.

I think what you want is to create a new Vectore based on the blocks position. like

Vector3 newBlockPos = hit.point…bla bla bla.

Yeah I did the exact same thing that you just posted 'I just called the transform var newbloc'. I dont understand how I can move a prefab when its already instantiated. I really appreciated your answers.

I tried doing what you said... Its not working out for me.. I am trying to convert js to c# and I can get the placement right but then it does this.

yea you don’t want to use hit.point because im guessing you want a kind of minecraft thing.

the thing is hit.point is actually where on the side you hit. assuming your using the default cube

also alot of what your using is implicit in the code. like you dont need a distance for the raycast or a quanternion.identity. you also dont need to round really.

if(physics.Raycast(ray,out hit)
{
   instantiate(Builder,hit.transform.position + hit.normal)
}

I appreciate your answer but it's doing the exact same thing. And yes I do want a minecraft style. Would it be better to use a joint system? I also need to move all these blocks around later.

whats debug.drawline(hit.transform.position, hit.transform.position + hit.normal); show