Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 debotten · Aug 23, 2018 at 03:40 PM · scripting problemcollision3d

Player dies when it Hits/crushes a block

I want the cube (player) to die when it hits another cube (not player). I dont know writing scripts. Can anybody help me? Thanks.

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
0

Answer by freny87 · Aug 23, 2018 at 05:48 PM

You should watch some tutorials if you are interested on making games. Here you have a really short one about what you asked for https://www.youtube.com/watch?v=QRp4V1JTZnM

You will never learn if you ask someone for such a simple code instead of doing research

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 ModLunar · Aug 23, 2018 at 06:04 PM

I don't mean to be rude, but you'd best get acquainted with how programming works, at least just in Unity. C# is a lovely language, and you'll have much more power to make what you want :). It is a large time investment, but it's definitely worth it, and even just small steps every week are much better than none!

To answer your question, Unity has "messages" that get sent to MonoBehaviour scripts on a GameObject at important parts in the game. Most notably, you've probably heard of Awake(), Start(), and Update() in MonoBehaviour classes. You can expect Unity to magically call these methods for you at the appropriate times -- you could just put a Debug.Log("Hi"); statement in Update(), and it'll happen every update frame (and your console will get spammed).

Similar to those messages, there exists other ones for getting information about what's happening with the physics side of things. You can detect when objects collide, see when objects enter and leave trigger Colliders, etc.

What you'll want to have in your MonoBehaviour class is:

public void OnCollisionEnter(Collision collision) { //When this runs, you know your object hit something! //it hit collision.collider, which is attached to collision.gameObject }

Then you can ask questions in the code using if statements about the object that collided with the current one. For example, if your block has a script on it called PainfulBlock, and you have "public class PainfulBlock : MonoBehaviour { ... }" defined somewhere, then you can check if the object the player collided with has that PainfulBlock component on it like this:

public void OnCollisionEnter(Collision collision) { PainfulBlock block = collision.GetComponent<PainfulBlock>(); if (block != null) { //This player did hit a block! //Therefore, they should die -- write that code in here. } }

OnCollisionEnter(...) gets called only the first physics update where two colliders enter contact with each other. There's also OnCollisionStay(...) and OnCollisionExit(...) to get called while they stay in contact, or exit contact with each other, respectively. Note that in order for your player's MonoBehaviour, containing this OnCollisionEnter(...) method, to have it called properly:

- There must be at least 2 objects in your scene with a Collider of some sort

- One of the two colliders, for a "Collision" message, must have a non-kinematic Rigidbody component as well

- Place your script with OnCollisionEnter(...) on the same GameObject as the player's collider to see when the player comes into contact with other objects

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

How can i look for collisions of the bulidngs in may array ? how can i use OnTRiggerEnter/exit ? 0 Answers

Script appears as null and not null at the same time 1 Answer

Goal Collision for pong game 0 Answers

How to use OverlapCollider? 1 Answer

Player getting stuck in corners 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