- Home /
The question is answered, right answer was accepted
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
Answer by superventure · Dec 21, 2010 at 02:15 AM
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);
Answer by fireDude67 · Dec 21, 2010 at 02:48 AM
To do it with the physics system:
Rigidbody
component (Component > Physics > Rigidbody)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)
Answer by NumbedShot · Jan 10, 2011 at 04:16 PM
For the T_T Tutorial the space bar is going to be used to shoot fireballs, keep that in mind.
Answer by ant123 · Jun 18, 2011 at 01:33 PM
I tried both these scripts just now, first one didnt jump, second one didnt compile!
will have a nap and then see why!
Answer by naruto · Jun 19, 2011 at 03:47 PM
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)
Follow this Question
Related Questions
Natural Jump algorithm 2 Answers
How to get global up vector? 1 Answer
Player Controls 0 Answers
"Hover" object up and down 2 Answers