- Home /
Enemies mess up force controlled Player
I wrote a First-Person controller script that uses AddForce() to move the player around. I ran into a strange bug when I started adding enemies. My player can run around, use his jet-pack, and work perfectly, but when enemies spawn, it screws things up. Looking in the direction of the enemies reduces the player's speed by so much that he can barely move. looking away lets you run around like usual. Destroying the enemies instantly makes everything better again. I am so confused right now. Keep in mind, my Player has absolutely NO script related interactions with the enemies. the enemies ONLY access the player's position for aiming. I thought it might be a scaling problem or something like that since I did my own modelling and importing, but after removing components that might have been related to that problem, the issue still existed. If you have any idea what might be causing this issue that would be great! Thanks in advance!,
Do you have the enemies script if it only happens when they appear it may be in there?
Answer by drhodge · Feb 10, 2021 at 01:08 AM
public class Terminator : MonoBehaviour { public GameObject torso; public GameObject legs; public GameObject arms;
public float walkingSpeed;
public float turnSpeed;
public GameObject rightGun;
public GameObject leftGun;
float distance;
GameObject target;
private Animator anim;
private Animator legAnim;
bool walk;
public GameObject targetingSystem;
public GameObject arm1;
public GameObject arm2;
public GameObject head;
// Start is called before the first frame update
void Start()
{
walk = false;
anim = GetComponent<Animator>();
legAnim = legs.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
PartDestruction();
if(walk == true)
{
transform.Translate(Vector3.forward * Time.deltaTime * walkingSpeed);
legAnim.SetBool("walk", true);
}
else
{
legAnim.SetBool("walk", false);
}
if (targetingSystem.GetComponent<targetingSystem>().enemyVisible == true)
{
anim.SetBool("shoot", true);
walk = true;
target = GameObject.FindGameObjectWithTag("Player");
GunTarget();
FullTarget();
}
else
{
anim.SetBool("shoot", false);
walk = false;
}
}
void FireRight()
{
rightGun.GetComponent<shootTEMP>().Fire();
}
void FireLeft()
{
leftGun.GetComponent<shootTEMP>().Fire();
}
void GunTarget()
{
var q = Quaternion.LookRotation(target.transform.position - transform.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 50 * Time.deltaTime);
//var lookPos = target.transform.position - torso.transform.position;
//lookPos.y = 0;
//var rotation = Quaternion.LookRotation(lookPos);
//torso.transform.rotation = Quaternion.Slerp(torso.transform.rotation, rotation, Time.deltaTime * turnSpeed);
}
void FullTarget()
{
var lookPos = target.transform.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime* .8f);
}
void PartDestruction()
{
if(arm1 == null)
{
this. GetComponent<healthTEMP>().ChangeHealth(-1000);
}
if (arm2 == null)
{
this.GetComponent<healthTEMP>().ChangeHealth(-1000);
}
if (head == null)
{
this.GetComponent<healthTEMP>().ChangeHealth(-1000);
}
}
}
So this is the script (excuse any bad practices). I used similar code to make another enemy work, but THAT enemy does not cause the problem.
theres no AddForce in this script. You would be better off showing us your movement controller for your player
Answer by Megaboy238 · Feb 10, 2021 at 10:40 AM
Im not sure but when enemyVisible = true I think this continually loops in Update maybe draining resources and should probably only loop once
if (targetingSystem.GetComponent<targetingSystem>().enemyVisible == true)
{
anim.SetBool("shoot", true);
walk = true;
target = GameObject.FindGameObjectWithTag("Player");
GunTarget();
FullTarget();
}
Possibly, if the movement script adds force depending on time.deltaTime, a drop in framerate, for example, if resource-draining processes are occuring, for example, looking at an enemy, may cause this strange movement. Good spot $$anonymous$$egaboy238.
@drhodge try not to use GetComponent or GameObject.Find so many times. Do it once in the start and store the references. Hopefully that helps
Good points. This is probably not false. I will have to try cutting down on the jobs per frame. Part of the reason that the enemy has to continually search for a player is that switching between a vehicle and walking, the player game object changes. I can loop it slower though, say once a second instead of once every frame. I am not sure how this problem would only occur when the player is looking at the enemy though. We'll see
@drhodge you can cut this down an awful lot by
1) caching your TargetingSystem in a variable
2) caching your player in a variable and simply updating that variable when the player gameobject changes
Gets you from one GO.Find and GO.GetComponent each frame to a single call each in Start().
So, I did some work making it more performant but it didn't work. Interestingly enough, if I disable the script completely, the problem still occurs. (Sorry, should have noticed that before) I don't have ANY idea what the problem is now.
Is this script on the enemies to target the player? Is the targetingSystem an asset from someone else as they are often design to have the player target enemies and have references to the player via object or tag name etc to influence which way the player looks. Check the targetingSystem code or share
The targeting system is a drag and drop asset that checks if the player is visible by the enemies and has the enemies target him. It is $$anonymous$$OST LIKELY not that script because I use it with other enemies which do not cause this problem. Even so, I will post the code when I get a chance.