Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Urgent 2D AI problem! Please assist!

Discussion in '2D' started by mojokage, Dec 17, 2014.

  1. mojokage

    mojokage

    Joined:
    Feb 15, 2014
    Posts:
    8
    So I have a Game Jam (a set amount of time dedicated completely to making a video game). This one is lasting a week and I'm the team's coder, it has to be done by tomorrow night and I still haven't got my AI script working! I'm currently trying to convert my 3D script to 2D which is causing problems, due to the LookAt feature it's breaking all of the code and rigidbody features that prevent it from not rotating or moving along the Z axis... I'd really really appreciate if someone could look at this code and assist or just show me a properly working 2D Enemy AI script! Thankyou!
    - Joni


    Code (JavaScript):
    1. var distance : float;
    2.     var target : Transform;  
    3.     var lookAtDistance : float = 15.0;
    4.     var attackRange = 10.0;
    5.     var moveSpeed = 5.0;
    6.     var damping = 6.0;
    7.  
    8.     private var isItAttacking = false;
    9.  
    10.     function Update () {
    11.     transform.rotation.x = 0;
    12.     transform.rotation.y = 0;
    13.     transform.rotation.z = 0;
    14.     transform.position.z = 0;
    15.  
    16.  
    17.  
    18.     distance = Vector3.Distance(target.position, transform.position);
    19.     if(distance < lookAtDistance)
    20.     {
    21.     isItAttacking = false;
    22.     renderer.material.color = Color.yellow;
    23.     lookAt ();
    24.     }
    25.     if(distance > lookAtDistance)
    26.     {
    27.     renderer.material.color = Color.green;
    28.     }
    29.     if(distance < attackRange)
    30.     {
    31.     attack ();
    32.     }
    33.     if(isItAttacking)
    34.     {
    35.  
    36.     renderer.material.color = Color.red;
    37.     }
    38. }
    39. function lookAt ()
    40. {
    41. var rotation = Quaternion.LookRotation(target.position - transform.position);
    42. transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    43. }
    44. function attack ()
    45. {
    46.     isItAttacking = true;
    47.     renderer.material.color = Color.red;
    48.     transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
    49. }
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    If I'm reading correctly, you want an AI that moves toward it's target if it's close enough to attack, that sound about right?

    I've got a character base that I use for examples like this, I've extended it to include the AI as you describe. Scene enclosed as unity package:
     

    Attached Files:

  3. BindingForceDev

    BindingForceDev

    Joined:
    Aug 13, 2014
    Posts:
    40
    I'm not sure if this helps, but hopefully it's some food for thought... In my 2D game I use a directionToTarget vector, used the to calculate the relative degree angle between the origin object and its target. Sorry if the below code seems messy/wont work unless you have initialized the vars I use and it's a bit out of context so I tried to make it more universal for you.

    Code (JavaScript):
    1. //First get the directionToTarget vector
    2. directionToTarget = (target.transform.position - transform.position).normalized;
    3.  
    4. //Calculate the attack angle, given the direction. And yes, (x,y) is intentionally flipped here (as I found it in the example), just worked that way for me, not sure why lol.
    5. var attackAngle : float  = Mathf.Atan2(directionToTarget.y, directionToTarget.x) * Mathf.Rad2Deg;
    6.  
    7. //Create a Vector 3 to store the new rotation information.
    8. var rotation : Vector3 = new Vector3(rotation.x, rotation.y, rotation.z + attackAngle - 90);
    9.  
    10. //From here you should be able to set a 2D sprite's orientation and use the info for moving towards said target.
    For some reason I had to subtract 90 to get the correct angle. I'm using those steps to assign the rotation for instantiated projectiles. Of course the code here keeps the rotation information for locking onto the target. You can also use the direction to add velocity of course. I'm currently trying to figure out a good way to rotate the origin object toward the final attackAngle, but for now my game doesn't need that functionality yet. I wish 2D had a built in LookAt type function but I haven't found a similar one line equivalent... Anyway, good luck!!!
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    in my 2D game i am using that:
    move () {
    if (Target.transform.position.x < transform.position.x)
    rigidbody2D.velocity = new Vector2 (-Speed, rigidbody2D.velocity.y);
    else if (Target.transform.position.x > transform.position.x)
    rigidbody2D.velocity = new Vector2 (Speed, rigidbody2D.velocity.y);}

    Its not exactly code, but the principle. Target is public GameObject, Speed is public float.

    for fight something like:

    TargetDistance = Mathf.Abs (Target.transform.position.x - transform.position.x);

    if (TargetDistance > FightRange)
    move();
    if (TargetDistance <= FightRange)
    fight();

    but it will work only for platformer with x-axe move.
     
    Last edited: Dec 17, 2014