Hi every body i have a script attached to my player that make it jump.On my computer the script works fine but on mobile usally jump correctly but some times it over jump WHY???
-My Y gravity is -60
-I play it on a mobile device(android)
that the script
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
bool Su;
bool landed=true;
// Update is called once per frame
void Update () {
if(Input.touchCount>0){
Su=Input.GetTouch(Input.touchCount-1).deltaPosition.y>10&landed;
}
else{
Su=false;
}
if(Su&landed){
rigidbody.AddForce(new Vector3(0,19,0),ForceMode.VelocityChange);//USE ForceMode.Impulse give the sameresult
Su=false;
}
}
void OnCollisionStay(){
landed=true;
}
void OnCollisionExit(){
landed=false;
}
}
But as a side note, be sure to keep checking your input in the regular Update(). Input is checked and cleared per regular update cycle, and if you check it in fixed update you can miss inputs. It should work if you set flags or variables in Update() for your input and then check them in FixedUpdate().
I am literally building a physics-based character controller right now so I am quite sure this is the case. Try it yourself if you want.
If you have a game running at 60fps and your FixedUpdate() is running at 50fps you will likely rarely run into issues. Try it with a FixedUpdate of 10 or 20 fps.
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Jump")) Debug.Log ("Jumped.");
}
void FixedUpdate(){
if (Input.GetButtonDown("Jump")) Debug.Log ("Fixed Update: Jump");
}
FixedUpdate() will miss inputs.
It seems to be the case that when Update fires, it has all the input queued up since the last Update, and it gets rid of it afterwards so it can react to the new input. FixedUpdates() happen independent of this so if an input happens in between two Updates() without a FixedUpdate() in there, the FixedUpdate() will miss it.
I just tried this with 60FPS with fixed at 10FPS and FixedUpdate() missed 5 consecutive jump presses.
Yes, that would make a difference. I imagine the InputManager works per frame there as well, but since it is keeping track of the state of the button internally, you will never miss it.
It’s important for things like “Jump” where it is a single button press.
Relating to the original question, the docs don’t explicitly specify Update (though the examples check it there). I would guess that it is refreshed on a per frame basis as well, and that you will probably miss touches if you only check it in FixedUpdate.
You won’t notice it because FixedUpdate runs according to your Fixed Timestep setting, which by default runs smoothly. However if you severely adjust this value so FixedTimeStep occurs every few seconds, you will certainly note input is not being handled as well as it should. Personally I would keep only physics stuff in FixedUpdate and input and everything else in Update().
I didnt test your code, but I just by looking at it i found what seems to be a reasonable amount of wtf, which i have tried to correct as good as i can without doing the testing for you. Read the comments in the code.
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour
{
bool PerformJump = false;
bool Landed=true;
void Update ()
{
for(int i = 0; i < Input.touchCount; i++)
{
if(Input.GetTouch(i).phase == TouchPhase.Began) //notice "Began" - otherwise it would keep setting PerformJump to true
{
if(Input.GetTouch(i).deltaPosition.y>10)
{
PerformJump = true;
break;
}
}
}
}
void FixedUpdate()
{
if(PerformJump Landed)
{
rigidbody.AddForce(new Vector3(0,19,0),ForceMode.VelocityChange);
PerformJump = false;
Landed = false;
}
}
void OnCollisionEnter()
{
Landed = true;
}
void OnCollisionExit()
{
//Removed, because if you touched two platforms, and now only touch one, you should STILL be "landed". If you need it, fix it first
//Landed = false;
}
}
I copy and paste your code , the player doesn’t over jump, but also, it doesn’t jump…
On 25 times i slide up the player jumps only 1 times, i retried several times that but it rarely jump…
DON’T KNOW WHAT’S THE PROBLEM
I doubt that using deltatime in FixedUpdate is a good idea.
Belva, try remove this “if(Input.GetTouch(i).deltaPosition.y>10)” … then he should jump whenever you touch the screen I misinterpreted that you wanted swipe control, so using deltaPosition together with touch phase began is not so good.