- Home /
Weird MoveTowards behaviour on low fps only.
Hey there, I made a nice animation to transit my camera between my menu and a shop menu using Vector3.MoveTowards. It works smooth and no problems expect if the game runs on low fps( like 15), the camera position will be abit higher from where it should be, and everytime you transit again between shop and menu, the camera will get higher and higher. I tried in code manually setting position of camera after animation(movetowards) is done, it doesn't change anything, and printing values of camera position and desired place position are the same, when they are not?
Picture:
sorry it doesnt let me upload here, gives some parsing error, the link is just lightshot printscreen.
Code of animation:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraLogic : MonoBehaviour
{
private bool isrotating = false;
public bool isgoingshop = false;
private bool isplaying = false;
private bool triggeroncemenu = true;
private bool triggeronceshop = true;
public Transform shopview;
public GameObject menu;
public GameObject shopmenu;
public GameObject firstmsg;
private float lerpspeed = 400f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
if(!isplaying)
{
if(!isgoingshop)
{
if(triggeroncemenu)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(20, -90, 0), 2f * Time.fixedDeltaTime);
transform.position = Vector3.MoveTowards(transform.position, new Vector3(0,4.5f,-5), lerpspeed * Time.fixedDeltaTime);
}
if(Vector3.Distance(transform.position, new Vector3(0,4.5f,-5)) < 0.5f)
{
if(triggeroncemenu)
{
transform.rotation= Quaternion.Euler(20, 0, 0);
firstmsg.SetActive(false);
menu.SetActive(true);
isrotating=true;
triggeroncemenu=false;
triggeronceshop = true;
transform.position = new Vector3(0,4.5f,-5);
}
}
}
else
{
if(triggeronceshop)
{
isrotating=false;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(20, 90, 0), 2f * Time.fixedDeltaTime);
transform.position = Vector3.MoveTowards(transform.position, shopview.position, lerpspeed * Time.fixedDeltaTime);
}
if(Vector3.Distance(transform.position, shopview.position) < 0.5f)
{
if(triggeronceshop)
{
triggeronceshop=false;
triggeroncemenu=true;
shopmenu.SetActive(true);
transform.rotation= Quaternion.Euler(20, 0, 0);
transform.position = shopview.position;
print(transform.position);
print(shopview.position);
}
}
}
}
if(isrotating)
transform.Rotate(0,0.2f,0, Space.World);
}
public void gamemode()
{
isplaying=true;
isrotating=false;
transform.rotation= Quaternion.Euler(20, 0, 0);
transform.localPosition= new Vector3(0,80,-100);
}
public void goshop()
{
isgoingshop=true;
}
public void gomenu()
{
isgoingshop=false;
}
}
TLDR: on low fps scenario only, 1 object placed on position of other object but sometimes will be a bit higher from where it should be, when their position is equal. works fine on high fps. everything done in fixedupdate.