Why does my platform move when I press my directional keys when it does not have the PlayerController script?
Hello ! I would like someone to help me solve my problem.
Okay, I'm doing the Create with code course and I arrived at Lab4. I planned my personal project ... A platform game combine with an endless runner in side view (the camera is in orthographic mode) with 3D models.
I created a new Unity project, set up primitives for prototyping and wrote the player's script, the PlayerController, the game object "player" to a Rigidbody component because I need it for gravity so I use playerRb.AddForce () to move the cube that represents the player.
Now I'm programming the basic gameplay, platforms must appear on the right and move to the left using the AddForce method.But the problem is that when I test my game and jump up my cube on the platform, it's not my cube that moves when I press the direction keys but the platform itself. And when I jump, so when I'm not in contact with the platform it starts to move to the left as it is written in his script.
I tried to change the conditions that changed the variable isGrounded, because at first it was only to change when the player jumped and fell to the ground but when I did not jump and fall from an isGrounded platform remained true, which that the cube could be controlled in the air quite easily and could jump at any moment. So I had to do many tries until I found the trick to use OnCollisionStay () and OnCollisionExit () instead of testing if the object gets into the soil collider or jumps.
And in the end everything worked until I moved the platform and after trying to move it without physics, with the method transform.Translate () ... Which gave a bad result of collision I end up using a physic.Raycast () as a last resort. And the jump against him works very well.
Question: Why does my platform move when I press my directional keys when it does not have the PlayerController script?
Here is the code for you to understand my problem and maybe find the error I made:
public class PlayerController: MonoBehaviour
{
// Control the speed of movement of the player on land and in the air
public float speed = 60.0f;
public float flySpeed = 10.0f;
// Control the power of the impulse that the player takes to jump
public float jumpForce = 8.0f;
public float threasold = 1.0f;
// Check if the player is on the ground or in the air
private bool isGrounded = true;
// the position on the X axis of the invisible wall on the right
private float xBound = 24;
private Rigidbody playerRb;
private BoxCollider playerCollider;
// Start is called before the first frame update
void Start ()
{
// Get the Rigidbody component that is on the GameObject "Player"
playerRb = GetComponent <Rigidbody> ();
playerCollider = GetComponent <BoxCollider> ();
}
// Update is called once per frame
void FixedUpdate ()
{
MovePlayer ();
ConstrainPlayerPosition ();
}
// Allows the player to move using the arrow keys and the spacebar
void MovePlayer ()
{
// Get the keyboard input of the user
float horizontalInput = Input.GetAxis ("Horizontal");
// Check if the player is on the floor
if (Physics.Raycast (transform.position, transform.TransformDirection (-Vector3.up), playerCollider.bounds.extents.y + threasold))
{
isGrounded = true;
Debug.Log ("isGrounded =" + isGrounded);
}
else
{
isGrounded = false;
playerRb.AddForce (Vector3.right * flySpeed * horizontalInput);
Debug.Log ("isGrounded =" + isGrounded);
}
// Move the character to the right or to the left with the right arrow and the left arrow when not hanging on the edge of a platform
if (isGrounded)
{
playerRb.AddForce (Vector3.right * speed * horizontalInput);
}
// Make the character jump when the player presses the spacebar and the character is on the ground
if (Input.GetKeyDown (KeyCode.Space) && isGrounded)
{
playerRb.AddForce (Vector3.up * jumpForce, ForceMode.Impulse);
}
}
// Prevents the player from leaving the field of view of the camera
void ConstrainPlayerPosition ()
{
// Create an invisible wall at the left edge of the screen
if (transform.position.x <-xBound)
{
transform.position = new Vector3 (-xBound, transform.position.y, transform.position.z);
}
// Create an invisible wall at the right edge of the screen
if (transform.position.x> xBound)
{
transform.position = new Vector3 (xBound, transform.position.y, transform.position.z);
}
}
}
Answer by lgarczyn · Nov 06, 2019 at 12:36 PM
First, is your platform a dynamic rigidbody? If it moves, it should a kinematic rigidbody, if it doesn't it should just be a non-trigger collider. Except for that I do not see anything that could be moving the platform. Check all other active scripts, especially those that use Input.
Second, to constrain the player you should really use colliders, or at least Rigidbody.MovePosition, not Transform.position.
You should be careful about allowing infinite acceleration for your player. This can be solved by drag, max lateral velocity, or simply setting the velocity to speed * input instead of using AddForce.
Thanks for your help ! I asked my question on another forum and I was able to solve my problem. Was the shape of the collider that had problem because it was explained to me that a cube (player) tends to cause another moving cube (in this case the platform) when they collide,
So I have replaced by a sphere. But you solved another of my problems by advising me to use Rigidbody.$$anonymous$$ovePosition () ins$$anonymous$$d of Transform.Translate () to constrain the player's position. And I try to pay attention to infinite acceleration as you suggested.
Your answer
Follow this Question
Related Questions
Adding a Highscore to game 1 Answer
Ammo Script 0 Answers
Input Command Issues 0 Answers
Why is the script not working (function not changing value) 0 Answers