This is frustrating
Dont understand what im doing wrong, I want my object to just simply jump whiles its moving towards the right by presing the spacebar and i want it so that the object jumps over other obstacles coming towards it. I want the player to use the spacebar to make the object jump over the obstacle basically. And i want the jump to be able to jump again. Not just one jump and thats it.
Right now i have this script and the objects moves towards the right yes, but when i press the spacebar for it to jump, it just falls down and off the screen and through my ground. I dont know what happened? is it a problem with gravity.. or does something have to be appliedd...? like a layer or some type of component idk? is it something to do with gravity... dont understand im new and learning i just want to be able to understand whats going on.
Script:
using UnityEngine; using System.Collections; using System;
public class Frog : MonoBehaviour { private Vector3 movement; private float ground; private float speed = 10f; private float jump_power = 15f; 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
}
}
i want the player to jump up and drop back down on the ground. not jump and fall through the screen n off the game wtf
Please post answers as answers and comments as comments. I converted this answer to a comment for you this time...
Answer by vanessa101 · Jun 16, 2016 at 07:52 AM
okay so what are you saying i should add? I added the fixed update and your code but nothing happened. the object still dropped when i press spacebar to make it jump. seems like it just moves to the right but once the spacebar is pressed , thats the end it just falls off screen
This is not a good ANSWER for this question @vanessa101.
Please post comments as comments under whatever you want to comment on.
$$anonymous$$y answer has a suggestion for the condition you might want to add. You can't trust that the object's y-position will hit exactly y==0 when it comes back down so you need to adjust the position if it goes under ground.
Answer by NoseKills · Jun 16, 2016 at 06:07 AM
if (transform.position.y == ground)
movement.y = (Input.GetButton("Jump") ? jump_power : 0f); // jump
else
movement.y -= gravity; // gravity
The only thing keeping the object from sinking is that you set movement.y to 0 if the object is at a right y-coordinate and 'jump' is not pressed.
You apply the movement to the object using Time.deltaTime which depends on your framerate.
So lets say you press jump and movement.y becomes 15 (jump_power). Then transform.Translate(movement * Time.deltaTime);
changes the y position of the object to let's say 0.94 (depends on deltatime). In the next Update()s the object drops by Time.deltaTime * gravity
, let's say first it drops by 0.31 then by 0.33 then by 0.31. Now the y position of the object is -0.01 and there isn't anything to stop it from falling.
Somewhere you need to add a condition like if (transform.position.y < 0) transform.position = new Vector3(transform position.x, 0, transform position.z
or make some other way to keep the object above ground.
In theory your code could work by using FixedUpdate instead of Update because that would make Time.deltaTime constant and because your jump_power is exactly 3*gravity, the object should mathematically land on y==0 height in some Update(). Unfortunately you can't trust floating point numbers to be exactly what you expect. Y could still become -0.0000001 and break the system.
Your answer
Follow this Question
Related Questions
Help with jumping script 2 Answers
Need help with jumping on third person movement script. 0 Answers
Cant Move towards Right and Jump at same time? 2 Answers
I wrote a jumping script, but it isn't working and i'm not even getting any error codes. pls help. 0 Answers
Help: Use a Trigger object to slow Player while inside object 0 Answers