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!,
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);
}
}
}
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();
}