Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Coreyf716 · Sep 09, 2012 at 10:07 PM · javascriptrigidbodytransform

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);
 
 }
 
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RetepTrun · Sep 10, 2012 at 01:40 AM 0
Share

Do you also have a character controller on him? Those sort of take away your physics.

Any errors appear? How much is jumpamout?

avatar image Coreyf716 · Sep 10, 2012 at 01:56 AM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Coreyf716 · Sep 30, 2012 at 07:56 PM 0
Share

@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?

avatar image rhys_vdw · Oct 01, 2012 at 12:35 AM 0
Share

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

avatar image rhys_vdw · Oct 01, 2012 at 12:50 AM 0
Share

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);

avatar image Coreyf716 · Oct 01, 2012 at 12:52 AM 0
Share

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?

avatar image rhys_vdw · Oct 01, 2012 at 01:02 AM 0
Share

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.

Show more comments
avatar image
0

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--;
                 }
             }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges