Add the basic movements and jump for the child

This commit is contained in:
Patrice Vignola 2015-08-22 02:23:39 -04:00
parent 69699e5f73
commit 606d55999c
6 changed files with 52 additions and 6 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -5,27 +5,62 @@ using System.Collections;
public class Child : MonoBehaviour
{
public float Speed = 10f;
public float JumpForce = 10f;
public GameObject GroundCheck;
private Rigidbody _rb;
private bool _isGrounded = false;
private float _xValue;
private float _zValue;
void Awake()
{
_rb = GetComponent<Rigidbody>();
}
public void Move(float xValue, float zValue)
void Update()
{
_isGrounded = IsGrounded();
Debug.Log(_isGrounded);
}
void FixedUpdate()
{
// We move the child depending on the camera orientation
Vector3 forwardDir = Camera.main.transform.forward;
Vector3 rightDir = Camera.main.transform.right;
forwardDir.y = 0f;
forwardDir *= zValue * Speed;
forwardDir *= _zValue * Speed;
forwardDir.y = _rb.velocity.y;
rightDir *= _xValue * Speed;
rightDir.y = 0f;
rightDir *= xValue * Speed;
_rb.velocity = forwardDir + rightDir;
}
private bool IsGrounded()
{
Collider[] colliders = Physics.OverlapSphere(GroundCheck.transform.position, 0.5f, 1 << LayerMask.NameToLayer("Ground"));
return colliders.Length > 0;
}
public void Move(float xValue, float zValue)
{
_xValue = xValue;
_zValue = zValue;
}
public void Jump()
{
if (_isGrounded)
{
_isGrounded = false;
_rb.AddForce(new Vector3(0f, JumpForce, 0f));
}
}
}

View File

@ -14,12 +14,13 @@ public class ChildController : MonoBehaviour
void Awake()
{
InputManager.Instance.PushActiveContext("Gameplay", (int)PlayerNumber);
InputManager.Instance.AddCallback((int)PlayerNumber, HandlePlayerInput);
InputManager.Instance.AddCallback((int)PlayerNumber, HandlePlayerAxis);
InputManager.Instance.AddCallback((int)PlayerNumber, HandlePlayerButtons);
_child = GetComponent<Child>();
}
private void HandlePlayerInput(MappedInput input)
private void HandlePlayerAxis(MappedInput input)
{
if (this == null) return;
@ -48,4 +49,14 @@ public class ChildController : MonoBehaviour
_child.Move(xValue, zValue);
}
private void HandlePlayerButtons(MappedInput input)
{
if (this == null) return;
if (input.Actions.Contains("Jump"))
{
_child.Jump();
}
}
}

Binary file not shown.