- Home /
Question by
Quentin_Arragon · Dec 05, 2015 at 06:15 PM ·
raycastscreenpointtorayhit.point
Raycast.hitpoint always returning back to the camera
Hello,
Here is a video of my problem : https://youtu.be/F9hCJ9Y9Tcg
I have a ground, and I want to place a sort of building on it. In the game the player have to click on a button, choose the emplacement, and valid by re-clicking again. (like in any RTS game)
For that I use a Raycast, and I instantiate the building on the hitPoint. The problem is : the hitPoint go from the ground to the camera, and again, and again, ... And I can't understand why :/
If anyone can help me...
This is a part of my code :
void Update ()
{
if(lookingForSpot == true)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray, out hit))
{
Vector3 hitPoint = hit.point;
Vector3 constructionPoint = hit.barycentricCoordinate;
//hitPoint.y = 0.1f;
Debug.DrawLine(Camera.main.transform.position, hitPoint);
if(isCreated == false)
{
buildingNumber += 1;
GameObject clone;
clone = Instantiate(statueA, hitPoint, Quaternion.Euler(270, 200, 0)) as GameObject;
clone.name = ("StatueA" + buildingNumber);
clone.tag = "EnConstruction";
isCreated = true;
}
GameObject cloneb = GameObject.Find("StatueA" + buildingNumber);
cloneb.transform.position = hitPoint;
cloneb.GetComponent<Renderer>().material = constructionPositive;
if(constructionColliderChecking == false)
{
cloneb.GetComponent<Renderer>().material = constructionPositive;
if(Input.GetButton("Fire1"))
{
lookingForSpot = false;
cloneb.GetComponent<Renderer>().material = statueAmaterial;
cloneb.GetComponent<Collider>().isTrigger = false;
cloneb.AddComponent<CollisionConstructionChecking>();
cloneb.tag = "Construit";
}
}
if(constructionColliderChecking == true)
{
cloneb.GetComponent<Renderer>().material = constructionNegative;
}
}
}
}
public void StatueConstruction (string buildingToConstruct)
{
isCreated = false;
lookingForSpot = true;
}
}
Comment
Problem solved. $$anonymous$$y raycast was hitting the instantiated object..