Search Unity

Cube Enemy Controller is not working

Discussion in 'Scripting' started by nahinjk34, May 25, 2017.

  1. nahinjk34

    nahinjk34

    Joined:
    Apr 20, 2017
    Posts:
    21
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class CubeController: MonoBehaviour
    6. {
    7.     public float speed;
    8.     public float health;
    9.     public float range;
    10.     public int Damage = 5;
    11.  
    12.     float damage;
    13.     int Deaths;
    14.     Transform Player;
    15.     Rigidbody rig;
    16.     ParticleSystem part;
    17.  
    18.     void Start()
    19.     {
    20.         GameObject player = GameObject.Find ("Player");
    21.         Shoot shot = player.GetComponent<Shoot> ();
    22.         damage = shot.damage;
    23.  
    24.         part = gameObject.GetComponent<ParticleSystem> ();
    25.         rig = gameObject.GetComponent<Rigidbody> ();
    26.         Player = GameObject.FindGameObjectWithTag ("Player").transform;
    27.  
    28.     }
    29.  
    30.     void Update()
    31.     {
    32.         if (health <= 0)
    33.         {
    34.             StartCoroutine (Death ());
    35.             Deaths = Deaths + 1;
    36.         }
    37.  
    38.         GameObject player = GameObject.Find ("Player");
    39.         float dist = Vector3.Distance (gameObject.transform.position, player.transform.position);
    40.  
    41.         if (range > dist)
    42.         {
    43.             gameObject.transform.LookAt (Player);
    44.  
    45.             rig.AddForce(0.0f,0.0f,speed * Time.deltaTime);
    46.         }
    47.  
    48.     }
    49.  
    50.  
    51.  
    52.     void OnTriggerEnter(Collider other)
    53.     {
    54.         if (other.gameObject.CompareTag ("Shard"))
    55.         {
    56.             part.Emit (1);
    57.             health -= damage;
    58.             Destroy (other.gameObject);
    59.         }
    60.     }
    61.     public IEnumerator Death()
    62.     {
    63.         part.startColor = Color.red;
    64.         part.Emit (1);
    65.         yield return new WaitForSeconds(0.25f);
    66.         Destroy (gameObject);
    67.     }
    68. }
    So I am using this and when I get close to the enemy it moves to me, but then it just keeps going in that direction even if it is looking at me... I think that it is using global and not local... and if so i dont know how to change that
     
  2. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Just a few things I'll mention here.. one I find it kind of odd that you've got so a call to find the player in your Update, plus 2 different cases where you set a variable to be the player object in the start. I'd suggest just create a member variable and set that once in Start or Awake... unless your game is multiplayer, which I'm thinking this code wouldn't suffice for anyways. Anyways, that's not the cause of your problem.. just a suggestion.

    Re: the problem, there's one thing confusing me here.. you're use of Rigidbody.AddForce... I see you supplying 3 parameters, but when I look up AddForce in the Unity Manual, there's two different function calls:

    public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force); (two parameters)
    public void AddForce(float x, float y, float z, ForceMode mode = ForceMode.Force); (4 parameters)

    So I'm not sure if you're using this function correctly.

    Add to that, you're only changing movement of the enemy when they are out of range of the player. I think you need to set something up on changing direction when they are within range too.

    Edit: had to rush off to a meeting right after posting this, so sorry if I misunderstood your code at all.
     
    Last edited: May 25, 2017
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    So many GameObject.Find uses. Those are slow and not good for your game, and there's even one that you call inside of Update() , where you could at least cache it at the beginning (Start) and re-use until you learn of better ways :) Feel free to look it up/ask.

    As for the 2 versions of AddForce, from the previous poster, that's totally okay. The OP's version is using the second option (because the 4th parameter is optional, ie: has a default value, you don't have to supply it if you don't want to (and/or don't want to alter that option*).

    It's supposed to look at you and go towards you, up until you're <= distance away (from range). Are you saying it never stops? Does it ever get close enough to stop adding the force? Maybe you're adding up too much force, I'm not sure.
    You could try to do the LookAt. and then set the velocity. That should give it speed to move towards you. Then once you're in range, disable the velocity. Which, of course, you could just also disable the velocity with your current AddForce setup, also.
    just like:
    Code (csharp):
    1.  else rig.velocity = Vector3.zero;
    Try that.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, as for the explanation, I took him to mean that the enemy would start approaching but once it was in range, it wouldn't stop approaching.. and just drifted/kept on going.. Not entirely sure how that would look.. if you drifted far enough you'd start getting force on the way back lol .. Anyways, a work in progress.

    As for adding force, probably "speed" to the author, is just a variable to use. .and kinda makes sense, if he's moving on just that 1 axis, the 'z' component is a value.. I dunno. :)

    Side note. I usually reserve AddForce for more bursty (lol) things, like jumping or sharp movement.. moving is usually easier with velocity, imo.. but not really right/wrong there, I suppose.
     
  5. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    I actually just misunderstood AddForce (I'm rusty with Unity and getting back into it).. lol I get it now though. I had a brainfart there.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It's all good. Probably happens once or twice a day to me, even here regularly lol.
     
  7. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    You're quick on the draw too.. I edit my posts a lot, and I deleted that post you replied to when I realized my brain fart, but you were too quick. hahaha
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm going to stop talking now. poor guy might never find his answer hahaha. :)
     
  9. nahinjk34

    nahinjk34

    Joined:
    Apr 20, 2017
    Posts:
    21
    thanks for the help
     
  10. nahinjk34

    nahinjk34

    Joined:
    Apr 20, 2017
    Posts:
    21
    The object is still only moving in one direction... i want it to move in the direction that it is looking and it just moves in one direction if i am in the range of the enemy
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You're welcome. Did you get anything useful/fixed/improved based on the feedback from this thread? :)
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah, okay, now that you explained it in those words, I think I know what the issue is.
    You might want to try:
    Code (csharp):
    1. rig.velocity = transform.forward * speed; // (or AddForce if you prefer)
    After you look at. transform.forward will be the object's "forward direction" - locally.. as opposed to before, you were setting the Vector based on a global direction.
    I overlooked that, as I was a tiny bit lost in understanding what your issue was. :)