- Home /
Fps getting lower over time
Hello! I was trying to make simple raycast AI. It works, when i launch it FPS is fine, but after about 30 seconds game becomes very laggy. Sorry for my english :P Here is full script :
using UnityEngine;
using System.Collections;
public class EnemyRaycastAI : MonoBehaviour {
[Header ("What can object move on")]
public LayerMask toRideOn;
[Header("What object should jump on")]
public LayerMask toJumpOn;
[Header ("What object should attack")]
public LayerMask toAttack;
public float lazyCheckPS = 2; // its called lazy check, because its used when enemy doesnt see player. Player finding raycast isnt implemented yet. It sets how many times per second ray should be cast.
public float fastCheckPS = 10;
Rigidbody2D rb;
Transform objectTransform;
public float offx = 1; // Parameters of ray
public float offy = 1; // Parameters of ray
private float x = 1; // Parameters of ray
private float y = 1; // Parameters of ray
public float lenght = 1; // Parameters of ray
public float speed = 1; // speed of enemy
void Start () {
rb = GetComponent<Rigidbody2D>();
objectTransform = GetComponent<Transform>();
StartCoroutine(LazyCheck());
}
void Update () {
Movment();
}
public IEnumerator LazyCheck()
{
for (int i = 0; i >= 0; i++)
{
yield return new WaitForSeconds(1 / lazyCheckPS);
Check();
}
}
public void Check ()
{
x = offx * transform.localScale.x;
y = offy *Mathf.Abs(transform.localScale.x) ;
RaycastHit2D groundCheck = Physics2D.Raycast (transform.position,new Vector2 (x, y) , lenght, toRideOn);
Debug.DrawLine(transform.position, new Vector2 (x,y), Color.black);
if (groundCheck.collider != null )
{
Movment();
}
else
{
speed = -speed;
objectTransform.localScale = new Vector3(-objectTransform.localScale.x,objectTransform.localScale.y,objectTransform.localScale.z);
}
}
public void Movment ()
{
rb.velocity = new Vector2 (speed, rb.velocity.y);
}
Comment
Your answer
Follow this Question
Related Questions
Please help me with AI 1 Answer
Help me with AI 1 Answer
Need coding help with fps 0 Answers
Have falling object exit from a collider after collision? 2 Answers
AI Evasion help 1 Answer