Is there a reason this script should lower fps by 10-15 frames?
This script when enabled on my object lowers fps by 10-15 frames, any reason why? I'm pretty sure its the vector3.movetowards but I really badly need it, are there any alternatives that are more performance friendly?
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour {
public float movementSpeed;
public float animMovementSpeed;
Animator animator;
public GameObject player;
public float distance;
public float minDistance;
// Use this for initialization
void Awake ()
{
animator = GetComponent<Animator>();
player = GameObject.Find("Player");
}
// Update is called once per frame
void Update ()
{
if (distance > minDistance)
{
transform.position = Vector3.MoveTowards(transform.position, player.transform.position, movementSpeed * Time.deltaTime);
animator.SetFloat("MovementSpeed", 1.0f);
}
if(distance <= minDistance)
{
animator.SetFloat("MovementSpeed", 0.0f);
}
distance = Vector3.Distance(transform.position, player.transform.position);
if (player.transform.position.x > transform.position.x)
{
transform.eulerAngles = new Vector2(0, 180);
}
else
{
transform.eulerAngles = new Vector2(0, 0);
}
}
}
Does the object you move have a static collider? (a collider but no rigidbody)
$$anonymous$$easuring performance by FPS is a poor choice. Losing 10 frames from what base value? 30 fps? 30000 fps? Either way, saying 10 fps tells you nothing. You need to measure how long the script took to execute: Did it take 0.01ms or 1000ms?
To find out what is really going on, you'll need to use the profiler. Or else everyone has to guess what your problem is. I'm guessing you're moving a static collider as suggested by Taxen0
Your answer
Follow this Question
Related Questions
Unity Not using GPU anymore for anything 0 Answers
Studdering on android just moving a transform? 0 Answers
framerate suddenly drops in iphone 11 pro max 1 Answer
How should I fix the post processing framerate 0 Answers
Strange framerate problem 0 Answers