- Home /
Move Script Issues
I have a move script, that I'm working on. I took the base script from the API and have been gradually adjusting to get the desired effects. I've recently run into an issue in which, when the player moves left, he doesn't flip over to do so. It maybe important to note the following as well, the character is a 3d character that I'm using for a 2d platformer. Originally, I was using Rigidbody but since I'm still getting the hang of Unity 5 (after just kind of figuring out 4.5) I found that the character controller with the base API was a better starting point.
Ultimately, I know it's up to me to figure it out since this is for my portfolio class in school but any help to get the player to flip the way he's facing would be greatly appreciated.
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PMTest : MonoBehaviour { public float speed = 6.0F; public float jumpSpeed = 8.0F; public float gravity = 20.0F; private Vector3 moveDirection = Vector3.zero; bool facingRight = true;
void FixedUpdate() {
CharacterController controller = GetComponent<CharacterController> ();
if (controller.isGrounded) {
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"), 0);
moveDirection *= speed;
if (Input.GetButton ("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
if (Input.GetKey ("right")) {
GetComponent<Animation> ().Play ("walk");
FlipFacing ();
} else if (Input.GetKey ("left") && !facingRight){
GetComponent<Animation> ().Blend ("walk");
}else if (Input.GetButtonDown ("Jump")) {
GetComponent<Animation> ().Play ("jump");
}
if (Input.GetKey ("space")) {
GetComponent<Animation> ().Play ("punch");
}
if (Input.GetKey ("down")) {
GetComponent<Animation> ().Play ("walk");
}
if (moveDirection > 0 && !facingRight) {
FlipFacing ();
}
}
void FlipFacing(){
facingRight = !facingRight;
Vector3 charScale = transform.localScale;
charScale.x *= -1;
transform.localScale = charScale;
}
}
Are you sure the code is reaching the FlipFacing() function? I would throw in a Debug.Log("Flipped.");
Just in order to make sure you get to the code.