I have a empty object follow the player
but the player is falling faster than the object
the player and the object movement script booth in fixedupdate Function
Can you guys help me with it
Without seeing the code, it could be caused by a number of factors. Please share the relevant code, especially the parts where you handle the movement for both the player and the empty object. Also, let us know if you’re using a Rigidbody or a CharacterController, as it could be related to physics settings.
thanks for reply
so this is the player movement code:
private void FixedUpdate()
{
Move();
Gravity();
Flip();
}
#endregion
#region Jump
public void OnJump(InputAction.CallbackContext context)
{
if (context.performed && CK.IsGrounded)
rb.velocity = new Vector2(rb.velocity.x, JumpForce);
if (context.canceled )
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
private void Gravity()
{
if(!CK.IsGrounded)
{
if (rb.velocity.y > 0 )
{
rb.gravityScale = 2;
}
else if (rb.velocity.y < 0)
{
rb.gravityScale = Mathf.Lerp(rb.gravityScale,4,1);
}
}
else
{
rb.gravityScale = 2;
}
}
#endregion
#region Movement
private void Move()
{
if(CK.IsGrounded)
rb.velocity = new Vector2(horizontal * Speed, rb.velocity.y);
else
rb.velocity = new Vector2(horizontal * AirSpeed, rb.velocity.y);
if (horizontal != 0)
Anim.IsWalking = true;
else
Anim.IsWalking = false;
}
private void Flip()
{
if ((horizontal > 0 && !IsFacingRight) || (horizontal < 0 && IsFacingRight))
{
IsFacingRight = !IsFacingRight;
CF.Turn();
}
if (IsFacingRight)
{
Vector3 rotator = new Vector3 (transform.rotation.x , 0 , transform.rotation.z);
transform.rotation = Quaternion.Euler(rotator);
}
else
{
Vector3 rotator = new Vector3(transform.rotation.x, 180 , transform.rotation.z);
transform.rotation = Quaternion.Euler(rotator);
}
}
public void OnMove(InputAction.CallbackContext context)
{
horizontal = context.ReadValue<Vector2>().x;
}
and this is the follower script
private void FixedUpdate()
{
transform.position = Player.position;
}
public void Turn()
{
LeanTween.rotateY(gameObject, Rotation(), FlipTime).setEaseInOutSine();
}
private float Rotation()
{
IsFacingRight = !IsFacingRight;
if (IsFacingRight)
return 0;
else
return -180;
}
and this is the player rigidbody
another info
I made a virtual camera to follow that follower
so this is the virtual camera script
private void Awake()
{
if ( instance ==null )
{
instance = this;
}
for (int i = 0; i < AllCamera.Length; i++)
{
if (AllCamera[i].enabled)
{
CurrentCamera = AllCamera[i];
Transposer = CurrentCamera.GetCinemachineComponent<CinemachineFramingTransposer>();
}
}
}
public void StartDump(bool IsPlayerFall)
{
Damper = StartCoroutine(Dumping(IsPlayerFall));
}
public IEnumerator Dumping(bool IsPlayerFalling)
{
IsLerpingYDamping = true;
float StartDumpAmount = Transposer.m_YDamping;
float EndDumpAmount = 0f;
if (IsPlayerFalling)
{
EndDumpAmount = FallingYDamp;
LerpedFromPlayerFalling = true;
}
else
{
EndDumpAmount = NormalDump;
}
float DampingTimer = 0f;
while (DampingTimer < DampingTime)
{
DampingTimer += Time.deltaTime;
float LerpedDumpAmount = Mathf.Lerp(StartDumpAmount, EndDumpAmount, (DampingTimer/DampingTime));
Transposer.m_YDamping = LerpedDumpAmount;
yield return null;
}
IsLerpingYDamping = false;
}
and I call this corotuine by this script
void Start()
{
FallSpeedDumbChanging = CameraManager.instance.WhenDampChange;
}
void Update()
{
if (rb.velocity.y < FallSpeedDumbChanging && !CameraManager.instance.IsLerpingYDamping && !CameraManager.instance.LerpedFromPlayerFalling)
{
CameraManager.instance.StartDump(true);
}
else if (rb.velocity.y >= 0 && !CameraManager.instance.IsLerpingYDamping && CameraManager.instance.LerpedFromPlayerFalling)
{
CameraManager.instance.LerpedFromPlayerFalling = false;
CameraManager.instance.StartDump(false);
}
}
If I understand correctly, the issue might be that you’re directly copying the player’s position to the follower. Since the player accelerates while falling due to gravity, it might look like the follower is lagging behind (if this is what you mean by ‘slower’).
A possible solution that would be easy, is to make the empty object a child of the player in the hierarchy. That way, its position will be relative to the player and it will move at the exact same rate. Have you already tried this?
If you only want this to happen at certain times, you can use:
follower.transform.SetParent(player.transform); to make it a child, and follower.transform.SetParent(null); to detach it when needed.
I did but the problem is when the player flip the follower also flip with him and I don’t want it to happen
I already showed you that when the player flip the follower change his rotation smoothly
Why not give the Follower a kinematic Rigidbody, and then connect it to the player via a physics joint of some sort?
thanks it’s worked
