Hello everyone! I am working on making an auto runner game. I have a Powerup called “Shield” that gives the Player another point of health. It is supposed to disappear when the Player touches it. The code works, and it does both of those things, however I’ve noticed while playing that the Player also slows down/appears to shake a little bit when it comes into contact with the Powerups, and also after jumping off of enemies’ heads if it connects with the enemy in a weird way. Like if it jumps on the enemies’ head towards the end of the sprite, for example.
I think it MAY have to do with the Translate code I am using to make the Player automatically move right. However, I also have a “Speed” variable that I don’t want to remove because I am going to have objects later that will increase that speed. It could also have to do with the Player’s Rigidbody, because the Player also does this when they collide with Enemies. I also have the Player’s gravity set to 6, and they are set to Dynamic in their Rigidbody2D. The Powerups do not use a Rigidbody, and only use a capsule collider. I figured they wouldn’t need a rigidbody because they are just powerups and won’t be moving anywhere. The enemies however, DO have Rigidbody2D.
Here is my code for the Player’s Movement:
public class Movement : MonoBehaviour
{
public Rigidbody2D player;
// Speed that the Player moves forward
public float speed = 1.0f;
// Jump Speed Variable. Changing makes Player jump higher.
public float jumpSpeed = 15f;
//Ground Check Variables
public UnityEngine.Transform groundCheck;
public float groundCheckRadius;
public LayerMask groundLayer;
private bool isTouchingGround;
// Animator Variable
public Animator animator;
private void Start()
{
player = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
// Ground Check
isTouchingGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
// Makes the Player constantly move forward to the right
// Speed * 15 makes the player move faster
transform.Translate(Vector3.right * (speed * 12) * Time.deltaTime);
// Make Player Jump
if (Input.GetButtonDown("Jump") && isTouchingGround)
{
player.velocity = new Vector2(player.velocity.x, jumpSpeed);
}
}
}
Here is my code for picking up the Shield Powerup:
public class ShieldCollect : MonoBehaviour
{
public int addhealth;
// Calls the PlayerHealth script
public PlayerHealth playerHealth;
private void OnCollisionEnter2D(Collision2D collision)
{ // If the Player collides with the Shield powerup
if (collision.gameObject.tag == "Player")
{
// Increase Player's Health by 1
playerHealth.HealthUp(addhealth);
// Remove the Shield Powerup from the level
Destroy(gameObject);
}
}
}
And finally the code for jumping on Enemies.
public class EnemyStomp : MonoBehaviour
{
public Rigidbody2D player;
public float jumpSpeed = 15f;
private void OnCollisionEnter2D(Collision2D collision)
{
// When Player's foot collides with Enemies that have the "WeakPoint" tag
if(collision.gameObject.tag == "WeakPoint")
{
// Makes the Player bounce off of the enemy when they stomp on them.
// Reuses the same code as whent the Player Jumps.
player.velocity = new Vector2(player.velocity.x, jumpSpeed);
// Destroy the Enemy when Player's foot hits them.
Destroy(collision.gameObject);
}
}
}