Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oliver-jones · Apr 01, 2011 at 11:55 AM · instantiateraycastprefab

Help - Instantiate A Prefab On Mouse Position

This has been bugging me for ages now! I honestly don't know what to do (besides Unity Answers).

Right, I have a 'click and drag' system going on - its basically a GUI button, in which when you click on it a 'dummy turret' prefab appears under your mouse, and where ever you click the 'dummy turret' turns into a 'real turret' prefab and is placed

dummy = it has no script - so it wont fire at enemy when positioning turret.

real = it has script - it will shoot!

Now! What I am TRYING to do, is not allow the user to place a turret on a collider tagged 'NoZone' (the path in which the enemy walk on). I have also made another prefab for this which is basically a 'dummy' one but with red material applied. This is what I want to be instantiated under the mouse instead of the dummy when user is raycasting over NoZone ... and vice verser.

Here is my DragScript:

var objNoZone : Transform; // the prefab used for a no zone area - (not allowed)

var objDummy : Transform[]; // the prefab that needs to be initiated first var objGroup : Transform[]; // the prefab that is initiated when the user placed the dummy

var objItemNames : String []; var objLimit : int [];

var selGridInt : int = 0;

private var selectedGrid : int = 0; var groundPlane : Transform;

var dummyObj : Transform;

var currentObj : Transform;

var cloneHolder : Transform; var followSpeed : float = 10; private var chooseLocation : boolean = false; private var cancelSelection : boolean = false; var currentSelected : int = 30;

var gunTurretGUI : Texture; var missileTurretGUI : Texture;

private var ZoneHit : boolean = false; private var ZoneOn : boolean = false; private var currentHit;

function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; var layerMask = 1 << 8; if (chooseLocation) { layerMask = ~layerMask; Physics.Raycast (ray, hit, 600, layerMask); Debug.DrawLine (ray.origin, hit.point); dummyObj.Translate((Vector3(hit.point.x, dummyObj.position.y, hit.point.z) - dummyObj.position) * Time.deltaTime *followSpeed); currentHit = hit;

 }
 if(currentHit.collider.tag == "NoZone" &amp;&amp; ZoneOn == false){
     objNoZone = Instantiate (objNoZone,Vector3(0,0,0),Quaternion.identity);
     objNoZone.Translate((Vector3(dummyObj.position.x, dummyObj.position.y, dummyObj.position.z) - currentObj.position));
     //ZoneOn = true;
 }

 if (Input.GetButtonDown("Fire1") &amp;&amp; chooseLocation == true &amp;&amp; ZoneHit == false){
     currentObj = Instantiate (objGroup[currentSelected],Vector3(0,0,0),Quaternion.identity);
     currentObj.Translate((Vector3(dummyObj.position.x, dummyObj.position.y, dummyObj.position.z) - currentObj.position));
     chooseLocation = false;
     Destroy(dummyObj.gameObject);
 }


 // Cancel Selection \\
 if(Input.GetButtonDown("Fire2") &amp;&amp; dummyObj.gameObject){
     objLimit[currentSelected] ++;
     chooseLocation = false;
     //Destroy(currentObj.gameObject);
     Destroy(dummyObj.gameObject);
 }

}

function OnGUI () {

 selGridInt = 0;
 if(chooseLocation == false){
     //if (GUI.Button(Rect(10, 10, 120, 30), objItemNames[selGridInt] + " ("+objLimit[selGridInt]+")") &amp;&amp; objLimit[selGridInt] &gt; 0){
     if (GUI.Button(Rect(100, Screen.height - 170 ,140 ,140), gunTurretGUI)){
         currentSelected = selGridInt;
         chooseLocation = true;
         objLimit[selGridInt] --;

         dummyObj = Instantiate (objDummy[selGridInt],Vector3(0,0,0),Quaternion.identity);
         dummyObj.position.y = groundPlane.position.y + 0.5;
         selectedGrid = 0;


     }

 }

}

Now what happens so far, is that the 'NoZone dummy' keeps initiating at 0,0,0 every frame whilst the mouse is over the collider NoZone.

Pleaseeee help me! Would much appreciate it!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bampf · Apr 01, 2011 at 01:22 PM

The code that is instantiating the NoZone dummy doesn't have the same IF conditions that the other IF-clauses do, so it is running too often. In particular, it is running when chooseLocation is false.

For starters, it should check for chooseLocation == true:

if(currentHit.collider.tag == "NoZone" && chooseLocation == true && ZoneOn == false){

Most of the Update code only runs when chooseLocation is true, so it might be clearer to just check that once.

Another issue is that you don't seem to be destroying objNoZone anywhere.

Instead of two models, an easier approach would be to just change a texture of the turret's material, depending on which zone the player is mousing over. (Or you could swap in an entirely different material if you prefer.)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Instantiate prefab from original position to click mouse point position 0 Answers

Instantiating prefab question. 1 Answer

Instantiate as children of GameObject hit by raycast. 2 Answers

Instantiate a prefab and parenting it? 1 Answer

A way to instantiate an item with 90 Degree increments? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges