- Home /
Question by
HappyCrayfish · Oct 09, 2015 at 08:33 PM ·
c#instantiateraycastingmouse position
Positioning a instantiated object using Input.Mouseposition doesn't position right?
So, I'm trying to instantiate a object on mouse position, but the object is instantiated on a totally different position when launching the game.
Here's my current code :
void Update()
{
if (towerBools [0] == true) {
if (Input.GetButtonDown ("Fire1")) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Instantiate(testTowerPrefab[0], Input.mousePosition, transform.rotation);
Debug.Log ("Instantiated Tower1");
towerBools[0] = false;
}
}
}
void OnGUI()
{
GUI.Box (new Rect (Screen.width - 300, 0, Screen.width / 4, Screen.height - 2), "", boxStyle);
if (GUI.Button (new Rect (Screen.width - 300 / 2, 50, 50, 50), "Tow Pic", buttonStyle)) {
towerBools[0] = true;
}
}
Comment
Answer by flaviusxvii · Oct 09, 2015 at 08:44 PM
You forgot to do the actual ray-cast!
http://docs.unity3d.com/ScriptReference/Input-mousePosition.html
So, did that and now it won't instantiate at all. I put the instantiate code just like they did on the Scripting API.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Mouse pos + raycast 1 Answer
Multiple Cars not working 1 Answer
Problem with ScreenToWorldPoint and touches (C#) 2 Answers
Instantiating an object as a child of another object 1 Answer