Move an Object until it collides with wall?
Hi. Im making my first unity game and I have a problem. I have a cube that can move with the arrow keys. The cube is in a simple room. The cube spawns at the right at the top in the room. If I press down and then I immediately press up the cube turns around at the half of his way. I don't want this to be possible. if I press the down key, I want the cube to go down until he touches the wall. I'm not good at coding so i don't know how to do this.
this is my Code now:
using UnityEngine;
public class MoveWithForceARROWS : MonoBehaviour
{
private Rigidbody _rigidbody;
[SerializeField] private float _movementForce = 10f;
private void Awake() => _rigidbody = GetComponent<Rigidbody>();
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.UpArrow))
_rigidbody.AddForce(_movementForce * Vector3.forward);
if (Input.GetKey(KeyCode.DownArrow))
_rigidbody.AddForce(_movementForce * Vector3.back);
if (Input.GetKey(KeyCode.RightArrow))
_rigidbody.AddForce(_movementForce * Vector3.right);
if (Input.GetKey(KeyCode.LeftArrow))
_rigidbody.AddForce(_movementForce * Vector3.left);
}
}
Your answer
Follow this Question
Related Questions
How i stop Player rotation when collide 2 Answers
Issue with character movement and collision using Physics2D.Raycast. 0 Answers
How to SHOOT an object on a curved path without using Rigidbody 2 Answers
How can I remove movement or camera rotation in one direction with a trigger 0 Answers
OnMouseDown Not Working after Moving 0 Answers