- Home /
Jump Script Not Working
I created a simple jump script, but it doesn't seem to work. It worked the first few times and then it just stopped. I have a rigidbody attached to the player.
function Start () {
}
var JumpAmount = 1;
function Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
rigidbody.AddForce (Vector3.up * JumpAmount);
}
}
Do you also have a character controller on him? Those sort of take away your physics.
Any errors appear? How much is jumpamout?
JumpAmount has a value of 1. The only character controller on it is my own. The jump function is a part of it.
function Awake () {
rigidbody.freezeRotation = true;
}
function Start () {
}
var WalkAnimF1 : AnimationClip;
var WalkAnimF2 : AnimationClip;
var WalkAnimDF : AnimationClip;
var $$anonymous$$oveAmount = 0.15;
var JumpAmount = 1;
var FootSteps : AudioClip;
function Update () {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) {
transform.Translate (Vector3.forward * $$anonymous$$oveAmount);
audio.clip = FootSteps;
audio.Play();
animation.clip = WalkAnimF1;
animation.Play();
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) {
transform.Translate (Vector3.left * $$anonymous$$oveAmount);
audio.clip = FootSteps;
audio.Play();
animation.clip = WalkAnimF1;
animation.Play();
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)) {
transform.Translate (Vector3.back * $$anonymous$$oveAmount);
audio.clip = FootSteps;
audio.Play();
animation.clip = WalkAnimF1;
animation.Play();
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
transform.Translate (Vector3.right * $$anonymous$$oveAmount);
audio.clip = FootSteps;
audio.Play();
animation.clip = WalkAnimF1;
animation.Play();
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.J)) {
rigidbody.AddForce (Vector3.up * JumpAmount);
}
}
It's incomplete.
Answer by RetepTrun · Sep 10, 2012 at 02:07 PM
I tried your jump part. You just need to make the amount like 500 for it to be noticable, or more if the scale of your things isnt 1cube=1meter.
Answer by Coreyf716 · Sep 26, 2012 at 10:40 PM
Space can be pressed continually allowing the player to jump too high. How can I disable the space bar for a limited time before allowing for it to be pressed again?
Answer by rhys_vdw · Sep 26, 2012 at 11:53 PM
When you use Rigidbody.AddForce(), you are adding an instantaneous force. ie. It's like turning a rocket booster on for an instant. Jumping is generally achieved by manually adjusting the velocity.
Also, forces only work on non-kinematic Rigidbodies. In most games the player is a kinematic rigidbody. This means that the player will not respond to forces (be pushed around by other objects).
If you do want your player to respond to forces (usually if it's a car or spaceship which has acceleration), then I've added some code at the bottom here.
Unless you want to be able to jump in the air, you wont need a cooldown on jump. You can check if you're touching the ground instead.
var jumpSpeed : float = 10.0;
@HideInInspector
var isGrounded : boolean = false;
// All rigidbody manipulated should be performed in FixedUpdate
function FixedUpdate () {
if (isGrounded && Input.GetKeyDown(KeyCode.Space)) {
// Set the vertical velocity immediately.
var newVelocity : Vector3 = rigidbody.velocity;
newVelocity.y = jumpSpeed;
rigidbody.velocity = newVelocity;
isGrounded = false;
}
}
function OnCollisionStay (c : Collision) {
// Check every part of the collider that is touching something.
for (var cp : ContactPoint in c.contacts) {
// If it's roughly the bottom of the collider, we're touching the ground!
if (cp.point.y < collider.bounds.min.y + 0.05) {
isGrounded = true;
break;
}
}
}
Note that if you use a CharacterController instead of a Rigidbody you can just use CharacterController.isGrounded, which is probably implemented better than the shitty hack I've just provided.
Oh and the code is untested.
EDIT: Looking at your code, you should definitely be using a kinematic rigidbody or a CharacterController, which means you cannot use AddForce. You are not meant to modify transform.position with a non-kinematic rigidbody, it will not detect collisions properly.
I recommend removing your collider and rigidbody and changing to a CharacterController. Use its SimpleMove method instead of changing transform.position. It'll save you a lot of headaches.
@rhys_vdw I'll be using a character controller. I took a look at the simple move method, but it seems much more complicated than transform.translate. Is it a good idea to use the transform.translate method?
I'm sure that I don't want to use a kinematic rigidbody. Gravity is essential for my project.
Character controllers don't have gravity. How would I give the player gravity?
Actually Simple$$anonymous$$ove is the same as Transform.Translate, but it applies gravity.
So you just say:
controller.Simple$$anonymous$$ove(velocity);
However the documentation says "Velocity along the y-axis is ignored." So you may wish to use "$$anonymous$$ove" ins$$anonymous$$d and apply your own gravity.
Gravity is an accelerating force, so you can do something like this:
void Update() { velocity += Physics.gravity Time.deltaTime; controller.Simple$$anonymous$$ove(velocity Time.deltaTime); }
In both of these you'll have to deter$$anonymous$$e velocity, but it should be the same argument that you'd give to Transform.Translate.
Here is a script I made for a platformer. Take a look at its Act() method. (It's part of a larger project so it wont work on its own.)
https://dl.dropbox.com/u/19971570/code/Player.cs
EDIT: fixed error in Simple$$anonymous$$ove
Hey, I made a mistake in my last comment. For Simple$$anonymous$$ove you supply velocity as the argument. For $$anonymous$$ove you supply the actual translation.
Distance = speed * time, so:
controller.Simple$$anonymous$$ove(velocity);
controller.$$anonymous$$ove(velocity * Time.deltaTime);
So, if in the example you gave me, you used a velocity against a rigidbody to jump. How would I jump with a character controller?
Have a look at the example I linked to. CharacterController has an "isGrounded" check. You just keep track of the velocity you're applying every frame, and set its y value (it's vertical component) to a specific speed.
Answer by RetepTrun · Sep 27, 2012 at 12:16 AM
This is a rocket launcher from a unity tutorial.
It has a limit on how fast you can shoot.
Find and copy the part you need. >:( why forums always have to mess with the formatting?
var projectile : Rigidbody;
var initialSpeed = 20.0;
var reloadTime = 0.5;
var ammoCount = 20;
private var lastShot = -10.0;
function Fire () {
// Did the time exceed the reload time?
if (Time.time > reloadTime + lastShot && ammoCount > 0) {
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile:Rigidbody=Instantiate(projectile,transform.position,transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0,0,initialSpeed));
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
ammoCount--;
}
}
Your answer
Follow this Question
Related Questions
Anti-Gravity vehicle bounce-back help. 1 Answer
Rotating Rigidbody properly using AddRelativeForce 0 Answers
Player Falls Over 1 Answer
transform.LookAt overrides rigidbody rotation 0 Answers
Rectangular Cube is sinking in floor rotating at 90 degree 0 Answers