Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 UDN_cc9279a9-c439-44f4-bdc7-5c6007f6cca7 · Aug 18, 2016 at 02:21 PM · playercontrollerjumpplatformerplatform

my ball sometimes jumps higher or sometimes not

player = ball

my ball sometimes jumps higher or sometimes not i set the jump height to 12 but it jumps sometimes much much higher than that or sometimes not here are the scripts i am used

using System;

using UnityEngine;

namespace UnityStandardAssets.Vehicles.Ball {

public class Ball : MonoBehaviour {

     [SerializeField] private float m_MovePower = 5; // The force added to the ball to move it.
     [SerializeField] private bool m_UseTorque = true; // Whether or not to use torque to move the ball.
     [SerializeField] private float m_MaxAngularVelocity = 25; // The maximum velocity the ball can rotate at.
     [SerializeField] private float m_JumpPower = 12; // The force added to the ball when it jumps.

     private const float k_GroundRayLength = 1f; // The length of the ray to check if the ball is grounded.
     private Rigidbody m_Rigidbody;


     private void Start()
     {
         m_Rigidbody = GetComponent<Rigidbody>();
         // Set the maximum angular velocity.
         GetComponent<Rigidbody>().maxAngularVelocity = m_MaxAngularVelocity;
     }


     public void Move(Vector3 moveDirection, bool jump)
     {
         // If using torque to rotate the ball...
         if (m_UseTorque)
         {
             // ... add torque around the axis defined by the move direction.
             m_Rigidbody.AddTorque(new Vector3(moveDirection.z, 0, -moveDirection.x)*m_MovePower);
         }
         else
         {
             // Otherwise add force in the move direction.
             m_Rigidbody.AddForce(moveDirection*m_MovePower);
         }

         // If on the ground and jump is pressed...
         if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
         {                               
             m_Rigidbody.AddForce(Vector3.up*m_JumpPower, ForceMode.Impulse);
         }
     }
 }

} and the second script

using System;

using UnityEngine;

namespace UnityStandardAssets.Vehicles.Ball

{

 public class BallUserControl : MonoBehaviour
 {
     private Ball ball; // Reference to the ball controller.

     private Vector3 move;
     // the world-relative desired move direction, calculated from the camForward and user input.

     private Transform cam; // A reference to the main camera in the scenes transform
     private Vector3 camForward; // The current forward direction of the camera
     private bool jump; // whether the jump button is currently pressed


     private void Awake()
     {
         // Set up the reference.
         ball = GetComponent<Ball>();


         // get the transform of the main camera
         if (Camera.main != null)
         {
             cam = Camera.main.transform;
         }
         else
         {
             Debug.LogWarning(
                 "Warning: no main camera found. Ball needs a Camera tagged \"MainCamera\", for camera-relative controls.");
             // we use world-relative controls in this case, which may not be what the user wants, but hey, we warned them!
         }
     }


     private void Update()
     {
         // Get the axis and jump input.

         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         jump = Input.GetButton("Jump");

         // calculate move direction
         if (cam != null)
         {
             // calculate camera relative direction to move:
             camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
             move = (v*camForward + h*cam.right).normalized;
         }
         else
         {
             // we use world-relative directions in the case of no main camera
             move = (v*Vector3.forward + h*Vector3.right).normalized;
         }
     }


     private void FixedUpdate()
     {
         // Call the Move function of the ball controller
         ball.Move(move, jump);
         jump = false;
     }
 }

}

THANKS FOR ANY HELP

Comment
Add comment · Show 3
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 meat5000 ♦ · Aug 18, 2016 at 03:19 PM 0
Share

This is usually the answer :

https://unity3d.com/learn/tutorials/topics/scripting/delta-time

avatar image UDN_cc9279a9-c439-44f4-bdc7-5c6007f6cca7 meat5000 ♦ · Aug 18, 2016 at 03:55 PM 0
Share

THAN$$anonymous$$S FOR YOUR HELP but can you explain more further

avatar image meat5000 ♦ UDN_cc9279a9-c439-44f4-bdc7-5c6007f6cca7 · Aug 18, 2016 at 04:24 PM 0
Share

If movements are not scaled with respect to the varying framerate then velocities and distances can become dependent on the FPS. Its explained in the link, I'm sure.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by _dns_ · Aug 18, 2016 at 02:39 PM

Hi, Update() and FixedUpdate() are not called the same amount of time. What happens is that sometimes 2 Update() are called for only 1 FU (= on high framerates; the inverse can happen on low framerates). Imagine the first U the jump is pressed but the second one it is no more. No FU was called in between so your character won't jump. It's the same for the movement: the first frame the axis may report 1.0 but the second only 0.5 so the "move" vector will only be 0.5 when FU interpret it.

