- Home /
How do I stop objects from rotating when they collide
I'm trying to make a game where you have two cubes and one of them can move vertically and other horizontally. To win you must use the cubes to push each other in the directions the other can't move to their objectives. For example cube one can only move vertically and cube two can only move horizontally. And cube 2's objective is below it so it moves under cube one and cube one pushes cube 2 onto its objective. (This game is a 3D type game, but the perspective is above the game level so it can technically be 2D even it uses 3D objects). The problem is, when the cubes push each other they may shift and rotate. I only want them moving directly vertical and horizontal. They end up getting pushed to where they move diagnol. I tried freezing rotation on rigid body and it didn't work. I just need them to stay straight when they push against eachother and not shift.
Here is the code for Player Movement for Cube One
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pOneMovement : MonoBehaviour
{
public float speed = 5f;
void Update()
{
float ver = Input.GetAxis("Vertical");
transform.Translate(0, 0, speed*ver*Time.deltaTime);
}
}
Here is the collision script for when Cube One reaches its objective
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionPlayerOne : MonoBehaviour
{
public pOneMovement PlayerOne;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "playerOneGoal")
{
PlayerOne.enabled = false;
}
}
}
If there was something I didn't explain well enough and you might have an idea but you don't totally understand the question just ask and I'll try and re-explain it. Thanks
Answer by kreaturian · May 27, 2019 at 05:05 AM
In the rigidbody component, there should be a menu called "Constraints". There you can change whether or not it rotates or moves on a certain axis.
Answer by mseever2022 · May 27, 2019 at 05:51 AM
Actually i retried it just to make sure and before i only checked the x and z for freeze rotation, and it didnt work, but then i tried freezing the y to and it did so thank you! @kreaturian