- Home /
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
This is usually the answer :
https://unity3d.com/learn/tutorials/topics/scripting/delta-time
THAN$$anonymous$$S FOR YOUR HELP but can you explain more further
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.
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.
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.
yeah it's my mistake to accept answer but later i checked it's not work for me
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.
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 :)
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.
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
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.
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.
please can you explain further what do you mean by "put the part of the code"
Your answer
Follow this Question
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