- Home /
Question by
vincentains44 · Apr 24, 2018 at 04:26 AM ·
c#charactercontroller
My 2D player controller isn't working. Why?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
//how fast player can move
public float topSpeed = 10f;
//tell player which direction it is facing
bool facingRight = true;
public Rigidbody2D rb2d;
void Start()
{
rb2d = GetComponent<Rigidbody2D> ();
}
//physics
void fixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
rb2d.velocity = new Vector2 (moveHorizontal * topSpeed, rb2d.velocity.y);
if (moveHorizontal > 0 && !facingRight)
Flip ();
else if (moveHorizontal < 0 && facingRight)
Flip ();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Comment
Answer by Priyanka-Rajwanshi · Apr 24, 2018 at 06:03 AM
@vincentains44 It should not be fixedUpdate but FixedUpdate.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
2d Movement Jumps to the Left 3 Answers