Help wanted -- How to make my character jump (using slightly modified Tornado Twins move script)

Hello. Here is my script:

    var speed = 3.0;
var rotateSpeed = 3.0;
var bullitPrefab:Transform;
var bullitspeed = 2000000;
private var dead = false;

function OnControllerColliderHit(hit : ControllerColliderHit)
{
    if(hit.gameObject.tag == "fallout")
    {
        dead = true;
    }
}

function Update ()
    {
        var controller : CharacterController = GetComponent(CharacterController);
        transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
        var forward = transform.TransformDirection(Vector3.forward);
        var curSpeed = speed * Input.GetAxis ("Vertical");
        controller.SimpleMove(forward * curSpeed);

        if(Input.GetButtonDown("Fire1"))
        {
            var bullit = Instantiate(bullitPrefab, GameObject.Find("FireballSpawnPoint").transform.position, Quaternion.identity);

            bullit.rigidbody.AddForce(transform.forward * bullitspeed);
        }
    }

    function LateUpdate()
    {
        if(dead)
        {
            transform.position = Vector3(20, 4, 20);
            gameObject.Find("Main Camera").transform.position = Vector3(0, 4, -10);
            dead = false;
        }
    }

@script RequireComponent(CharacterController)

I need to add a jump function. I tried myslef, but it kept failing. Help would really be appreciated (I want to use space as jump) Thanks!!! :D

5 Answers

5

Set the button you want by going to edit> project settings> input.

after that>>

set a var like : var jumpSpeed = 10;

    if (Input.GetButton ("Jump")) 
    if (controller.isGrounded){
    moveDirection.y= jumpSpeed;

You may also need to set up gravity

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
controller.Move(moveDirection * Time.deltaTime);

Thank you, I will try it soon. :P

–

To do it with the physics system:

  1. Attach a `Rigidbody` component (Component > Physics > Rigidbody)
  2. Add the following code to jump:

    var jumpSpeed = 10;
    
    function Update() { 
        if (Input.GetKeyDown(KeyCode.Space)) {
            rigidbody.AddRelativeForce(0, jumpSpeed, 0);
        }
    }
    
    

Another way to do it would be to use a character controller (Component > Physics > Character Controller)

Thank you, I will try it soon. :P

–

For the T_T Tutorial the space bar is going to be used to shoot fireballs, keep that in mind.

Yes, I changed that to click though.

–

I tried both these scripts just now, first one didnt jump, second one didnt compile!

will have a nap and then see why!

add a var “jumpSpeed” then in your Update function add this

		if(Input.GetButtonDown("Jump")){
		if (controller.isGrounded){
		  moveDirection.y = jumpSpeed;
		 }
	  
	 }
	 
	 // Apply gravity
	 moveDirection.y -= movement.gravity * Time.deltaTime;

	 // Move the controller
	 controller.Move(moveDirection * Time.deltaTime);

so your script should look something like this

var speed = 3.0;
var rotateSpeed = 3.0;
var jumpSpeed : float = 5.0; 
var bullitPrefab:Transform;
var bullitspeed = 2000000;
private var dead = false;

function OnControllerColliderHit(hit : ControllerColliderHit)
{
    if(hit.gameObject.tag == "fallout")
    {
        dead = true;
    }
}

function Update ()
    {
        var controller : CharacterController = GetComponent(CharacterController);
        transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
        var forward = transform.TransformDirection(Vector3.forward);
        var curSpeed = speed * Input.GetAxis ("Vertical");
        controller.SimpleMove(forward * curSpeed);


        if(Input.GetButtonDown("Fire1"))
        {
            var bullit = Instantiate(bullitPrefab, GameObject.Find("FireballSpawnPoint").transform.position, Quaternion.identity);

            bullit.rigidbody.AddForce(transform.forward * bullitspeed);
        }
		
		if(Input.GetButtonDown("Jump")){
			if (controller.isGrounded){
			  moveDirection.y = jumpSpeed;
			 }
		  
		 }
		 
		 // Apply gravity
		 moveDirection.y -= movement.gravity * Time.deltaTime;

		 // Move the controller
		 controller.Move(moveDirection * Time.deltaTime);
		
    }

    function LateUpdate()
    {
        if(dead)
        {
            transform.position = Vector3(20, 4, 20);
            gameObject.Find("Main Camera").transform.position = Vector3(0, 4, -10);
            dead = false;
        }
    }
 
      @script RequireComponent(CharacterController)