- Home /
Why does this placement script not work?
I am getting no errors or anything yet when I start up the game and click the button to instantiate a prefab that I can move around it doesn't work. The object doesn't even show up. Why is this. I am creating this script that makes the movement and instantiation of the object avalaible and than I am calling it in another script for when I click a button. I am trying to get it so that the gameobject moves relative to your objects position and it should always be 15 units away from the camera until this certain point which I will add to the script later. Can someone tell me whyt his isn't working. I have very basic knowledge on working with Vectorsa and such. Thank you for any help, here is the script:
using UnityEngine;
using System.Collections;
public class PlacingParts : MonoBehaviour
{
private Transform currentPart;
void Start ()
{
}
void Update ()
{
if (currentPart != null)
{
Vector3 m = Input.mousePosition;
currentPart.position = new Vector3(m.x,m.y,15);
}
}
public void PlaceItem(GameObject b)
{
currentPart = (((GameObject)Instantiate(b)).transform);
}
}
////////////////////////OTHER SCRIPT BELOW/////////////////////////////
//This is where I am calling the placingObjects script
//I stored the placingParts script in a variable and than //used it
GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
for (int i = 0; i< cockpit.Length; i++)
{
if(GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),cockpit[i].name, partSlotButton))
{
placingparts.PlaceItem(cockpit[i]);
}
}
GUI.EndGroup ();
The problem could come from your caller function. Would you $$anonymous$$d posting the code that calls the PlaceItem function?
I posted it in the question. Thanks for any help you give!
Answer by Jeff-Kesselman · May 06, 2014 at 12:28 AM
Okay, so a few things.
The first is that the mouse X and Y are in screen pixel coordinates, not world coordinates.
If you want to know where that point is on the screen in world coordinates you need to use Camera.main.ScreenToWorldPoint(Input.mousePosition).
Next, I don't actually see PlaceItem being called at all.
It's on a different script. I'll post it in the question.
After I changed the Vector3 m to equal Camera.main.ScreenToWorldPoint(Input.mousePosition). It just instantiates the object off to the side and I can't move it around or anything. What would be the fix to this?
If you call
Camera.main.ScreenToWorldPoint (Input.mousePosition)
it will always return a point on the camera's near clip. You need to specify the distance from the camera you want the point to be. The call would look like this
Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15f));
That works ,but it doesn't always stay in front of my camera when the camera rotates and it's slightly off from the mouse. Any solution to these problems?
Well, I've had the best luck with exact positioning by using a Plane and a Plane ray cast.
can you tell us exactly what you are trying to accomplish?