Camera shaking problem when player jump and stay on the moving platform
Hello I am new to here.And I had search many artcles for this issue , but still don't know how to solve this issue correctly.
I move my player in FIxedUpdate , using rigidbody.addForce (interplate of rigidbody is set to "interplate" ,and physics material of capsule collider is no friction because I don't want it to get stuck when touching other collider).
I move camera in LateUpdate. (call doPosFollow)
I move platform in FixedUpdate (platform has rigidbody and iskinematic of it is true). And platform also has a trigger to set my player become child of platform.
The film is link bellow to show the shaking problem of camera. https://www.youtube.com/watch?v=qrj-kFuClYU
The white box is using "rigid.movePosition". it looks more smooth, but when I parent my player to it, player will not move along with the platform.
The yellow box is using "transform.position" to set value directly. but when i parent my player to it,although player will move along with it, but the shaking problem of camera occures.
At the end of the film I speed up the moving speed of camera to avoid shaking problem . But when player is standing on yellow box , we can see the yellow box is in the vibration.
What is the correct method to operate the movingPlatform ? I am really a super mario fan.
https://www.udemy.com/create-a-2d-game-in-unity/
I find another method in this tutorial. Although he is not using rigidbody, but I am . (and he update character in LateUpdate)
public Transform platform;
public void setPlatform(Transform taget)
{
platform = taget;
recordPos();
}
public void clearPlatform()
{
platform = null;
}
Vector3 recordPosLocal;
Vector3 recordPosWorld;
public Vector3 diff;
void syncPosOnPlatform()
{
if (platform == null)
return;
Vector3 newPosWorld =platform.TransformPoint(recordPosLocal);
diff=newPosWorld - recordPosWorld;
transform.position += diff;
}
void recordPos()
{
if (platform == null)
return;
recordPosWorld =transform.position ;
recordPosLocal = platform.InverseTransformPoint(recordPosWorld);
}
s$$anonymous$$d of setting parent, His method is find the diff of position. setPlatform/clearPlatform is called in OnTriggerEnter/OnTriggerExit. syncPosOnPlatform and recordPos are called in FIxedUpdate of my player.
When I jump to yellow box , vibration still occurs.
When I jump to white box , the player moves bigger than diff of position, Like in a escalator .
Answer by gooodpgr · Aug 09, 2017 at 11:54 AM
There is another method.
https://docs.unity3d.com/ScriptReference/Rigidbody-position.html
And I made the following changes.
(1)all moving platform using "rigid.position = newPos;" to move.
(2)move my player still using addForce in FixedUpdate
(3)move my camera in FIxedUpdate (my old code is in LateUpdate)
(4)set interplate of rigidbody is "None"
(5)set parent is not useful when (1) , so I adjust position of player by recording deltaPostion of moving platform.
(6)NPC has rigidboy must set interplate of rigidbody is "None" too, otherwise the NPC will vibrate when player push him.
public class RecordPositionDiff : MonoBehaviour {
[SerializeField]
RecordPositionDiff helper;
Vector3 oldPos;
Vector3 diff;
private void Awake()
{
oldPos = transform.position;
}
// Update is called once per frame
void FixedUpdate () {
Vector3 newPos = transform.position;
diff = newPos - oldPos;
oldPos = newPos;
}
void OnTriggerEnter(Collider other)
{
if (!TagDefined.canOnMovableSet(other.gameObject.tag))
return;
PlanetPlayerController controller = other.gameObject.GetComponent<PlanetPlayerController>();
if (controller != null)
controller.setPlatform(this);
}
void OnTriggerExit(Collider other)
{
if (!TagDefined.canOnMovableSet(other.gameObject.tag))
return;
PlanetPlayerController controller = other.gameObject.GetComponent<PlanetPlayerController>();
if (controller != null)
controller.clearPlatform();
}
public Vector3 getDiff()
{
return diff;
}
public Vector3 getHelperDiff()
{
Debug.Assert(helper != null);
return helper.getDiff();
}
}
public class PlanetPlayerController
{
RecordPositionDiff platform;
void syncPositionByPlatform()
{
if (platform == null)
return;
transform.position += platform.getDiff();
}
void FixedUpdate()
{
//...
syncPositionByPlatform();
//...
}
}
oh,There is no diffrent when I using "rigid.position = newPos;" or "transform.postion=newPos;" to move platform.
when using "transform.postion=newPos;" to move platform, "set parent" method could be working now. Otherwise is not
Your answer
Follow this Question
Related Questions
Camera Reset Script 1 Answer
How to see a prefab from inside it? 1 Answer
How can I change the viewport rect of two active cameras at runtime? - Splitscreen Multiplayer 1 Answer
how to resize my orthographic Camera to fit a dynamically generated grid of cubes(Mine Sweeper) 0 Answers
Sprite showing up in camera but not showing up in game view 0 Answers