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
1
Question by Badsniper · Apr 16, 2012 at 03:42 AM · javascriptnot workingoncollisionenter

Collision problem

var rocket : Rigidbody; var speed = 10.0;

function FireRocket() { var rocketClone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation); rocketClone.velocity = transform.forward * speed; } function OnCollisionEnter () { Destroy (gameObject); }

function Update (){ if (Input.GetButtonDown ("Fire1")){ FireRocket(); } }

Hi there! I'm new to unity and I'm just learning how to make basic game elements, right now I'm trying to have this rocket disappear when it hits a wall or some object, but it does not do that. I wasn't able to find many other relevant questions like mine.

The object that shoots out is a cube (I know, not a missile) and at first it collides with terrain - that didn't work. So I had the cube collide with a cube and that didn't work either. The colliders are all on and I feel like I am missing something fairly obvious.

Thanks in advance for the help!

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 aldonaletto · Apr 16, 2012 at 04:55 AM

The OnCollisionEnter code is in the weapon's script, but it should be in the missile script - that's the missile that collides with things, not the weapon!

  • Weapon script:

var rocket : Rigidbody; var speed = 10.0;

function FireRocket(){ var rocketClone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation); rocketClone.velocity = transform.forward * speed; }

function Update (){ if (Input.GetButtonDown ("Fire1")){ FireRocket(); } } - Rocket script:

function OnCollisionEnter (){
    Destroy (gameObject);
}
Comment
Add comment · Show 3 · 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 Badsniper · Apr 16, 2012 at 05:34 AM 0
Share

I get it! Or at least I think I do.

I parented (I think this is the right term) the rocket script to the game object (a cube with Rigidbody component) then I made the game object the Rocket variable in the weapon script!

Thanks!

avatar image bodec · Apr 16, 2012 at 06:25 AM 0
Share

when you want to use the on trigger function you need to make sure you set up a collider as a trigger.

avatar image aldonaletto · Apr 16, 2012 at 11:58 AM 0
Share

No, this isn't parenting: you added the script to the projectile. A script is a component, just like Rigidbody, Collider etc., and is added to some GameObject, while parenting is a hierarchycal relationship between GameObjects. $$anonymous$$any people (me included) confuse these two concepts at first, what causes a lot of headaches.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Flight Script throws no errors but does nothing. 2 Answers

SendMessage Not Working: JavaScript 1 Answer

Checking to see if two objects are colliding. 2 Answers

OnGui working with javascript but not with C# 2 Answers

OnCollisionEnter not working. 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