I'm developing a platformer in Unity using the 2D engine. I have my player character which has a BoxCollider2D and a RigidBody, and a number of 'walls' which have BoxColliders.
Now, I copied the script for moving the player from another project and made some changes. The part which has to do with movement is as follows:
public void FixedUpdate()
{
physVel = Vector2.zero;
// move left
if(Input.GetKey(KeyCode.LeftArrow))
{
physVel.x = -runVel;
}
// move right
if(Input.GetKey(KeyCode.RightArrow))
{
physVel.x = runVel;
}
// jump
if(Input.GetKey(KeyCode.UpArrow))
{
if(jumps < maxJumps)
{
jumps += 1;
if(jumps == 1)
{
_rigidbody.velocity = new Vector2(physVel.x, jumpVel);
}
}
}
//Apply gravity
_rigidbody.AddForce(-Vector3.up * fallVel);
// actually move the player
_rigidbody.velocity = new Vector2(physVel.x, _rigidbody.velocity.y);
}
Now this works perfectly fine.
The problem arises if the player jumps into a wall. If I keep the direction button mashed 'towards' the wall after having jumped, he is suspended in mid-air. As in the collision appears to be reducing movement on both axis to zero. If I release the direction, he falls normally. Collisions on the other axis works fine. I can hit my head or walk without issue.
Am I missing something obvious?
EDIT: try adding a material with 0 friction to both player and walls and see what happens, if it stops it is a friction error.
Related
Brand new to Unity/C# so this might be a stupid error. I have a player and a push-able box that when pushed towards another object (that usually the player cannot walk through) causes the box to stop moving and the player to get stuck and be unable to move but stuck mid-animation.
They basically just all get stuck in eachother
https://i.stack.imgur.com/rkNtu.png
I followed tutorials for a lot of these things but couldn't manage to find one for pushing the box so I did it by myself, which is what I'm thinking caused these issues.
The player has a 2D circle collider and a 2D rigidbody (with a dynamic body type and discrete collision detection).
It also has all of its code to do with walking that looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//Movement speed variable
public float moveSpeed;
//Giving information of where the collisions layer is (edited in actual unity)
public LayerMask collisionsLayer;
//Variable to check if the character is moving
private bool isMoving;
private Vector2 input;
//Animation SetUp
private Animator animator;
private void Awake()
{
animator = GetComponent<Animator>();
}
//End of Animation SetUp
private void Update()
{
//If the player is NOT moving
if (!isMoving)
{
//'GetAxisRaw' gives 1 or -1 --> right = 1, left = -1
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
//Making it so that the player cannot move diagonally
if (input.x != 0) input.y = 0;
if (input != Vector2.zero)
{
//Animation section
animator.SetFloat("moveX",input.x);
animator.SetFloat("moveY",input.y);
//End of animation section
var targetPos = transform.position;
//Adds 1 or -1 depending on the key input (GetAxisRaw)
targetPos.x += input.x;
targetPos.y += input.y;
if (IsWalkable(targetPos))
//Calls coroutine 'IEnumerator Move'
StartCoroutine(Move(targetPos));
}
}
//for animation
animator.SetBool("isMoving",isMoving);
//Special type of function w/ 'IEnumerator' as return type. Used to do something over a period of time.
IEnumerator Move(Vector3 targetPos)
{
isMoving = true;
//Checks if the diff between the target position & the players current position is greater than a vry small value
while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
//If there is a diff btwn targetpos & the current position --> we will move the player towards the target position by a very small amount
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
//Stops execution of the coroutine, resumes it in the next update function
yield return null;
}
//Sets the players current position to the target position
transform.position = targetPos;
isMoving = false;
}
}
//Checking if the next available tile is actually able to be walked on / if the next tile is blocked
private bool IsWalkable(Vector3 targetPos)
{
if(Physics2D.OverlapCircle(targetPos, 0.1f, collisionsLayer) != null)
{
return false;
}
return true;
}
}
The box has a box collider 2D and a 2D rigidbody (dynamic bodytype; discrete collision detection)
And the collisions tilemap (which is set to be used by the composite) has a tilemap collider 2D, rigidbody 2D (static) and a composite collider 2D
If anyone actually knows how to make it so that the entire game doesn't break when I try to move the box past objects, that'd be really helpful because I've been working on this for ages and haven't figured anything out at all
You should try running the debugger. I think you're finding yourself in an interlocked condition, where your character can't get to the target position (because it's blocked), but it hasn't reached the target position (because it's blocked), so it keeps trying to move, which prevents you from giving it a new command.
I think the easiest way to work around this is to cache your target directions - should be in positive x, positive y, or negative x, or negative y. Then, if you detect the user trying to move in the opposite direction you cancel the move and start a new one.
There are lots of ways to work around this, though, and I do see you are trying to check the target before moving to it, but you're checking the raw input, which may overshoot an obstacle. For example, if the tree is at 0.5 meters, but you're looking at a target of 1 meter, then the target is clear. You get blocked by the tree at 0.5 meters, and because you never reach 1 meter you never exit the coroutine.
You've got to run a debugger and step through the code and see what specifically isn't responding.
I'm starting to learn C# with Unity and I guess my code present some issues, the movement of my player works but not that good. Let me explain, I have a player at the bottom of the screen, moving only from left to right by itself, using transform.translate (I didn't used rigidbody) because on collision with the sides it stops, and is not a constant movement, the idea is that, once collides with the sides, it changes of direction, but same velocity.
The issue happens when the player colides with the walls (removed the mesh renderer to keep them transparent) but if at the same time you press the key to trigger also the change of direction the player gets stuck at the corner, as shown on this gif example:
Wait for the Player to get stuck at the corners, the gif takes like 15 secs.
And here's my actual Script for the Player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PMovement : MonoBehaviour
{
public float playerSpeed = 8.0f;
void Update()
{
float moveX = playerSpeed * Time.deltaTime;
transform.Translate(moveX, 0, 0);
if (Input.GetKeyUp(KeyCode.Space))
{
playerSpeed = playerSpeed * -1;
}
}
void OnCollisionEnter(Collision target)
{
if (target.gameObject.tag.Equals("SideWalls") == true)
{
playerSpeed = playerSpeed * -1;
}
}
}
Your code looks ok for what you want to achieve. Unfortunately, in Unity it may not be enough to pin point the problem, as the code heavily depends on the setting from the editor. One possible solution to this or similar problems:
If you still have blocking colliders, (The collision functions need some sort of rigidbody.), or if you clamp your player's position, to stop overflow, the following can happen:
Collider on right side is hit
Player direction change to left
Space is hit while colliders are still touching
Player direction change to right again
There is no collider on right side left to start the colliderEnter event again.
In this case block your space recognition while the player is touching one side.
If you change direction when you hit the wall, but you press Space at just the right time, you'd end up inside the wall a bit most likely. Then it'd keep thinking it was entering the wall collision and reversing direction over and over. Here's what I'd do.
public class PMovement : MonoBehaviour
{
private List<GameObject> Collisions = new List<GameObject>();
public float playerSpeed = 8.0f;
void Update()
{
float moveX = playerSpeed * Time.deltaTime;
transform.Translate(moveX, 0, 0);
if (Collisions.Count <= 0 && Input.GetKeyUp(KeyCode.Space))
{
playerSpeed = playerSpeed * -1;
}
}
void OnCollisionEnter(Collision target)
{
if (!Collisions.Contains(target.gameObject) && target.gameObject.tag.Equals("SideWalls") == true)
{
Collisions.Add(target.gameObject);
playerSpeed = playerSpeed * -1;
}
}
void OnCollisionExit(Collision target)
{
if (Collisions.Contains(target.gameObject)) {
Collisions.Remove(target.gameObject);
}
}
}
By keeping a list of wall objects you're inside, you can disallow the space bar if you're inside a wall. Keep in mind there's probably better ways to do the things you're doing, but this should fix the issue you're having for now.
I have a player game object. Whenever I change direction, I flip the player's x scale, so it's 1 when he's facing right and -1 when he's facing left.
This code is responsible for flipping the player scale and moving the player horizontally:
void FlipScale()
{
facingRight = !facingRight;
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
}
void MoveHorizontally()
{
if (wantsToMoveLeft && wantsToMoveRight) { return; }
if (wantsToMoveRight)
{
rb.velocity = new Vector2(runVelocity, rb.velocity.y);
if (!facingRight) { FlipScale(); }
}
else if (wantsToMoveLeft)
{
rb.velocity = new Vector2(-runVelocity, rb.velocity.y);
if (facingRight) { FlipScale(); }
}
}
The player object has a child object, which has the main sprite renderer that uses this shader material:
Continuously changing the Fade from 1 to 0 creates an effect of the player dissolving, finally disappearing completely at 0. Here is what it looks like half-way:
However, when I flip the player using the script above, the shader breaks, and the player disappears. Additionally, when I change the Fade value from 1 to exactly less than 0.1, the player suddenly appears to be glowing:
Flipping the sprite again does not fix the issue. Furthermore, flipping the sprite through the inspector does not break the shader, only when I do it through code. I used this tutorial to create this shader: https://www.youtube.com/watch?v=WiDVoj5VQ4c. This is my current shader graph:
I have just started using shaders, so I apologise if the problem is very obvious. I tried a lot of fixes on stack overflow and other websites, but nothing has worked so far. If I didn't include enough information, please ask.
EDIT 1: Found out that, when the shader breaks, the player starts glowing whenever the Fade value is below the Edge value. If it's the same or above, the player looks normal, but the dissolve effect still doesn't work.
EDIT 2: Changing to an unlit shader doesn't fix the issue.
So I recently came back to the project and after testing for about an hour, I facepalmed myself really hard when I found out the problem.
I was using Vector2 to flip the scale!!! Vector2 defaults the z value to 0. That's why flipping it in the inspector didn't break anything...
The solution is to simply use Vector3 like so:
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, tranform.localScale.z);
or:
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, 1f);
Recently I have started my first 2D game project on unity and everything has been going well. My only problem so far is with my enemy. My enemy, when attacking, jumps in the air and then falls and hits the ground. I want my script to detect when it falls and how hard it falls then create a force that pushes the player back as if its an "explosion."
So my questions are how do I detect the enemy hitting the ground, after a fall, and then add a force?
I tried using onCollisionEnter2D on Unity but it does not work since technically even when the enemy is moving it's still "falling."
Here is my attempt at checking if the enemy fell then searching for the player and calling the explosion force function.
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Ground")
{
foreach (Collider2D Obj in Physics2D.OverlapCircleAll(transform.position, radius))
{
if (Obj.GetComponent<Rigidbody2D>() != null && Obj.gameObject != gameObject)
{
Debug.Log("Calling Function");
Rigidbody2D rb = Obj.GetComponent<Rigidbody2D>();
ExplosionForce2D forceScript = GetComponent<ExplosionForce2D>();
forceScript.AddExplosionForce(rb, force, transform.position, radius);
}
}
}
}
This is my code for adding a force to the object.
public void AddExplosionForce (Rigidbody2D body, float expForce, Vector3 expPosition, float expRadius)
{
var dir = (body.transform.position - expPosition);
float calc = 1 - (dir.magnitude / expRadius);
if (calc <= 0) {
calc = 0;
}
body.AddForce (dir.normalized * expForce * calc);
}
I expect that, if the player is in the enemy radius, and the enemy jumps, falls, and hits the floor it will push the player back as if it was an explosion.
you can use a flag that can check that if the enemy is falling or not, so when your enemy is actually falling set this as true, if not (means moving) then set as false.
Checking for Velocity is a good solution.
So you can do like,
body.velocity.magnitude
// magnitude to remove the direction issue.
// now apply all this as
body.AddForce(dir.normalized * expForce * calc * body.velocity.magnitude);
I try to move my character but he moves through the border of the scene.
When I debug it sayscharTransform has 0 0 0 coords in Vector3.
Transform charTransform;
float leftHorizontalBound;
float rightHorizontalBound;
void Start()
{
charTransform = this.transform;
leftHorizontalBound = camera.ViewportToWorldPoint (new Vector3 (0,0, camera.nearClipPlane)).x;
rightHorizontalBound= camera.ViewportToWorldPoint (new Vector3 (1,0, camera.nearClipPlane)).x;
}
void Update()
{
if(charTransform.position.x <= leftHorizontalBound)
{
charTransform.position = new vector2(leftHorizontalBound + 0.1f);
return;
}
if(charTransform.position.x >= rightHorizontalBound)
{
charTransform.position = new vector2(rightHorizontalBound - 0.1f);
return;
}
//MAKE HERE YOUR MOVEMENT BASED ON INPUT.
}
i cant exactly say what is your problem and wish you assume your problem more accurate but i think you are working something like a boad game with a fixed camera and something like a ball always moves and you just want to keep that object in the scene.
in your code you defined a position by variables and made a position check for every frame. thats not what basically is done in unity and there will be problems. you can make an empty object and put in on the borders you want and check the position of that object with your moving object.
your next way is make an object and add a collider component to it. if your game is 3d add a collider and if its 2d game add a 2d collider and a collider to moving object and after that it never moves toward the borders but still there will be some limits so if it didnt work i think you should add a rigidBody to both of them.