- Home /
How to make jump something without using a character contoller?
Hi, I want to do a script(preference with javascript) to make something like jumping using the rigidbody and the collider but without using the character controller and like that make a script to jump like it can jumps when you put the character contoller script:
var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0;
private var moveDirection = Vector3.zero; private var grounded : boolean = false;
function FixedUpdate() { if (grounded) { // We are grounded, so recalculate movedirection directly from axes moveDirection = new 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 var controller : CharacterController = GetComponent(CharacterController); var flags = controller.Move(moveDirection * Time.deltaTime); grounded = (flags & CollisionFlags.CollidedBelow) != 0; }
@script RequireComponent(CharacterController)
So please could some one tell me how to converte this script to another but using only rigidbody and collider.
Answer by Sriram · Dec 06, 2010 at 07:23 AM
I use something like this in C#, should be easy to convert.
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour { #region Public Variables public float JumpSpeed = 100.0f;
private bool isGrounded = false;
void Start() { animation.wrapMode = WrapMode.Default; }
void Update() {
if (isGrounded && Input.GetButton(m_JumpButton))
Jump();
}
void OnCollisionStay() { animation.Play("idle"); isGrounded = true;
}
void Jump() {
isGrounded = false;
animation.Play("jump_pose");
rigidbody.AddForce(Vector3.up *JumpSpeed);
}
}
I will like more in java script but it dosent matter, really thanks for the answer.
Answer by teonicel · Nov 05, 2012 at 12:48 PM
//C# example
private float JUMP_FORCE = 10.0f; private bool isTouchingMap;
void Update () { if(Input.GetKeyUp(KeyCode.Space)){ jump(); } }
public void jump(){ //rigidbody.AddForce(Vector3.up JUMP_FORCE); if(isTouchingMap){ rigidbody.velocity += Vector3.up JUMP_FORCE; } }