Im new to unity and im trying to make a 2d game that requires my object to move towards the right and jump Unfortunately, im unable to make the object move and jump at the same time. Can someone help?
Heres the script im using. It appears to be that if the object is moving towards the right, the object is unable to jump. But if the object is able to jump, it is unable to move towards the right. Is my script over replacing the jump script?
Script #C
using UnityEngine;
using System.Collections;
public class Frog : MonoBehaviour
{
public Vector3 userDirection = Vector3.right;
float maxJumpHeight = 4.0f;
float groundHeight;
Vector3 groundPos;
float jumpSpeed = 15.0f;
float fallSpeed = 10.0f;
public bool inputJump = false;
public bool grounded = true;
public static int movespeed = 1;
void Start()
{
groundPos = transform.position;
groundHeight = transform.position.y;
maxJumpHeight = transform.position.y + maxJumpHeight;
}
void Update()
{
transform.Translate(userDirection * movespeed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space)) Physics.Raycast(transform.position, -transform.up, 1);
{
if (grounded)
{
groundPos = transform.position;
inputJump = true;
StartCoroutine("Jump");
}
}
if (transform.position == groundPos)
grounded = true;
else
grounded = false;
}
IEnumerator Jump()
{
while (true)
{
if (transform.position.y >= maxJumpHeight)
inputJump = false;
if (inputJump)
transform.Translate(Vector3.up * jumpSpeed * Time.smoothDeltaTime);
else if (!inputJump)
{
transform.position = Vector3.Lerp(transform.position, groundPos, fallSpeed * Time.smoothDeltaTime);
if (transform.position == groundPos)
StopAllCoroutines();
}
yield return new WaitForEndOfFrame();
}
}
}