I am making a 2d sidescroller where u jump multiple times to get to another side of lava pits and such.
The problem I have is that when the character jumps it teleports up and doesn’t smoothly increase in altitude. I think this is due to the transform.Translate part of the code but I just don’t how to to imply the AddForce or the velocity.
Another problem I have is that the gravity increases over time which makes it fall faster and faster which I do not want. I want it so every time you jump the gravity resets so it looks like a natural multi jump.
I am using this script with the Character Controller, Character Motor, and the FPSInputController
Heres the code :
function Start () {
}
var moveSpeed : float = 0.15;
var jumpSpeed : float = 4;
function Update(){
if (Input.GetKey("d"))
transform.Translate(-moveSpeed,0,0);
if (Input.GetKey("a"))
transform.Translate(moveSpeed,0,0);
if (Input.GetKeyDown("space"))
transform.Translate(0,jumpSpeed,0);
}
What you want will probably require storing a vertical velocity variable.
Your problem is more on the physics side than the scripting side. Gravity should remain constant, but that means a constant acceleration, which is a constant change in velocity. And so then when you jump you would set the velocity to jumpSpeed and when you land, set it to 0.
Platformers can be trickier than you might expect. You might do better to practice on something more akin to Asteroids or Galaga until you’re comfortable with the physics of it. Or like Flappy Bird, because the velocity and gravity is there but you don’t need to handle your bird landing. Or you could retool the game to rely upon the rigidbody physics (which has its own velocity built in) and then all your controls will use .AddForce() instead of .Translate()
Flappy Birds is exactly what I am aiming for actually. I just can’t get it to have a smooth increase in altitude. I have tried using rigidbody.Addforce with the forcemode.impulse. The main problem I have right now is that using the physics like rigidbodys nothing happens. Sometimes it makes the camera go up and then start jumping around on the way coming back down but the player stays stationary. I can post a video or my other codes if needed.
Thank you very much. I just took away the three that you asked if they were necessary and put on some of the code that guy mentioned and put some of my own for controlling it left and right. The only thing now is that the player doesn’t always respond to the jump. Every now and then it will not pick up the spacebar being clicked and sometimes it responds perfectly.
function Start () {
}
var moveSpeed : float = 0.05;
var jumpSpeed : float = 4;
function Update(){
if (Input.GetKey("d"))
transform.Translate(moveSpeed,0,0);
if (Input.GetKey("a"))
transform.Translate(-moveSpeed,0,0);
}
function FixedUpdate(){
if (Input.GetKeyDown("space"))
rigidbody.velocity.y = jumpSpeed;
}
There is only one other script on the player which is :
#pragma strict
var speed : float = 4;
function Start () {
}
function FixedUpdate () {
rigidbody.velocity.x = speed;
}
Thank you very much for both of your responses. Softwizz I was going to change that I just hadn’t got to it yet and wasn’t really thinking things through. Also thank you for the script I will add that into my project. Also rrh, for future knowledge what should i use instead of GetKeyDown or should I put it in Update but I have been told not to use rigidbodys in the Update().
The warning against Update applies if you want to do something constantly, rather than just once. I would actually be more suspicious of your first example where you did this in Update:
if (Input.GetKey("d"))
transform.Translate(-moveSpeed,0,0);