- Home /
Instantiated Objects not being set at ground/terrain level?(Solved)
Objects that I am instantiating into my game are being placed above the level of my terrain (not always the same height but about 6 - 7 units higher)
This is my script-
using UnityEngine;
using System.Collections;
public class ObjectToFollowMouse : MonoBehaviour
{
public GameObject Target;
public GameObject ActualPrefab;
RaycastHit hit;
private float raycastLength = 1000;
void Start()
{
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//Makes the 'Fake' Prefab follow the mouse.
if(Physics.Raycast(ray, out hit, raycastLength))
{
Debug.Log(hit.collider.name);
if(hit.collider.name == "Terrain")
{
Target.transform.position = hit.point;
}
}
//Instantiates Actual Building Prefab
if(Input.GetMouseButtonDown(1))
{
GameObject TargetObj = Instantiate(ActualPrefab, hit.point, Quaternion.identity) as GameObject;
TargetObj.name = "Target Instantiated";
//Destroys the Fake !
Destroy(gameObject);
}
}
}
How can I ensure the units/objects are always going instantiate at ground level.
???
ok.. get a few errors with that one -
Assets/Scripts/ObjectToFollow$$anonymous$$ouse.cs(31,88): error CS1061: Type UnityEngine.Transform' does not contain a definition for
y' and no extension method y' of type
UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)
Assets/Scripts/ObjectToFollow$$anonymous$$ouse.cs(31,48): error CS1502: The best overloaded method match for UnityEngine.Vector3.Set(float, float, float)' has some invalid arguments Assets/Scripts/ObjectToFollow$$anonymous$$ouse.cs(31,48): error CS1503: Argument
#2' cannot convert object' expression to type
float'
???
oh, sry, edited first comment. there must be
specificVector.Set(hit.point.x, hit.collider.transform.position.y, hit.point.z);
And I think u must get upper then ground (cause char is centered by Y-coordinate), so get him upper ("1" for ex, u need to get this variable by yourself).
specificVector.Set(hit.point.x, hit.collider.transform.position.y+1, hit.point.z);
Oh..so close
Objects instantiating perfectly at terrain level now. BUT... they are not following the mouse after the initial selection.
This part of the script as I had it made them follow the mouse until the player right clicked the mouse
//$$anonymous$$akes the 'Fake' Prefab follow the mouse.
if(Physics.Raycast(ray, out hit, raycastLength))
{
Debug.Log(hit.collider.name);
if(hit.collider.name == "Terrain")
{
Target.transform.position = hit.point;
}
}
And Im edited script again :)
Just add again:
Target.transform.position = specificVector;
cause u deleted specificVector ;) and your prefab instantiates in (0,0,0) just copy my script from first comment, i guess now it can works well
Answer by chariot · Feb 11, 2015 at 03:56 PM
using UnityEngine;
using System.Collections;
public class ObjectToFollowMouse : MonoBehaviour
{
public GameObject Target;
public GameObject ActualPrefab;
public Vector3 specificVector;
RaycastHit hit;
private float raycastLength = 1000;
void Start()
{
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//Makes the 'Fake' Prefab follow the mouse.
if(Physics.Raycast(ray, out hit, raycastLength))
{
Debug.Log(hit.collider.name);
if(hit.collider.name == "Terrain")
{
specificVector.Set(hit.point.x, hit.collider.transform.position.y, hit.point.z);
Target.transform.position = specificVector;
}
}
//Instantiates Actual Building Prefab
if(Input.GetMouseButtonDown(1))
{
GameObject TargetObj = Instantiate(ActualPrefab, specificVector, Quaternion.identity) as GameObject;
TargetObj.name = "Target Instantiated";
//Destroys the Fake !
Destroy(gameObject);
}
}
}
Answered script