Hello all,
I set my Player’s movement using AddForce, but there is a bit of a stutter effect when the Player runs into transform.translate barriers. Below is the code I am using.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
public float speed = 100.0f;
public float jumpForce = 10.0f;
public float playerBoundX = 44.0f;
public float playerGravity = 2f;
public Rigidbody playerRb;
public bool isOnAir = true;
public bool isOnGround = true;
public bool hasHitWall = true;
// Start is called before the first frame update
void Start()
{
playerRb = gameObject.GetComponent();
Physics.gravity *= playerGravity;
isOnAir = false;
hasHitWall = false;
}
// Update is called once per frame
void Update()
{
PlayerMovement();
PlayerBoundaries();
hasHitWall = false;
}
//Lets the Player move and jump.
void PlayerMovement()
{
//Left and Right movement.
float horizontalInput = Input.GetAxis(“Horizontal”);
if (!hasHitWall)
{
playerRb.AddForce(Vector3.right * speed * horizontalInput);
}
//Space to Double Jump.
if (Input.GetKeyUp(KeyCode.Space) && isOnGround && !isOnAir)
{
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isOnGround = false;
isOnAir = true;
}
if (Input.GetKeyDown(KeyCode.Space) && !isOnGround && isOnAir)
{
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isOnAir = false;
}
if (isOnAir && isOnGround)
{
isOnAir = false;
}
}
//Sets the screen boundaries for the player.
void PlayerBoundaries()
{
//Left Boundary.
if (transform.position.x < -playerBoundX)
{
transform.position = new Vector3(-playerBoundX + 1, transform.position.y, transform.position.z);
hasHitWall = true;
}
//Right Boundary.
if (transform.position.x > playerBoundX)
{
transform.position = new Vector3(playerBoundX - 1, transform.position.y, transform.position.z);
hasHitWall = true;
}
}
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag(“Ground”))
{
isOnGround = true;
}
}
}
Is there a remedy for this?