- Home /
Block Movement
Hello, I have a problem:
I have a BlockMovement with which you can move blocks with left arrow / right arrow, left or right.
The BlockMovement is on the prefabs of all blocks.
Well, if a block fell down. A new one spawns.
But now I can move both blocks.
And then three, four, etc.
How do I fix that only the newly spawned block moves or the block that has already fallen is "fixed" in its position?
using UnityEngine;
public class BlockMovement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.position = new Vector3(transform.position.x + 1,
transform.position.y,
transform.position.z);
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position = new Vector3(transform.position.x - 1,
transform.position.y,
transform.position.z);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Block") || collision.gameObject.CompareTag("Ground"))
{
this.enabled = false;
}
}
}
Answer by TheOnlyMike675 · Oct 11, 2020 at 04:48 PM
Do the blocks have a collider and a rigidbody? And are the tags properly assigned to the blocks? If the rigidbodies/colliders are 2D make sure to use OnCollisionEnter2D instead of OnCollisionEnter
The blocks have an collider and an rigidbody, tags are set, its an 3d game so theyre 3d
Could you try a debug.Log inside the OnCollisionEnter? Curious to see if the collision is being detected