Unity3d - Collision detection and position inaccuracy - unity3d

I'm a beginner on Unity3D and I decided to create a 3D Tetris to start learning. Everything was fine so far, but I have a problem with collision detection.
https://gyazo.com/49fbf5798dc67a546c5e187fde8f6096
As you can see on this screen, the blue tetromino is not at the good place. This is due to the fact that the orange tetromino position in X is not precise (-2.9999).
Each block that makes up a tetromino has the tag "Block". The ground has the tag "Ground".
This is how I detect collisions in my OnCollisionEnter method.
if (collision.collider.tag == "Block" && !detectedBefore)
{
for (int k = 0; k < collision.contacts.Length; k++)
{
if (Vector3.Angle(collision.contacts[k].normal, validDirection) <= contactThreshold)
{
GetComponent<TetrominoMovement>().Snap();
GetComponent<TetrominoMovement>().enabled = false;
FindObjectOfType<Tetromino>().GenTetromino();
detectedBefore = true;
break;
}
}
}
if (collision.collider.tag == "Ground" && !detectedBefore)
{
GetComponent<TetrominoMovement>().Snap();
GetComponent<TetrominoMovement>().enabled = false;
FindObjectOfType<Tetromino>().GenTetromino();
detectedBefore = true;
}
Do you know how to get around this problem? Thank you in advance.

Related

Stop building momentum when running into a wall. (2D Top-Down)

