- Home /
Make code less laggy? and one object in trigger rather than all?
So, i have this code using System.Collections; using System.Collections.Generic; using UnityEngine;
public class cannonshoot : MonoBehaviour
{
public float time;
//public float damage;
float timer;
void Start()
{
time++;
timer = time;
}
private void OnTriggerStay(Collider other)
{
if (timer <= 0.4)
{
if (other.gameObject.tag == "enemie")
{
other.gameObject.GetComponent<Health>().Min();
}
Debug.Log("Damage");
}
}
void Update()
{
timer -= Time.deltaTime;
if (timer < 0)
{
timer = time;
}
Debug.Log(timer);
}
}
But, it's EXTREMELY laggy, and gets all objects with tag "enemie" rather than just a few (i didn't know how to make that work)
If you can help me, it's greatly appriciated. :3
A few things jump out at me.
1) I need to know what you expect to happen
2) There is a lag built into the fact that OnTriggerStay ignores the tag check until .6 seconds has elapsed
3) There is no real synchronization between that 0.4 (how is that compiling without the f after 4? - what version of Unity and C# are you using?) time test and the timer reset every second, which means, depending on when the OnTriggerStay happens to fire, could mean, potentially, very long delays (in theory could be hours).
Point 3 is possible because there could be a series of sequences where OnTriggerStay skips because timer is > .4, but the Update loop both decrements that to zero and resets it to 1 before another OnTriggerStay happens to fire (I can't see what is triggering the OnTriggerStay, so I'm going only by what I see, which is to say I see nothing to inform how often and when the trigger would fire).
In fact, I can't tell what you intend, so I can't even hint as to what to do - I don't know what direction to point you without an expectation in $$anonymous$$d.
First of all I would remove all Debug.Log, especially from physics related methods. Have you tried profiling?
Yeah, OnTriggerStay is just inefficient and kinda bad to use in general. If you can manage to find a substitute for that (using both OnTriggerEnter and OnTriggerExit generally works for me), that would be one point of improvement, but others have already said stuff about OnTriggerStay.
Answer by anthot4 · Jul 23, 2018 at 01:40 PM
As @JVene says, you need to explain what you want the code to do better. But I'm guessing that the Cannon is instantiated as soon as it is fired and what you are trying to do is check if the timer has been longer than 0.6 seconds and the cannon collides with an enermie then execute the min function on the health script of the enermie. I think the reason for your problems is that you are using OnTriggerStay which is executed every frame that a trigger collider enters the canon. Try changing it to OnTrigerEnter and that will only be called once when the canon hits the enermie. In future though, you need to explain the problem in more detail as it isn't that clear what you are trying to achieve.
that's not it.. I'll try to explain more
It's a simple static object with a trigger, and a timer, when the timer reaches below 0,4 it'll execute the health script to every single object with the "enemie" tag inside of the trigger.
which, is also not what i want, i'd prefer for it to only execute the health script on one or two enemies in range, rather than all.
Sorry i wasn't clear enough
The code you have put up should only execute the $$anonymous$$in function in the health script during a trigger collision between enermie and Cannonshoot. I suggest you look away from this script and have a look elsewhere for the problem as its not this script thats causing the problem. You can always use breakpoints and get more information about what "Other" contains.
as long as it's in the trigger, it checks if the timer is below 0.4 if it is, is executes, but is very laggy.
Answer by Liaram · Jul 23, 2018 at 03:39 PM
Try removing Debug.Log(timer); and Debug.Log("Damage"); Writing to log possibly several times each and every frame would be quite laggy. If you need to observe the values you can make the variable public and look at it in the editor.
Your answer
Follow this Question
Related Questions
Help with making a triggered gui message 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Change main Camera to another camera with a Trigger then back with a Timer. 1 Answer
Create custom Triggerevent 0 Answers