Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by drhodge · Feb 09, 2021 at 10:08 PM · physicsaddforcefirst-person-controller

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!,

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Megaboy238 · Feb 10, 2021 at 12:06 AM 0
Share

Do you have the enemies script if it only happens when they appear it may be in there?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
     }
 }

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image drhodge · Feb 10, 2021 at 01:09 AM 0
Share

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.

avatar image AbandonedCrypt drhodge · Feb 10, 2021 at 08:46 AM 0
Share

theres no AddForce in this script. You would be better off showing us your movement controller for your player

avatar image
0

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();
      }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Feb 10, 2021 at 10:44 AM 0
Share

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

avatar image drhodge · Feb 10, 2021 at 01:57 PM 0
Share

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

avatar image AbandonedCrypt drhodge · Feb 10, 2021 at 02:47 PM 0
Share

@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().

avatar image drhodge · Feb 10, 2021 at 11:39 PM 0
Share

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.

avatar image Megaboy238 drhodge · Feb 11, 2021 at 12:49 AM 0
Share

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

avatar image drhodge Megaboy238 · Feb 11, 2021 at 01:31 AM 0
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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

193 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Apply A Force to Reach a Specific Height 3 Answers

Why AddForce up is higher than AddForce right? 0 Answers

Affecting FPS controller/Character Motor physics with game object 1 Answer

Why use AddForce rather than modify velocity? 4 Answers

AddForce at mouse position only on table area 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges