using UnityEngine;
using System.Collections;
public class RayCast : MonoBehaviour {
public GameObject building;
public Material buildingHighlightedMaterial;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0))
{
RaycastHit buildingPlacement;
Ray ray = new Ray ( transform.position, transform.forward);
if ( Physics.Raycast(ray, out buildingPlacement))
Debug.DrawRay (ray.origin, ray.direction * 5000, Color.red);
{
Instantiate (building, buildingPlacement.point, Quaternion.identity);
if (Physics.Raycast ( ray, out buildingPlacement ) == true )
{
building.GetComponent<Renderer>().material = buildingHighlightedMaterial;
}
}
}
}
}
So I’ve got a bit of a simpler question for you guys, I’m experimenting with raycasting right now and I’ve gotten instantiation to work okay with the raycast but there’s just one problem. For some reason any objects I instantiate be they simple polygon cubes or particle effects end up halfway inside the object they’re instantiating to.
I’ve tried it with a simple plane, a cube with a bit more depth in it and I’ve tried increasing the colliders to try and force the object onto the surface but unfortunately no joy so with my limited knowledge of raycasting I’m running out of ideas. Decreasing the draw distance of the raycast also seems to have no affect.
Oh and woops I left some code in there, I’m also messing around with the idea of changing materials based on what my raycast hits very much like when you place stuff in an RTS or survival builder and the building turns red or green. So that’s what that little bit of extra code is.
RaycastHit.Point is the exact point on the collider that the raycast hits, and the object you instantiate will have that point as it’s center - which would explain why things are - literally - halfway inside other objects
Thanks for the quick response! Okay, I’m having a search around now for some answers, looks like there’s already people who have posted stuff up about this.
Bah, well I know how to fix it now, but I just got to get the right explanation, I need to use RaycastHit.normal rather than .point, however when I replace .point with .normal they cubes now just instantiate underneath my objects and refuse to go anywhere else .
Going to keep searching but I would’ve thought this is an easy fix lol.
Okay, I think I found a legitimate solution after searching about.
This makes total sense, I’m just having a bit of an annoyance adapting it to my code without simply copy and pasting this persons answer, I don’t want to use anything like tags, just have it check objects and not instantiate inside them.
Here’s what I’m messing with so far, the answer provided makes total sense, it also explains why I couldn’t just go around changing stuff up randomly within instantiate.
using UnityEngine;
using System.Collections;
public class RayCast : MonoBehaviour {
public GameObject building;
public Vector3 specificVector;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = new Ray ( transform.position, transform.forward);
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition ), out hit ))
{
specificVector.Set (hit.point.x, hit.collider.transform.position.y, hit.point.z);
building.transform.position = specificVector;
Instantiate(building, specificVector , Quaternion.identity );
Debug.DrawRay (ray.origin, ray.direction * 5000, Color.red);
}
}
}
}
Just thought I’d upate this thread a little to let people know that I found a nicely detailed answer in Unity answer’s history.
As it far as I understand it, the trick seems to be to have the raycast check not the collider but a certain distance away from the collider and you need to make sure to attach this script to the object itself so that the object understands where it is in relation to the terrain which makes perfect sense because you’ve got the mouse position dictating the X and Z axis and then the object’s raycast detects the Y axis so it doesn’t come into conflict with the instantiation code on your other script for positioning the cube.
Presumably you could also duplicate this exact code for rotation and the X and Z position axis if you were worried about doing it on a spherical object for example.
Now I’ve added in this code to my instantiated object, the objects are pretty much placing perfectly and the best part is you don’t need resource consuming grids or rigidbodies to do this!