- Home /
Faux Gravity Jumping
I'm still very new to unity, I have only been working with it for about two and a half months, I followed a short tutorial on creating Faux Gravity, almost everything works out perfectly, except jumping. I know that I need to use ray-casting to create the jump script, but I have no idea of how to do that, any help would be very appreciated.
Here is the tutorial, jumping wasn't something that was covered in it though: https://www.youtube.com/watch?v=gHeQ8Hr92P4
Hey bud, you might attach something like this...
public float jumpHeight = 2.0f;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space)) { transform.position = new Vector3(transform.position.x, jumpHeight, transform.position.z); }
into your player gameobject`s, controller/controlling code.
when you hit the space bar, your player , on the Y axis(globally upwards) will change to the value specified in the public float jumpHeight.
Alongside the above... If, for example, your player has a character controller on it or a rigidbody and a collider, you could take advantage of that as it will always try to fall downward if nothing touching its base Anyways, hope that helps some Take care gruffy
Thanks a lot for all the help guys, and sorry I wasn't really able to do this myself, I'm still really new to all of this. :)
Answer by unity_6ZMJlhFpaWH3QA · Jul 02, 2019 at 12:24 AM
public class PlayerMovement : MonoBehaviour { //Scripts public PlanetScript PlanetScp;
//RigidyBody 2D
Rigidbody2D rb;
//bools
bool canMove = true;
// floats
public float MoveSpeed = 10f;
public float JumpSpeed = 10f;
//ints
public int NumberOfJumps = 2;
public int NumberOfJumpsDefault = 2;
public float MaxTimeInAir = 2;
void Start()
{
rb = GetComponent<Rigidbody2D>();
NumberOfJumps = NumberOfJumpsDefault;
}
void Update()
{
if (canMove)
{
if (Input.GetKey("d")) { Run(1); }
if (Input.GetKey("a")) { Run(-1); }
if (Input.GetKeyDown("space") && NumberOfJumps > 0)
{
Jump();
NumberOfJumps--;
}
if (rb.velocity.y == 0)
{
NumberOfJumps = NumberOfJumpsDefault;
}
}
}
void Run(int directionOnX)
{
Vector2 horizontal = new Vector2(directionOnX, rb.velocity.y).normalized;
rb.MovePosition(rb.transform.position + rb.transform.TransformDirection((horizontal) * MoveSpeed * Time.deltaTime));
}
void Jump()
{
Vector2 vertical = new Vector2(rb.velocity.x, JumpSpeed).normalized;
rb.velocity = rb.transform.position + rb.transform.TransformDirection((vertical) * MoveSpeed * Time.deltaTime);
rb.velocity = rb.transform.position + rb.transform.TransformDirection((-vertical) * MoveSpeed * Time.deltaTime);
//but theres a problem, if the player walks and jumps at the same time, he wont come back to the planet, give a try and tell me if u have any idea of how to fix that (:
Your answer
Follow this Question
Related Questions
How to run Unity3D on Ubuntu 6 Answers
need help with this script 1 Answer
Any easier way to code this? (Javascript) 0 Answers
I open my project and I found that everything's gone?! 2 Answers
Android Main Menu Help 1 Answer