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 samz · Aug 06, 2013 at 12:23 AM · instantiatevelocityballclonethrow

How to Instantiate and change velocity

I have a coconut which my player throws, where in mid-air it will multiply into 3 coconuts. 1 of which will have less velocity than the original and the other will have more, like this... alt text

now the problem is that the velocity from my code works whenever it wants. So sometimes these 3 coconuts are very close together and do not separate, like this:

alt text

i really dont understand why this happens, but playing around with my code i noticed that if i don't destroy these 2 additional clones then it works fine.

Heres my code: This script is used by both the original coconut and the clones, the variable "clonesMade" distinguishes between the two

 function Start()
 {
     Physics.IgnoreCollision(MyStaticVar.playerTransform.FindChild("Collider").collider, collider);
 
     if( !MyStaticVar.clonesMade )
     {
         // Wait until throwing animation is complete before executing the launch
         yield WaitForSeconds (MyStaticVar.playerTransform.animation["Throw"].length-0.7);
         
         ThrowMulti();
     }
     else
     {
         CloneControl();
     }
 }
     
 //********************************************************************************************
 
 function ThrowCoconut()
 {
     // Add launch force inrelation to the direction the player is facing
     transform.rigidbody.velocity = Vector3(35,50,0);
 }
 
 //********************************************************************************************
 
 function ThrowMulti()
 {    
     ThrowCoconut();
     CreateClone();
 }
 
 //********************************************************************************************
 
 function CreateClone()
 {
     MyStaticVar.clonesMade = true;
     yield WaitForSeconds(0.5);
 
     // Multi-coconut 1
     var clone1 : Transform = Instantiate(transform, transform.position, transform.rotation);
     clone1.name = "Clone1";
     Physics.IgnoreCollision(clone1.collider, collider);
 
     // Multi-coconut 2
     var clone2 : Transform = Instantiate(transform, transform.position, transform.rotation);
     clone2.name = "Clone2";
     Physics.IgnoreCollision(clone2.collider, collider);
 }
 
 // *********************************************************************************************
 
 function CloneControl()
 {
     if( name=="Clone1" )
     {
         transform.rigidbody.velocity += Vector3(5,0,0);
     }
     else if( name=="Clone2" )
     {
         transform.rigidbody.velocity += Vector3(50,0,0);
         MyStaticVar.clonesMade = false;
     }
 }
 
 //********************************************************************************************
 
 function OnCollisionEnter (col : Collision) 
 {
     // slow down when on platform
     if( col.transform.tag=="Platform" )
     {
         rigidbody.velocity.x = 2;
     }
     
     // Destroy
     if( name=="Pfb_Throw_PowerUp(Clone)" )
     {
         Destroy(gameObject, 2);
     }
     else if(name=="Clone1" || name=="Clone2")
     {
         Destroy(gameObject, 2.5);
     }
 }

_bad.png (121.2 kB)
_good.png (125.6 kB)
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

2 Replies

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

Answer by sparkzbarca · Aug 06, 2013 at 03:39 AM

first both those have + velocity so both are faster than the first.

Second and this is probably a big issue

they ignore the original collider but the clones can still collide with each other.

you need to ignorecollision clone1, clone2

Third get rid of the clone control function altogether. just do it inside creation also the current velocity to figure out what to add and subtract lastly use addforce not velocity directly.

 clone = instantiate (coconut, position,rotation);
 clone.rigidbody.addforce(-rigidbody.velocity.normalized * speed)
 
 clone2 = .....
 clone2.rigidbody.addforce (rigidbody.velocity.normalized * speed)

notice one has - in front and one doesnt. speed will be the difference in how much the front is forward and the back is back.

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

Answer by samz · Aug 06, 2013 at 07:52 AM

THANK YOU SO MUCH!!!!!!!!!!

  1. rigidbody.AddForce(rigidbody.velocity.normalized);

  2. rigidbody.velocity = Vector3.right * speed;

This has helped me loads, i added number2 since number1 would always give me a velocity of (0,0,0). And i need to keep the CloneControl otherwise the clones would incorporate the same velocity change as the original coconut being thrown, making it jump in mid-air after being instantiated.

but you deco put me on the right track!!!! thank you once again

Comment
Add comment · Show 1 · 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 sparkzbarca · Aug 06, 2013 at 05:11 PM 1
Share

oh yes im sorry 1 would initially thats because if you have a starting velocity of zero the normal of that is zero and of course zero multiplied by anythign is zero.

what you could do is copy the velocity of the base

clone1.rigidbody.velocity = parent.rigibody.velocity;

then

addforce(velocity.normalized * speed)

taht will work and that will include your movement in all 3 directions down and forward and depth and scuh

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 can I spawn balls in every direction around a source (making a sphere) and then make the balls move away from that source? 1 Answer

When cloning, how do you make your clones keep the original properties? 2 Answers

How can I display Cloned ball velocity in text ? 0 Answers

Multiplying something.forward using a variable doesn't actually multiply, but multiplying by a number does. 1 Answer

intelligent throwing 0 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