Hello all!
I am having an issue with my character controller script. It is working perfectly except for one issue. Whatever object I attach it to has its Y position increased by 8 at runtime. Essentially, my Idlestate script updates myVector and then my player controller script runs myController.Move(myVector).
I have a variable for velocity which I set myVector.y = to. This is my first time posting a question and I apologize if the formatting is off. Any help would be much appreciated!
public class PlayerController : MonoBehaviour
{
// Create an instance of the statemachine
public HStateMachine stateMachine = new HStateMachine();
// Start is called before the first frame update
void Start()
{
// Start the state off in the Idle State
stateMachine.ChangeState(new HIdleState(this));
// Reference to the animator
anim = GetComponent < Animator>();
}
// Character Controller reference
public CharacterController myController;
// Camera reference
// A transform is a reference to somethings position, rotation, and scale.
public Transform cameraTransform;
// Animator reference
public Animator anim;
// Bool used to tell which direction the player is facing
public bool facingRight = false;
// Player movement Speed
public float speed = 3f;
// Player Jump speed
public float jumpSpeed = 10f;
// Gravity affecting Player
public float gravityStrength = 5f;
// If player is on the ground they are able to jump
public bool canJump = false;
// Create a vertical velocity to use for implementing gravity and jumping.
public float verticalVelocity;
// Player's Vector
public Vector3 myVector;
// Collision Flags to detect if the player is grounded
public CollisionFlags flags;
// Update is called once per frame
void Update()
{
// Run the State Machine and handle the current state.
stateMachine.Update();
// Flip the player's sprite by rotating the x value 180 degrees
if ( (myVector.x < 0 && facingRight) || (myVector.x > 0 && !facingRight) )
{
facingRight = !facingRight;
transform.Rotate(new Vector3(0, 180, 0));
}
//myVector.y = verticalVelocity * Time.deltaTime; // add the new vertical velocity to player to simulate gravity.
// Use Input to move the character
// Move the character Controller and store the reference in a collision flag. Velocity along the y-axis is ignored. Gravity is automatically applied.
Debug.Log(myVector.y);
flags = myController.Move(myVector);
}
}
Idle state script:
public class HIdleState : HPlayerState
{
// Reference Player Controller
PlayerController playerController;
public HIdleState(PlayerController playerController)
{
// Point to the PlayerController passed in.
this.playerController = playerController;
}
public override void Enter()
{
Debug.Log("entering Idle state");
}
public override void Execute()
{
// Debug.Log("updating Idle state");
// Vector3 for the Player.
playerController.myVector = new Vector3(0, 0, 0);
// Get Input from the player
playerController.myVector.x = Input.GetAxisRaw("Horizontal");
playerController.myVector.z = Input.GetAxisRaw("Vertical");
playerController.myVector = Vector3.ClampMagnitude(playerController.myVector, 1f);
// Add speed to the player.
// Multipling something by time.deltatime converts it from moving an amount per frame to an amount per second.
playerController.myVector = playerController.myVector * playerController.speed * Time.deltaTime;
// Animate the player if they are walking and grounded.
if ((Math.Abs(playerController.myVector.x) > 0 && playerController.canJump) || (Math.Abs(playerController.myVector.z) > 0 && playerController.canJump))
{
playerController.anim.SetBool("Walking", true);
}
else
{
playerController.anim.SetBool("Walking", false);
}
// Check the collision on CharacterController.Move to know if the player is grounded.
if ((playerController.flags & CollisionFlags.Below) != 0)
{
// The player is grounded so set jumping bool in the animator to false.
playerController.anim.SetBool("Jumping", false);
playerController.canJump = true;
playerController.verticalVelocity = -3f;
}
else
{
playerController.canJump = false;
}
// Add new speed to old speed for acceleration on the vertical velocity.
playerController.verticalVelocity = playerController.verticalVelocity - (playerController.gravityStrength * Time.deltaTime);
// If the Jump button is pressed change to the Airborne State.
if (Input.GetButtonDown("Jump"))
{
if (playerController.canJump == true)
{
playerController.stateMachine.ChangeState(new HAirborneState(playerController));
}
}
// Set the player's y vector to verticalVelocity to know how much to move.
playerController.myVector.y = playerController.verticalVelocity * Time.deltaTime;
}
public override void Exit()
{
Debug.Log("exiting Idle state");
}
}