I’m doing endless runner in Unity and my player is for some reason moving to the left without me pressing anything.
Here is the code that I’m using:
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerMovement : MonoBehaviour
{
bool alive = true;
public float speed = 5;
public Rigidbody rb;
float horizontalInput;
public float horizontalMultiplier = 2;
void FixedUpdate()
{
if (!alive) return;
Vector3 forwardMove = transform.forward * speed * Time.fixedDeltaTime;
Vector3 horizontalMove = transform.right * horizontalInput * speed * Time.fixedDeltaTime * horizontalMultiplier;
rb.MovePosition(rb.position + forwardMove + horizontalMove);
}
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
if(transform.position.y < -5)
{
Die();
}
}
public void Die()
{
alive = false;
Invoke("Restart", 1);
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
And here is screenshot of the player:
Can someone please help me?
