Im making a game and I need a jump script. Its a 2d cube platformer and this is the script.
using UnityEngine;
using System.Collections;
public class PlayerCharacterController : MonoBehaviour {
public float movespeed = 3.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * movespeed);
}
}
I need to make the character jump and i dont know how. Im new so try to make it simple please. Thanks for your time.
This is from the Scripting reference (code in UnityScript):
/// This script moves the character controller forward
/// and sideways based on the arrow keys.
/// It also jumps when pressing space.
/// Make sure to attach a character controller to the same game object.
/// It is recommended that you make only one call to Move or SimpleMove per frame.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
Asking for scripts can sometimes get you a script, but just as often it gets a thumbs down and/or your question is ignored. As for jumping, you have three choices.
You can add a Rigidbody component to your character and then use AddForce() to make him jump up. He will come down because the physics engine will apply gravity.
You can use a character controller with Move() or SimpleMove(). One answer on how to make a character controller jump is here.
You can simulate gravity and a jump moving the transform (like you are moving horizontally in your code.) You can use the same logic on how to apply the jump that is used in the answer for the character controller.
Look at the script you have: it uses a simple translation to move the player along (I take it you have followed some basic maths course so you should know what translations are).
The property that defines the direction of movement is called right (Vector3.right), so maybe there is a similar property to move the player upwards?