So my movement isn't instant. You accelerate and decelerate. I use two floats starting at 0, going to 1, to calculate my movement speed. Here it is:
private void Update()
{
float moveTowardsX = 0;
float moveTowardsY = 0;
// Adjust thrust to be below max Thrust
if(thrust >= maxThrust)
{
thrust = maxThrust;
}
// Calculates movement speed based on all variables
float changeRatePerSecond = 1 / timeFromZeroToMax * thrust * Time.deltaTime;
changeRatePerSecond /= weight / 1.5f;
if(onMobile == true)
{
// Checks for Input. If Input is detected, values get changed [MOBILE]
if(isMovingLeft == true && isMovingRight == false)
{
moveTowardsX = -1.0f;
}
else if(isMovingRight == true && isMovingLeft == false)
{
moveTowardsX = 1.0f;
}
if(isMovingUp == true && isMovingDown == false)
{
moveTowardsY = 1.0f;
}
else if(isMovingDown == true && isMovingUp == false)
{
moveTowardsY = -1.0f;
}
if(isRotatingLeft == true && isRotatingRight == false)
{
rotationsPerSecond++;
}
else if(isRotatingRight == true && isRotatingLeft == false)
{
rotationsPerSecond--;
}
}
Then I parse these values into my movement code and smooth them out:
void FixedUpdate()
{
// Makes values change smoothly
valueX = Mathf.MoveTowards(valueX, moveTowardsX, changeRatePerSecond);
valueY = Mathf.MoveTowards(valueY, moveTowardsY, changeRatePerSecond);
// Turn numbers into movement
rb.velocity = new Vector2(valueX * runSpeed, valueY * runSpeed);
}
The Problem is that when I run into a wall, the numbers don't decrease. And if I run into a wall standing still, the numbers increase. Here is a video linking to my problem too (Look at the top right): https://youtu.be/XRE9p0yo4GA
What could I implement to fix this?
I have 2 suggestions, either one should work:
Do a ground check. Platformers often perform ground checks to check that the player is on the ground. By doing a box cast, you can check if your character is adjacent to a wall in the direction of their movement, and if they are, do not let them move (or gain speed) in that direction.
Here is a video for how to do a ground check. I would recommend the box cast method here, since it would give you the most accurate results:
https://www.youtube.com/watch?v=c3iEl5AwUF8
Use the rb.velocity of the object in a direction divided by your character's maximum speed in place of your valueX and valueY. So, use:
rb.velocity.x / runSpeed
instead of
valueX
and the same for y. This would take into account anything that changes the velocity of the object outside this script, such as crashing into a wall, but also if your character is thrown by some effect you create later on.

Unity 3d Gun Recoil Animation IK Problems

im having trouble with the hand IK of a -recoil shoot animation- that is on an override layer in the animator. I have it set up so that when I aim(left trigger), it allows me to shoot with another button(right trigger). I have it so the weight (hand IK) increases to 1 (in the rig layer) when I aim and decreases to 0 when I let go of the aim button.
The problem is that if I shoot the gun (uses recoil gun animation) and let go of the aim button immediately afterwards, the animation will continue to play but the IK will go to 0 and the hands will look all messed up.
I've tried setting the IK hand weight to 1 when he fires the gun, so he'll still hold onto it, but it doesn't work since shoot is only true if aim is being held down.
So overall I fire the gun, he does a recoil animation, and when you let go of aim too early, the animation looks all messed up like he's waving to you from behind his back. Like it aborts the animation or something.
private void HandleMovementInput()
{
horizontalMovementInput = movementInput.x;
verticalMovementInput = movementInput.y;
animatorManager.HandleAnimatorValues(horizontalMovementInput, verticalMovementInput, runInput);
if (verticalMovementInput != 0 || horizontalMovementInput !=0) //running
{
if (aimingInput)
{
if (animatorManager.isAimingGunn == false)
{
shootInput = false;
}
}
}
if (animatorManager.isAimingGunn)
{
animatorManager.leftHandIK.weight = 1; // IK weight
animatorManager.rightHandIK.weight = 1;
horizontalMovementInput = 0f; // joystick
verticalMovementInput = 0f;
}
else if(animatorManager.isAimingGunn == false)
{
if(verticalMovementInput != 0 || horizontalMovementInput !=0) //running
{
animatorManager.leftHandIK.weight = 0;
}
animatorManager.leftHandIK.weight = 0; // IK weight
animatorManager.rightHandIK.weight = 0;
}
}
private void HandleShoot()
{
if (aimingInput) // must aim otherwise shootInput(right trigger on controller) wont work
{
if (shootInput)
{
shootInput = false;
playerManager.UseCurrentWeapon();
}
}
}
I was able to fix it. I used this code to tell the character to keep aiming if still in the current animation.This allows for the animation to finish before you set the Hand IK to 0.
if(animator.GetCurrentAnimatorStateInfo(1).IsName("Pistol_Shoot"))
{
aimingInput = true;
}
Also Due to it getting overly complicated I changed the aim cancel button to the top left trigger on the controller ( while aim is on the bottom left trigger).

How to negate my player getting stuck if there is something above his head while crouching ( unity 3d )?

ive made this simple code to regulate the height of the player once Lcontrol is pressed
//crouching
if (Input.GetKey(KeyCode.LeftControl))
controller.height = 1.0f;
else
controller.height = 2.0f;
but my charachter gets stuck if there is a roof over his head while crouchin
how do i make a function that detects if there is something above my head so even if i unpress the Lcontroll button my caracter stays crouched
how do i lower the camera/body automatically because the camera rn gets stuck in the roof ( with the stuck player )
and you know generally be a smooth crouch ^^" , thanks in advance !
You can make one transform public "checkRoof ", put on the head of character and make something like this
if (Input.GetButtonDown("Down") && (grounded || checkRoof == true))
{
anim.SetBool("pressDown", true);
ChangeCollidersCrouch(); //change colliders (goes from character to character)
crouched = true;
}
if (!Input.GetButton("Down") && grounded && checkRoof == false)
{
anim.SetBool("pressDown", false);
anim.SetBool("Iscrouchwalk", false);
ChangeCollidersIdle();
speed = speedFix;
crouched = false;
}
To lower my camera I just attach it to my player so when my player gets smaller the camera goes down with it.

problem with gravity and sliding of an object

Well I have a problem, when I press a letter the player must climb or descend (depend of input player on y) and if I keep the letter pressed it should stay still on the wall without move, here is the problem with mo gravity and when I press the key the player don't stay on the wall, tha player continues sliding slow this is for the gravity on the game, I can't desability the gravity because I make special jumps of the wall. so my velocity.y = 0 don't work. How can create a solution for this.
wallDirX = GetDirectionWallPlayer();
if (controller.collisions.left || controller.collisions.right)
{
stateWallPlayer.WallClimbing = true;
if (directionalInput.y > 0)
{
if (velocity.y < wallClimbUpSpeedMax)
{
velocity.y = wallClimbUpSpeedMax;
}
}
else if (directionalInput.y < 0)
{
if (velocity.y < -wallClimbDownSpeedMax)
{
velocity.y = -wallClimbDownSpeedMax;
}
}
else
{
velocity.y = 0;
}
}

OnCollisionEnter is not working:

Please Guys help, I am new to Unity and programming:
I have a two Bouncing Ball, tag as BouncingBall1 and BouncingBall2, I want when a bullet hit both to destroy and if the time have not exceeded displaySecond and you have destroy the balls you win, but my problem is the OnCollisionEnter is not working. The part of wining is not working the rest are, my code fragment is below.
function OnCollisionEnter(col.collision) {
if ((displaySecond < 30) && (
col.gameObject.tag ==
BouncingBall1 == null &&
col.gameObject.tag ==
BouncingBall1 == null)) {
print("You have won");
}
}
col.gameObject.tag == BouncingBall1 == null
I think the problem is with the way you check if an object is destroyed. This isn't the way to do it. Also, you're checking if two identical lines of code are identical. So this will always return true if the display second is lower than 30.
Try changing it to
if ( (displaySecond < 30) &&
(col.gameObject.tag == "BouncingBall1") ) {
Debug.Log("You have won!")
}
It sounds like you want keep track of whether each ball has been destroyed and display "You have won" when both have been destroyed. This code is a little crude but should achieve this:
var ball1Destroyed = false;
var ball2Destroyed = false;
...
function OnCollisionEnter(col : Collision) {
if (col.gameObject.tag == "BouncingBall1") {
ball1Destroyed = true;
}
if (col.gameObject.tag == "BouncingBall2") {
ball2Destroyed = true;
}
if (displaySecond < 30 && ball1Destroyed && ball2Destroyed) {
Debug.Log("You have won");
}
}
This code assumes that the only collisions will be between bullets and balls (not between one ball and another), this can be done with collision layers.