- Home /
How to jump to platform by clicking on it
right now, I have my game set up intended for mobile. My character is the character controller. There is no gravity and no movement.
Character jumps to a platform when space bar is pressed and character automatically jumps at a fixed rate going from platform to platform. Platforms can't be skipped and players must jump through sequentially.
Instead of space bar, I want to figure out how i can click a specific platform and have the character go there to the platform I click. I want the platform that the player is supposed to jump on to be glowing and stop glowing afterwards.
If anyone can help me i would appreciate it :)
screenshot:
Test build here: https://www.dropbox.com/s/3qy39soj6lsdytc/Web.html?dl=0
This is what I have now for code:
public class Jump : MonoBehaviour
{
CharacterController motor;
public float maxJumpPower = 50; //Initial velocity for jump
public float jumpPower = 0; // Current velocity for jump
public float gravity = 0.75f; // Jump velocity reduced each frame
public float maxFallSpeed = -20f; // Maximum velocity for fall- Limited to prevent falling through platforms
bool isJumping = false; // Is the character jumping? Only move while jumping
bool movingRight; // The character will move to the right or to the left only
public float maxRunSpeed = 5; // The character will have its speed set to this if it is moving right, or this * -1 to move left
float runSpeed = 10f; // Current speed
void Start()
{
// Get the CharacterController attached to the gameObject that this script is attached to
motor = gameObject.GetComponent<CharacterController>();
}
void Update()
{
// if the character is on the ground, he must not be jumping
if (motor.isGrounded)
{
isJumping = false;
}
// if the character isn't jumping and they press jump, start them at full speed
if (Input.GetKeyDown(KeyCode.Space) && isJumping == false)
{
isJumping = true;
movingRight = !movingRight;
jumpPower = maxJumpPower;
}
// reduce jump velocity by gravity every frame
jumpPower -= gravity;
if (jumpPower < maxFallSpeed)
{
jumpPower = maxFallSpeed;
}
// Set the proper speed for moving right or left
if (movingRight)
{
runSpeed = maxRunSpeed;
}
else
{
runSpeed = -maxRunSpeed;
}
// If the character is jumping, move it. X is horizontal, and Y is vertical
if (isJumping == true)
{
Vector3 moveVector;
moveVector = new Vector3(runSpeed * Time.deltaTime, jumpPower * Time.deltaTime, 0);
motor.Move(moveVector);
}
}
}
Answer by Veldars · Apr 09, 2015 at 06:08 AM
For me the shorter way to do that is firstly to create a script implementing OnMouseDown function. After that you can attach this script to each platform and easily call a function from Your game controller when a platform is clicked.
public class PlatformController: MonoBehaviour
{
// The jump script
private Jump _jump;
void Start() {
// Find in the scene the jump script.
_jump = GameObject.FindGameObjectWithTag("JumpEmptyGameObject").GetComponent< Jump >();
}
void OnMouseDown () {
// call your function witch will make glowing the platform and make jump...
_jump.jumpFunction(this);
}
}
I hope this help...
Your own built function ^^. In your Jump class you can create a public function (jumpFunction) and send it the selected platform (this) ... After that I think you already have done most of the work.
how do i make it jump the specific amount of distance? in my code, i have an x and y force i can control and tweak but i can't turn it into a seperate function because the character will freeze in the air and i will have to hit jump button every frame.
private Vector3 _platformDistance;
public void jumpFunction(GameObject platform) {
isJumping = true;
_platformDistance = platform.transform.position - motor.transform.position; // I assum that motor is your player...
}
and you have your X and Y (_platformDistance.x && _platformDistance.y)
Your answer
Follow this Question
Related Questions
auto jumping to a platform 0 Answers
how to flip a sprite 0 Answers
2D Sidescroller enemy AI jump help! Picture Included! 3 Answers
Platformer cat not jumping and is flinging across the map when hitting a corner 2 Answers
Input Axis Vertical is not set up? 1 Answer