Question by
Unity-Artcraft · Apr 27, 2019 at 03:53 PM ·
jumpjumpingbuttons
[Solved, kind of] Assign Multiple Keys for jumping?
EDIT : I solved it, changed:
myRigidbody.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
into:
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
and it works now... something is messing with addforce... maybe my poor ground collision detection?
I have this little jump code here:
if ((Input.GetKeyDown(KeyCode.Joystick1Button0) ||
Input.GetKeyDown(KeyCode.Space) ||
Input.GetKeyDown(KeyCode.UpArrow)) && isGround )
{
myRigidbody.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
But if I press multiple keys at once, Space and Up, I jump three times higher :O ... So, what did I do wrong? (Besides everything)
Tried with GetButtonDown but same result... changed by circular-ground-collision check into a box but that didn't helped either...
And here the entire code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControllerInput : MonoBehaviour
{
Rigidbody2D myRigidbody;
[Header("Float Variables")]
[SerializeField] float speed;
[SerializeField] float jumpForce;
[SerializeField] [Range (0.0f, 1.0f)] float groundCheckRadiusX;
[SerializeField] [Range (0.0f, 1.0f)] float groundCheckRadiusY;
[Header("Air Control?")]
[SerializeField] bool airControl;
[Header("Jump Key")]
public KeyCode jumpButton;
[Header("Ignore This!")]
[SerializeField] Transform groundCheck;
[SerializeField] LayerMask whatIsGround;
Quaternion startRotation;
Quaternion currentRotation;
bool isGround;
private void Start()
{
startRotation = transform.rotation;
myRigidbody = GetComponent<Rigidbody2D>();
}
private void Update()
{
currentRotation = transform.rotation;
if (Input.GetButtonDown("Jump") && isGround )
{
myRigidbody.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
isGround = false;
}
if (isGround || airControl)
{
myRigidbody.velocity =
new Vector2(Input.GetAxisRaw("Horizontal") * ( speed * Time.deltaTime * 100),
myRigidbody.velocity.y);
}
if ((currentRotation != startRotation))
{
transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
}
}
private void FixedUpdate()
{
isGround = Physics2D.OverlapBox(groundCheck.position,
new Vector2(groundCheckRadiusX, groundCheckRadiusY), 0, whatIsGround);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireCube(groundCheck.position,
new Vector3(groundCheckRadiusX, groundCheckRadiusY, 0));
}
}
Comment
Your answer
