I'm creating a basic game that involves the character moving left to right and jumping.
I have this code.
void FixedUpdate()
{
//get input from up/down keys
float moveVert = Input.GetAxis("Vertical");
float moveHoriz = Input.GetAxis("Horizontal");
Vector3 movement = new Vector3 (moveHoriz, 0, moveVert);
//if(Input.GetKeyDown(KeyCode.RightArrow))
{
rigidbody.AddForce(movement * speed * Time.deltaTime);
}
if(Input.GetKeyDown(KeyCode.UpArrow) && !isFalling)
{
Jump(movement);
}
}
void Jump(Vector3 movement)
{
movement.y = jumpHeight;
movement.z = 0;
rigidbody.AddForce(jumpHeight * transform.forward + new Vector3(0,jumpHeight,0), ForceMode.Impulse);
isFalling = true;
}
There's several issues. The first of which is my character in Jump() doesn't jump up on the Y axis but back (away from the camera) on the Z axis.
I want my character to jump and be able to move left/right at the same time. At first I thought my first AddForce was messing with my Jump() AddForce so I tried to add some conditionals to check if it was the left/right arrow but those didn't work like I wanted. So I'm not sure if fixing the Jump() will fix this issue or not. Should I need to check for left/right arrows, or does rigidbody.AddForce(movement * speed * Time.deltaTime); only control the x axis movement? I tried something like rigidbody.AddForce(-Vector3.right * speed * Time.deltaTime); and my character moved right away without arrows.
↧