When i jump my marble disappears, and a lot of errors pop up. Really need help.
First - Add this script to a sphere.
Second - Change its mass to 0.1
Third - Play the game and press Space Bar.
var speed = 300.0;
var gravity = -5;
var canJump = true;
var jumpHeight = 2.0;
private var grounded = false;
function FixedUpdate() {
rigidbody.AddForce (Vector3(0, gravity, 0));
var cameraTransform = Camera.main.transform;
// Forward vector relative to the camera along the x-z plane
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
if (Input.GetKey ("w")) {
rigidbody.AddForce(forward * speed * Time.deltaTime);
}
if (Input.GetKey("s")) {
rigidbody.AddForce(forward * -speed * Time.deltaTime);
}
if (Input.GetKey ("d")) {
rigidbody.AddForce(right * speed * Time.deltaTime);
}
if (Input.GetKey ("a")) {
rigidbody.AddForce(right * -speed * Time.deltaTime);
}
if (grounded)
{
var velocity = rigidbody.velocity;
if (canJump Input.GetButton("Jump"))
{
rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
grounded = false;
}
function OnCollisionStay ()
{
grounded = true;
}
function CalculateJumpVerticalSpeed ()
{
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
So i changed AddForce to AddTorque for more of a realistic look but now it moves really slow. No matter what the speed is (1 or 1000) it still moves the same speed. Whats wrong ?
(I still have the jumping problem)
var speed = 300.0;
var gravity = -5;
var canJump = true;
var jumpHeight = 2.0;
private var grounded = false;
function FixedUpdate() {
rigidbody.AddForce (Vector3(0, gravity, 0));
var cameraTransform = Camera.main.transform;
// Forward vector relative to the camera along the x-z plane
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
if (Input.GetKey ("w")) {
rigidbody.AddTorque(right * speed * Time.deltaTime);
}
if (Input.GetKey("s")) {
rigidbody.AddTorque(right * -speed * Time.deltaTime);
}
if (Input.GetKey ("d")) {
rigidbody.AddTorque(forward * -speed * Time.deltaTime);
}
if (Input.GetKey ("a")) {
rigidbody.AddTorque(forward * speed * Time.deltaTime);
}
if (grounded)
{
var velocity = rigidbody.velocity;
if (canJump Input.GetButton("Jump"))
{
rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(Vector3 (0, gravity * rigidbody.mass, 0));
grounded = false;
}
function OnCollisionStay ()
{
grounded = true;
}
function CalculateJumpVerticalSpeed ()
{
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
[quote][/quote]
Ok, so i got the jumping problem sorted out but i still cant figure our how to get the AddTorque to rotate faster. Its making me MAD :x !!!
var speed = 300.0;
var gravity = -5;
var canJump = true;
var jumpHeight = 2.0;
private var grounded = false;
function FixedUpdate() {
rigidbody.AddForce (Vector3(0, gravity, 0));
var cameraTransform = Camera.main.transform;
// Forward vector relative to the camera along the x-z plane
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
if (Input.GetKey ("w")) {
rigidbody.AddTorque(right * speed * Time.deltaTime);
}
if (Input.GetKey("s")) {
rigidbody.AddTorque(right * -speed * Time.deltaTime);
}
if (Input.GetKey ("d")) {
rigidbody.AddTorque(forward * -speed * Time.deltaTime);
}
if (Input.GetKey ("a")) {
rigidbody.AddTorque(forward * speed * Time.deltaTime);
}
if (grounded)
{
var velocity = rigidbody.velocity;
if (canJump Input.GetButton("Jump"))
{
rigidbody.velocity = Vector3(velocity.x, jumpHeight , velocity.z);
}
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(Vector3 (0, gravity * rigidbody.mass, 0));
grounded = false;
}
function OnCollisionStay ()
{
grounded = true;
}