I am traying to place an object based on the mouse position, before setting the object final position, I want the player to be able to move the object around the map to choose a location.
I try using a Raycast, but it sends the object to the center of the terrain, instead of the exact position the ray is hitting.
Does ray cast detects exact position in a Terrain? or does it only detecs the transfrom of the object a a whole?
any ideas what am i doing wrong?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
namespace UnityStandardAssets.Characters.FirstPerson
{
public class ObjectPlacer : MonoBehaviour
{
public GameObject smelterdummy;
private ActionBar actionBar;
private InventoryV2 inventoryV2;
private PlayerHP playerHP;
private Actions actions;
private AudioSource collectSound;
private GameObject smelterGO;
private RaycastHit hit;
private bool once = true;
// Use this for initialization
void Start()
{
collectSound = GameObject.FindGameObjectWithTag("TakeSound").GetComponent<AudioSource>();
actionBar = GameObject.FindGameObjectWithTag("ActionBar").GetComponent<ActionBar>();
inventoryV2 = GameObject.FindGameObjectWithTag("GUI").GetComponent<InventoryV2>();
playerHP = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHP>();
actions = GameObject.FindGameObjectWithTag("AM").GetComponent<Actions>();
}
// Update is called once per frame
void Update()
{
if (smelterGO != null)
{
Ray placefinder = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(placefinder, out hit, 300))
{
smelterGO.transform.position = hit.transform.position;
smelterGO.transform.position = new Vector3(smelterGO.transform.position.x, Terrain.activeTerrain.SampleHeight(smelterGO.transform.position), smelterGO.transform.position.z);
}
}
Placement();
}
public void OnMouseOver()
{
}
void Placement()
{
if (actionBar.actionBarSlot[actionBar.abPosition] == 23)
{
if (once)
{
once = false;
smelterGO = Instantiate(smelterdummy) as GameObject;
}
actions.disableOn = true;
}
else
{
Debug.Log("HEre!!");
actions.disableOn = false;
Destroy(smelterGO);
once = true;
}
}
}
}
You were absolutly right, but now my object is moving in clycles from the hit.point into the camera... over and over again... any ideas why? @Rostam24
– raffaga