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 Regalith · Sep 25, 2013 at 04:50 AM · javascriptinstantiateraycast

Odd Behavior with my Drag and Drop Code

Hey so I just started working on a drag and drop construction system, where the user can choose a structure and click and drag the position and length of the wall he is creating(think age of empires wall system) so far the drag and drop works well enough for isolated segments of walls, but anytime I try building off an already instantiated wall, the wall segment gets destroyed 80% of the time, and the rest works about 30% of the time, which confuses me on why my code would work so unreliably. Here's the class I use to create structures

 class CreateStructure extends MonoBehaviour
 {
     
     //test Prefab
     public var testSegment:GameObject;
     public var testConnector:GameObject;
     
     //Start and finish Locations of mouse drag
     var startPos:Vector3;
     var endPos:Vector3;
 
     //Build Camera
     var topCam:Camera;
         
     function Start()
     {
         topCam = GameObject.FindGameObjectWithTag("Build Camera").camera;
         testSegment = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/WallSection1.prefab", GameObject);
         testConnector = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/WallConnector1.prefab",GameObject);
     }
 
     function Update()
     {
         //when mouse clicked down, store starting position
         if(Input.GetMouseButtonDown(0))
         {
             startPos = FindLocation();
         }
         
         //when mouse button released, store ending position
         if(Input.GetMouseButtonUp(0))
         {
             endPos = FindLocation();
             CalculateAndPlace(startPos, endPos, testSegment);
         }
     }
     
 
     
     
     
     function FindLocation() : Vector3
     {
         //raycast based on mouse location
         var ray : Ray = topCam.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if(Physics.Raycast(ray, hit))
         {
             //if no structures returned from raycast, return point
             if(hit.transform.tag == "Ground")
                 return hit.point;
                 
             //if structure returned, remove that structure    
             if(hit.transform.tag == "Small Wall" || hit.transform.tag == "Wall Connector")
             {
                 return ReplaceWall(hit.transform.gameObject);
             }
         }
         return hit.point;
     }
     
     
     
     
     function CalculateAndPlace(p1 : Vector3, p2 : Vector3, seg: GameObject)
     {
         //Create a ray long the path of the two points starting at startPos
         var temp:GameObject = new GameObject();
         temp.transform.position = p1;
         temp.transform.LookAt(p2);
         var directionRay : Ray = new Ray(p1, temp.transform.forward);
         
         //calculate distance between p1 and p2
         var distance :float = Vector3.Distance(p1,p2);
         
         //calculate number structures to be placed
         var numberOfStructures : int;
         var segmentLength = seg.renderer.bounds.size.z;
         numberOfStructures = distance / segmentLength;
         
         
         //place structures along the directionRay
         for(var i : int = 0; i <= numberOfStructures; i++)
         {
             PlaceStructure(directionRay.GetPoint(segmentLength * i), temp, seg);
         }
     }
     
     
     
     
     function PlaceStructure(position : Vector3, tempObj: GameObject, seg : GameObject)
     {
         Instantiate(seg, position, tempObj.transform.rotation);
     }
     
     //replace wall With connector prefab
     function ReplaceWall(obj : GameObject) : Vector3
     {
         //store obj components
         var tempTras :Vector3= obj.transform.position;
         var tempRot :Quaternion = obj.transform.rotation;
         
         //raise translate up 10 so raycast wont be below the ground
         tempTras.y += 10;
         
         //store ray to return
         var point : Vector3;
         
         //destroy object so raycast wont hit it
         Destroy(obj);
         
         //raycast to find position
         var hit : RaycastHit;
         if(Physics.Raycast(obj.transform.position, Vector3.down, hit))
         {
             point =  hit.point;
         }
         //instantiate the wall connector prefab
         Instantiate(testConnector, obj.transform.position, obj.transform.rotation);
         //return the raycast position
         return point;
 
     }
 }

Anyone have any ideas whats wrong with my code or a better system for drag and drop all together?

Comment
Add comment · Show 1
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
avatar image Regalith · Sep 25, 2013 at 08:34 PM 0
Share

Ok so I figured out the problems with the code and got it working 100%,so guess i got this covered

0 Replies

· Add your reply
  • Sort: 

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to activate a button? 1 Answer

Changing variable in a script of a game object hit with raycast 1 Answer

[SOLVED] Only instantiating once 1 Answer

Raycast help? 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