- Home /
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. this 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 this 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;
}
}
}
Answer by zolteeeen · Feb 11, 2021 at 09:39 PM
You can do something like this:
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.
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
![](https://koobas.hobune.stream/wayback/20220613024929im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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
Random Spawning Question 2 Answers
Trying to make a checkpoint script 3 Answers