• 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 $$anonymous$$ · Feb 10, 2021 at 02:52 PM · respawnkill player

Crush player with moving wall

I have a player (Ball) and a moveable cube. If i push the player into a wall with the cube I want the player to get crushed. I also don't want a menu if the player dies. I want the whole scene to immediately reset as soon as the player dies. t$$anonymous$$s is my script for the player:`

 using UnityEngine;
 
 public class MoveWithForceWASD : MonoBehaviour
 {
     private Rigidbody _rigidbody;
 
     [SerializeField] private float _movementForce = 10f;
 
     private void Awake() => _rigidbody = GetComponent<Rigidbody>();
 
     private void FixedUpdate()
     {
         if (Input.GetKey(KeyCode.W))
             _rigidbody.AddForce(_movementForce * Vector3.forward);
 
         if (Input.GetKey(KeyCode.S))
             _rigidbody.AddForce(_movementForce * Vector3.back);
 
         if (Input.GetKey(KeyCode.D))
             _rigidbody.AddForce(_movementForce * Vector3.right);
 
         if (Input.GetKey(KeyCode.A))
             _rigidbody.AddForce(_movementForce * Vector3.left);
     }      
 }

and t$$anonymous$$s here is my script for tha moveable cube:

 using UnityEngine;
 public class TestScriptFromUnityAnswers2 : MonoBehaviour
 {
     private Rigidbody _rigidbody;
     public bool _canChangeDirection;
     private Vector3 _direction;
     [SerializeField] private float _movementForce = 10f;
 
     public void Awake()
     {
         _rigidbody = GetComponent<Rigidbody>();
         _canChangeDirection = true;
     }
 
     private void Update()
     {
         if (_canChangeDirection)
         {
             if (Input.GetKeyDown(KeyCode.UpArrow))
                 SetForceDirection(Vector3.forward);
 
             if (Input.GetKeyDown(KeyCode.DownArrow))
                 SetForceDirection(Vector3.back);
 
             if (Input.GetKeyDown(KeyCode.RightArrow))
                 SetForceDirection(Vector3.right);
 
             if (Input.GetKeyDown(KeyCode.LeftArrow))
                 SetForceDirection(Vector3.left);
         }
     }
 
     private void FixedUpdate()
     {
         if (_direction.sqrMagnitude > 0.01f)
             _rigidbody.AddForce(_movementForce * _direction);
     }
 
     private void SetForceDirection(Vector3 direction)
     {
         _direction = direction;
         _canChangeDirection = false;
     }
 
     void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.name == "Wall")
         {
             _canChangeDirection = true;
             _direction = Vector3.zero;
         }
     }
 }
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

Answer by zolteeeen · Feb 11, 2021 at 09:39 PM

You can do somet$$anonymous$$ng like t$$anonymous$$s:

 private bool isCollidedWithWall = false;
 private bool isCollidedWithCube = false;
 
 public void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.name == "wall")
         isCollidedWithWall = true;
     else if (collision.gameObject.name == "cube")
         isCollidedWithCube = true;
 
     if (isCollidedWithWall && isCollidedWithCube )
         Destroy(gameObject);
        //restart game
 }
 
 public void OnCollisionExit(Collision collision)
 {
     if (collision.gameObject.name == "wall")
         isCollidedWithWall = false;
     else if (collision.gameObject.name == "cube")
         isCollidedWithCube = false;
 }

That will detect if you player has collided with both wall and cube... and in that case you can destroy object or reset game or whatever you like.

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 $$anonymous$$ · Feb 14, 2021 at 11:51 AM 0
Share

It does work pretty good but i still have 1 problem.

if (collision.gameObject.name == "wall")

can I also do this with a child?

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

110 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

Related Questions

How do I get my player to die when light shines on him? 2 Answers

Player will not Die or Respawn 1 Answer

C# InstaKill method for 2d game? 2 Answers

How to use a script on multiple gameobjects, then change a variable for one of them, not the other. 3 Answers

Respawn player at different Spawnpoints 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