Hi, I am new to the Unity engine. I was trying to recreate a tower builder game in 3D. The cube moves left and right in the game, and when the player hits space, it drops the cube. Currently, if I stopped the left and right movement and I am just dropping the cube.
In the above link, a video of the game is available. Every time I drop a cube, the cube bounces and the structure moves. The structure loses its stability and falls. Is there any way to drop the cube without the bounce? Or is there any way to balance the structure(Maybe stopping the movement in Z-axis without affecting other axes)?
I tried freezing the position along the z-axis. When I do that, the cube starts to slide along the x-axis and the structure falls
This is the tutorial that I am currently following:
Tutorial Link
Cube Rigid Body Properties:
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class MoveCube : MonoBehaviour
{
private float min_X = -2.2f, max_X = 2.2f;
public bool canMove;
private float move_Speed = 2f;
private Rigidbody myBody;
private bool gameOVer;
private bool ignoreCollision;
private bool ignoreTrigger = false;
public float gravityScale = 10.0f;
// Global Gravity doesn't appear in the inspector. Modify it here in the code
// (or via scripting) to define a different default gravity for all objects.
public static float globalGravity = -9.81f;
void Awake(){
myBody = GetComponent<Rigidbody>();
myBody.useGravity = false;
}
void Start()
{
canMove = false;
move_Speed *= -1f;
GameManager.instance.currentBox = this;
}
void Update()
{
if(!canMove)
{Gravity();}
MoveBox();
}
void MoveBox(){
if(canMove){
Vector3 temp = transform.position;
temp.x += move_Speed * Time.deltaTime;
if(temp.x > max_X){
move_Speed *= -1f;
}
else if(temp.x < min_X){
move_Speed *= -1f;
}
transform.position = temp;
}
}
void Gravity ()
{
Vector3 gravity = globalGravity * gravityScale * Vector3.up;
myBody.AddForce(gravity, ForceMode.Acceleration);
}
public void DropCube(){
canMove = false;
}
void Landed(){
if(gameOVer)
return;
ignoreCollision = true;
ignoreTrigger = true;
GameManager.instance.SpawnNewBox();
GameManager.instance.MoveCamera();
}
void RestartGame(){
GameManager.instance.RestartGame();
}
void OnCollisionEnter(Collision target){
if(ignoreCollision)
return;
if(target.gameObject.tag == "Platform"){
Debug.Log("Platform Collision");
Invoke("Landed",2f);
ignoreCollision = true;
}
if(target.gameObject.tag == "Box"){
Invoke("Landed",2f);
ignoreCollision = true;
}
}
}
Blockquote