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 Madswint · Nov 22, 2013 at 04:46 PM · instantiatepositionspawnprefabs

Why does this script spawn 13 ClickEffect prefabs instead of 1?

 private var targetPosition:Vector3; 
 var speed : float = 60;      
 var clickEffect : GameObject;
          
  
         function Update () {
  
             if(Input.GetKeyDown(KeyCode.Mouse0))
  
             {
  
 
            speed = 1;
  
                 var playerPlane = new Plane(Vector3.up, transform.position);
  
                 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  
                 var hitdist = 0.0;
  
                
  
                 if (playerPlane.Raycast (ray, hitdist)) {
  
                     var targetPoint = ray.GetPoint(hitdist);
  
                     targetPosition = ray.GetPoint(hitdist);
  
                     var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
  
                     transform.rotation = targetRotation;
  
                    
  
                      
  
                 }
  
              
  
             }
  
            
  
                 var dir:Vector3 = targetPosition - transform.position;
  
     var dist:float = dir.magnitude;
  
     var move:float = speed * Time.deltaTime;
  
     if(dist > move){
  
     transform.position += dir.normalized * move;
     Instantiate(clickEffect,targetPosition,Quaternion.identity);
  
     } else {
  
     transform.position = targetPosition;
  
     }
  
            
  
            
  
             transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
  
            
  
          
  
          
  
          
  
          }

Any ideas how to fix it? Thanks in advance.

Comment
Add comment · Show 2
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 thaiscorpion · Nov 22, 2013 at 04:49 PM 1
Share

On line 51 you are checking if dist is bigger than move, and then instantiating a clickEffect. But then dist is still bigger than move and will keep spawning clickEffects.

avatar image Madswint · Nov 22, 2013 at 05:33 PM 0
Share

Any ideas how to fix that?

2 Replies

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

Answer by Fornoreason1000 · Nov 22, 2013 at 06:29 PM

the problem you're instantiating for every frame that dist > move try putting the code within.

   if(Input.GetKeyDown(KeyCode.Mouse0))     
 {
 //code goes here
 }

that way it will only ever instantiate when you press the mouse button not for every frame that distance is more than move

 private var targetPosition:Vector3; 
 var speed : float = 60;      
 var clickEffect : GameObject;
  
  
     function Update () {
  
             if(Input.GetKeyDown(KeyCode.Mouse0))
             {
                 speed = 1;
                 var playerPlane = new Plane(Vector3.up, transform.position);
                 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                 var hitdist = 0.0;
  
                 if (playerPlane.Raycast (ray, hitdist)) {
  
                     var targetPoint = ray.GetPoint(hitdist);
                     targetPosition = ray.GetPoint(hitdist);
                     var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                     transform.rotation = targetRotation;
  
                 }
                 if(dist > move){
                     Instantiate(clickEffect,targetPosition,Quaternion.identity);
                 }
 
             }
  
  
 
     var dir:Vector3 = targetPosition - transform.position;
     var dist:float = dir.magnitude;
     var move:float = speed * Time.deltaTime;
  
     if(dist > move){
  
         transform.position += dir.normalized * move;
         //Instantiate(clickEffect,targetPosition,Quaternion.identity);
     } 
     else {
         transform.position = targetPosition;
     }
  
     transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
 
      }
Comment
Add comment · Show 4 · 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
avatar image Madswint · Nov 22, 2013 at 10:59 PM 0
Share

I'm getting these errors, I've tried to fix them, but I cant see what's wrong:

 Assets/$$anonymous$$it/Scripts/Attach to Player/$$anonymous$$ovement.js(23,21): BCE0005: $$anonymous$$ identifier: 'move'.
 
 &
 
 Assets/$$anonymous$$it/Scripts/Attach to Player/$$anonymous$$ovement.js(23,14): BCE0005: $$anonymous$$ identifier: 'dist'.
 
avatar image Fornoreason1000 · Nov 23, 2013 at 04:46 AM 1
Share

i do, move three above Get Button Down, so that they are declared at the start of the function. the error is caused by the variable being declared after you were trying to use them.

 var dir:Vector3 = targetPosition - transform.position;
     var dist:float = dir.magnitude;
     var move:float = speed * Time.deltaTime;

try to keep local variables at the top

avatar image Madswint · Nov 23, 2013 at 10:38 AM 0
Share

Never$$anonymous$$d, I found it, stupid me. Thanks for your help, I really really appreciate it!

avatar image Madswint · Nov 23, 2013 at 10:41 AM 0
Share

Oh, after first time you click somewhere, next time it just teleports, also the ClickEffect is only there for 1 second. Any ideas how to fix it?

avatar image
0

Answer by Landern · Nov 22, 2013 at 05:07 PM

Input.GetKeyDown will return true each frame, meaning the duration of the mouse button held down might be 13+/- frames.

Input.GetMouseButtonDown will return true once and wait for the user to release the button and subsequently presses the button again.

Comment
Add comment · Show 4 · 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
avatar image Madswint · Nov 22, 2013 at 05:33 PM 0
Share

Getting errors if I change it

avatar image thaiscorpion · Nov 22, 2013 at 06:11 PM 0
Share

what errors?

avatar image Madswint · Nov 22, 2013 at 06:19 PM 0
Share
 dynamic_bitset.test bit out of bounds
 UnityEngine.Input:Get$$anonymous$$ouseButtonDown(Int32)
 $$anonymous$$ovement:Update() (at Assets/$$anonymous$$it/Scripts/Attach to Player/$$anonymous$$ovement.js:8)
 
avatar image Fornoreason1000 · Nov 22, 2013 at 06:24 PM 1
Share

Actually GetButtonDown Returns once until they key is released yhe pressed again, your thinking of GetButton which returns as long as the button is pressed

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

19 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 avatar image avatar image avatar image avatar image

Related Questions

Spawning a prefab X units in 1 direction from an object 3 Answers

Unity2d : Spawning in Prefabs 1 Answer

How to instantiate the prefabs on grid in Unity? 1 Answer

instantiate prefabs y+1 from last instintiated prefab 1 Answer

Spawn Prefab or FBX model after import? 1 Answer


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