• 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 crazyKnight · May 26, 2011 at 07:58 AM · destroyparentarrow

Arrow Stab into ground and stay(script)

Hello :) I need help....

this is the code i am currently using

CODE#

 private var cam : follow_transform;
 
 function Start()
 {
     rigidbody.useGravity = false;
 }
 
 function Update () 
 {
     if(cam.fire)
     {
         FireArrow();
     }
 }
 
 function OnCollisionEnter(info : Collision) 
 {    
     if(info.gameObject.tag == "ground")   //ground is the base 
     {
         transform.rigidbody.isKinematic = true;
         gameObject.transform.parent = info.gameObject;
         DestroyAfteryield(2);
     }
 }
 
 function FireArrow()
 {
     rigidbody.useGravity = true;
     rigidbody.AddForce(-transform.forward * cam.fireforce * 110,ForceMode.Force);
 }
 
 
 function DestroyAfteryield(time : float)
 {
     yield WaitForSeconds(time);
     Destroy(gameObject);
 }

what i actually want is that when the arrow hits the ground it should actually stick there(just like a knife is stabbed)everything seems to work fine the arrow sticks to the ground but the moment it collides with the ground it gives an error(Cannot cast from source type to destination type in the following line(gameObject.transform.parent = info.gameObject;))and the arrow doesn't get destroyed as it should be according to the DestroyAfterYield function.

can anyone help me out with this thanks in advance

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 FLASHDENMARK · May 26, 2011 at 03:17 PM 0
Share

Hi. When posting code, please highlight it and press the "10101" button and it will look really neat and easy to read.

2 Replies

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

Answer by taters · May 26, 2011 at 11:59 AM

I think you need to parent to the 'transform' of the gameObject, not the gameobject itself.

try:

gameObject.transform.parent = info.gameObject.transform;

(not tested)

not sure on the destroy thing. Maybe that exception was causing that function to quit at that point, therfore not running the destroy. Put a print in the destroy function, see if it gets called.

Comment
Add comment · Show 6 · 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 crazyKnight · May 26, 2011 at 12:54 PM 0
Share

i have already tried what you are suggesting but its is giving me very weird results,if i try your code then the moment the arrow collides with the ground it transform itself it to some other position and scales(i cant understand in what way is it scaling the arrow bcoz the arrow turns into a "what should i call that" object)

avatar image crazyKnight · May 26, 2011 at 12:58 PM 0
Share

and the destroy function is not called

avatar image taters · May 26, 2011 at 03:12 PM 0
Share

The reason it's scaling odd is because as soon as you add the child to the parent, it inherits it's scale. Ideally you want the ground to be 1,1,1 scale. (You may have stretched a cube primitive?, you'll need to make a 3D model if you want a custom shape/size). As for the destroy function, it should work.

I knocked up a quick scene. http://www.pyramid-design.co.uk/temp/arrowtest.zip

It shows a cube sticking on another cube. All works. But as soon as you scale the ground, you'll notice it screws up. Destroy function seems to be working o.k too. Hope this helps :)

avatar image Joshua · May 26, 2011 at 03:13 PM 1
Share

Why the downvote? He is correct that transform.parent should be set to a type of Transform, not GameObject - so upvote to compensate.. Also. Format your code if you want people to bother reading it.

avatar image Joshua · May 26, 2011 at 03:48 PM 0
Share

@Taters (hadn't seen your comment at the time of posting $$anonymous$$e) the scaling could of course be compensated to rescale according to the parent's scale.

Show more comments
avatar image
0

Answer by crazyKnight · May 27, 2011 at 05:39 AM

thanks everyone for there help and support. instead of figuring out how to maintain scale... i was more concerned about the errors...and was not using transform instead of game object anyways thanks all

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Use clones as independent gameobjects 1 Answer

[Closed] Player Weapons Destroying Dont Destroy Confused. 2 Answers

If A collide with B A will become Child of B - C# 3 Answers

Destroy the child instantiated object 1 Answer

Destroying of parent causes issues for OnDestroy() in children 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