Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Little modification in my AI Script

Discussion in 'Scripting' started by Teilor111, Jul 21, 2016.

  1. Teilor111

    Teilor111

    Joined:
    May 22, 2016
    Posts:
    7
    Hi, I made script for enemy AI. Enemy should fallow player and attack when player is near. All works fine, one fail is only with enemy walk. I use this fragment for it:

    Code (CSharp):
    1.  
    2. if ((Vector3.Distance(target.position, myTransform.position) <= viewdistance) && (Vector3.Distance(target.position, myTransform.position) > ray_distance_e1))
    3.         {
    4.             transform.LookAt(target.position);
    5.             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    6.        }
    7.  
    Enemy flying instead of going straight on the road. (as in attached image).
    Could you help me with resolve this problem?
    Thanks,
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Either change the stairs to a ramp, and then move the enemy WITH RIGIDBODY (so either force adding, velocity setting or movetowards) but only on x and z axis or let the built in pathfinder handle this
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    You could raycast down from where you're trying to move the enemy, and place it at the point of the hit.
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    That would cause the enemy to move very very strangely (so basically teleport up) when "moving up" stairs
     
  5. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    I would just take the "y" portion out of the lookat(). As of right now you tell it to look directly in any direction and move in that direction. Take out the y portion so they still look that way, just not up or down, and they should move along the ground.... asuuming your enemy controller can handle the stairs as well as the player's.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That would cause the enemy to not move up and down at all. And I would say that if they're posting here regarding moving up and down stairs then the assumption would be that the enemy controller can't handle stairs. If it could, OP wouldn't have posted.

    I'd go the raycasting route as well and perhaps smooth it a bit so that the enemy doesn't pop with changes in elevation - or make the collider for the stairs a ramp and raycast against that instead.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Combining lookAt with the y reset to the enemy's Y and a raycast to move the thing down to/up from the ground would solve everything.

    OP's code is tilting the enemy back to look at the player, and then moves it. It'll move the enemy's feet through the floor at the top of the ramp, as long as the player keeps moving.

    Not if it's done every frame. It'll speed the enemy up slightly (as it'd move the same horizontal distance, pluss some vertical distance), but that could be fixed by normalizing the movement after adjusting it.
     
  8. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Nobody even remotely considering the builtin pathfinder? I mean, that can go up stairs, go towards things, would find a short path etc
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    We use the built-in Navmesh extensively in our project, and after over a year of experience and a lot of work, I can say confidently that it's utter S*** and should be avoided at all costs for anything but the smallest hobby project.

    There's no piece of technical debt in our project that's bigger than using the navmesh. It's got good intentions, but the system is simply not done, and you will run into huge issues that have to be worked around again and again and again.
     
    KelsoMRK and gorbit99 like this.
  10. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Then changing the steppes to a ramp and then:
    Vector3 mov = (transform.position - player.position).normalize * speed;
    rb := rigidbody of character
    Vector3 velocity = rb.velocity;
    velocity.x = mov.x; velocity.z = mov.z;
    rb.velocity = velocity
    and then just let things happen?