Hi everyone
I am fairly new to this, and can’t seem to find the ‘standard assets’ version of the answer.
Currently, my character is bouncing a second time if they land and I pressed the space bar while they were midair. I am trying to alter the firstpersoncontroller.cs to fix this. Could anyone please point me in the right direction?
Ideally, I would only want the character to respond to spacebar if they have already landed on the ground. I thought if (m_CharacterController.isGrounded && !m_Jumping) would fix this.
Here is the main code that seems to contribute below (not mine, from standard unity assets) - scroll down to ‘Where I’ve gone wrong’ to see which part I’ve experimented with…
Note, I’ve also changed it so private bool m_PreviouslyGrounded = true; This prevents the initial landing sound on character start up.
private Camera m_Camera;
private bool m_Jump;
private float m_YRotation;
private Vector2 m_Input;
private Vector3 m_MoveDir = Vector3.zero;
private CharacterController m_CharacterController;
private CollisionFlags m_CollisionFlags;
private bool m_PreviouslyGrounded = true;
private Vector3 m_OriginalCameraPosition;
private float m_StepCycle;
private float m_NextStep;
private bool m_Jumping;
private AudioSource m_AudioSource;
// Use this for initialization
private void Start()
{
m_CharacterController = GetComponent<CharacterController>();
m_Camera = Camera.main;
m_OriginalCameraPosition = m_Camera.transform.localPosition;
m_FovKick.Setup(m_Camera);
m_HeadBob.Setup(m_Camera, m_StepInterval);
m_StepCycle = 0f;
m_NextStep = m_StepCycle/2f;
m_Jumping = false;
m_AudioSource = GetComponent<AudioSource>();
m_MouseLook.Init(transform , m_Camera.transform);
}
// Update is called once per frame
private void Update()
{
RotateView();
// the jump state needs to read here to make sure it is not missed
if (!m_Jump)
{
m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
}
if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
{
StartCoroutine(m_JumpBob.DoBobCycle());
PlayLandingSound();
m_MoveDir.y = 0f;
m_Jumping = false;
}
if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
{
m_MoveDir.y = 0f;
}
m_PreviouslyGrounded = m_CharacterController.isGrounded;
}
Where I’ve gone wrong
I’ve tried to say ‘if the player is not jumping’ as a condition, under the assumption that this cannot be true when the character is in the air, but this doesn’t seem to work sadly.
if (m_CharacterController.isGrounded && !m_Jumping)
{
m_MoveDir.y = -m_StickToGroundForce;
if (m_Jump)
{
m_MoveDir.y = m_JumpSpeed;
PlayJumpSound();
m_Jump = false;
m_Jumping = true;
}
}