Now, how to fix it depends on what behavior you want: do you want to average the move vector in the case of 2 or more U for 1 FU ? If it's = 0 : set the value according to the axis, if it's not 0, average the value between the existing value and the new one. Set "move" to 0 after you "used" it in FU the same way you do for the "jump" value. For the jump, it's quite simple to have it not set to false in U if it's already to true.

It's the way to do it to have very precise controls = interpret inputs during Update and use those values in FixedUpdate. It's a little complicated though and should also take in account deltaTime in Update to know for how long a button was pressed or an axis moved.

Comment
Add comment · Show 12 · 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 NoseKills · Aug 18, 2016 at 06:17 PM 0
Share

I would be interested to know what actually fixed this problem (if it actually got fixed)...

He's not using deltaTime anywhere in this code and he' s correctly (with a quick glance) using a boolean in Update() to capture the input and toggling it off after applying it in FixedUpdate()

He is however using Input.GetButton() ins$$anonymous$$d of Input.GetButtonDown() which will cause the input to be applied multiple times if the press is longer than 1 Update()...which I believe is the issue here.

avatar image UDN_cc9279a9-c439-44f4-bdc7-5c6007f6cca7 NoseKills · Aug 18, 2016 at 06:28 PM 0
Share

yeah it's my mistake to accept answer but later i checked it's not work for me

avatar image _dns_ NoseKills · Aug 18, 2016 at 07:22 PM 0
Share

The fact that there is no deltatime usage here should not be the problem: he fills a vector with a jump value depending on the joystick axis during update. He then uses this vector during fixedupdate and then clears the vector & boolean (if implementing my suggestion). There is no need for deltatime as the force used is an impulse, not a speed over time.

You are right about the GetButtonDown! didn't see this one. The fact that sometimes it doesn't jump at all, if I understood the question, was not due to this but to the fact that there are multiple Update call between 2 fixedUpdates.

avatar image NoseKills _dns_ · Aug 19, 2016 at 06:09 AM 0
Share

Oh true. It's left a bit unclear whether it sometimes "jumps higher and sometimes not higher" or "sometimes not at all"

The deltaTime bit was meant to comment on meat5000's comment. DeltaTime probably isn't the issue here.

Well all of these are a possibility and should be considered by the OP :)

Show more comments
Show more comments
avatar image meat5000 ♦ · Aug 19, 2016 at 12:32 PM 0
Share

@Nosekills

Indeed T.dT may not be the answer. Its my default "You have to check this first" answer for these kinds of situations.

@UDN_cc Using AddForce to apply a jump will give varying results if a force is already being applied.

 if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
          {                               
               m_Rigidbody.velocity = Vector3(m_Rigidbody.velocity.x, 0.0f, m_Rigidbody.velocity.z);
               m_Rigidbody.AddForce(Vector3.up*m_JumpPower, Force$$anonymous$$ode.Impulse);
          }

Consider the above code where the Y velocity is zeroed before the force is applied. It may help illustrate what Im banging on about.

avatar image UDN_cc9279a9-c439-44f4-bdc7-5c6007f6cca7 meat5000 ♦ · Aug 19, 2016 at 01:44 PM 0
Share

it also not worked it gave errror ''error CS0119: Expression denotes a type', where a variable', value' or method group' was expected" but thanks for help

avatar image meat5000 ♦ UDN_cc9279a9-c439-44f4-bdc7-5c6007f6cca7 · Aug 19, 2016 at 02:35 PM 0
Share

The code actually works.

I just forgot C# needs a 'new' keyword before the Vector3.

 if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
           {                               
                m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, 0.0f, m_Rigidbody.velocity.z);
                m_Rigidbody.AddForce(Vector3.up*m_JumpPower, Force$$anonymous$$ode.Impulse);
           }

I will say though that "It gave an error, thanks anyway" shows a very dismissive and unwilling attitude.

Show more comments
Show more comments
avatar image
0

Answer by Brosilio_Gaming · Aug 19, 2016 at 05:18 PM

Put the part of the code that jumps the ball FixedUpdate(). I actually had exactly the same problem. With the ball jumping and everything. It will sometimes jump higher because of a higher framerate, because Update() is called each frame, whereas FixedUpdate() is called a set amount of times per second.

Comment
Add comment · Show 1 · 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 UDN_cc9279a9-c439-44f4-bdc7-5c6007f6cca7 · Aug 19, 2016 at 05:30 PM 0
Share

please can you explain further what do you mean by "put the part of the code"

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

7 People are following this question.

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

Related Questions

Smoothing out jumping? 1 Answer

Gravity and Jumping for a Character Controller 1 Answer

Dynamic jump? 1 Answer

Why do I double jump? This isn't supposed to happen... 2 Answers

How can I add air control to my 3d platformer's Character Controller? 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