Cant Move towards Right and Jump at same time?
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();
}
}
}
Answer by EpiFouloux · Jun 14, 2016 at 12:29 PM
Does this even compile?
You have on line 25 a Physics.Raycast()
just after the if statement doing nothing ! And I believe your doing it in a very complicated manner, try something more like that : (I don't garanty that it's perfect, juste wroted it from scratch)
public class player_movement : MonoBehaviour
{
private Vector3 movement;
private float ground;
private float speed = 15f;
private float jump_power = 20f;
public float gravity = 5f;
void Start()
{
ground = transform.position.y;
}
void Update()
{
movement.x = speed; // moving to the right
if (transform.position.y == ground)
movement.y = (Input.GetButton("Jump") ? jump_power : 0f); // jump
else
movement.y -= gravity; // gravity
transform.Translate(movement * Time.deltaTime); // applies movement
}
}
EDIT : changed the jmping part, does the movement work?
Answer by vanessa101 · Jun 14, 2016 at 05:31 PM
Hmm, it still didnt make the object move nor jump
Where do i place the jump and the move towards right script in this section?
well the movement.x = speed
is the moving part.. and the movement.y
is the jump part, I just editted the answer to make it more understandable
Alright thanks i got the script to work the movement part worked but when i hit spacebar to jump it just falls down off the screen and through my ground. Does my ground object need something applied to it? or doesthe object need something
your a genius, i love yu!. i just need that problem fixed above tho
Your answer
Follow this Question
Related Questions
Help with jumping script 2 Answers
Need help with jumping on third person movement script. 0 Answers
I wrote a jumping script, but it isn't working and i'm not even getting any error codes. pls help. 0 Answers
This is frustrating 2 Answers
Help: Use a Trigger object to slow Player while inside object 0 Answers