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

How to guess where a flying ship may be (Turret AI)

Discussion in 'Scripting' started by Shadowing, Jun 12, 2015.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    Hey guys

    Anyone have any ideas of a way I can have my turret make a educated guess where a ship might be judging by the direction its flying? So maybe like a future forward motion guess.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Future position = current position + velocity * time
     
  3. j-bbr

    j-bbr

    Joined:
    Jun 10, 2015
    Posts:
    13
    You could do something like this:
    Code (CSharp):
    1. probableFuturePosition = ship.transform.forward * shipspeed;
     
  4. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    Thanks for the reply abnu. I didn't know I could do math on forward like that. I'll try that out.
     
  5. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    Right now im using

    Code (csharp):
    1. Quaternion targetlook = Quaternion.LookRotation(TargetTransformPosition - MuzzleTransformPosition);
    and then

    Code (csharp):
    1. this.transform.rotation = Quaternion.Lerp(this.transform.rotation,targetlook,Time.deltaTime * 10000);
    So I guess my issue is I need the turret to turn and point at the future location
     
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    oh wait I think I just need to give Lookrotation
    Code (csharp):
    1. ship.transform.forward * shipspeed
     
  7. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    ya that idea is incorrect lol. that just gave the turret which way I was looking :p
     
  8. j-bbr

    j-bbr

    Joined:
    Jun 10, 2015
    Posts:
    13
    Which one? :)An even Simpler way would be to add an empty child to the ship called futurePosition and put it as far ahead of the ship as you want. Then put a script like this on the turret
    Code (CSharp):
    1. public Transform target;
    2.  
    3. void Update()
    4. {
    5. transform.LookAt(target);
    6. }
    and drag the futurePosition into the Transform slot.
    if your ship doesn't have a constant speed:
    Code (CSharp):
    1. public float offset =3f;
    2. void Update()
    3. {
    4. transform.localPosition = new Vector3(0, 0,offset );
    5. }
    on the futurePosition GameObject and then vary offset according to the speed of your ship
     
  9. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    That is an awesome idea Abnu
    Not sure that is going to work though. Been thinking about it for a while.
    I use a raycast to double check to make sure the ship is viewable before the turret fires.

    So Ive been trying to think of a way to still do that.
    One idea is give the transform that is placed ahead of the ship a box collider but then the collider would hit stuff. I notice if I use ignore collision by layer on stuff raycast stops working as well.
    So now I have another dead end again.

    Do you have any ideas on this by chance?
    Really appreciate the help so far btw
     
  10. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    Actually I just thought of something
    Instead of raycasting making sure I fire at something I want the turet to fire at. I'll just have it check things not to fire at. This way the ray cast doesn't have to hit the ship.

    Going to to see if that all works then. Think it will all be ok after doing that change.
     
  11. j-bbr

    j-bbr

    Joined:
    Jun 10, 2015
    Posts:
    13
  12. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
  13. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    Thanks Karwoch I'll check that out. I got abnu idea working great with placing a transform ahead of the ship and changing its distance from the ship according to the ships speed.
    Thanks a lot Abnu
     
  14. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    The little difference - with this scirpt I only put speed of my target, my position, target position, and velocity of my projectile and it works! Hits directly every time even on 6km range :D Now I only need to add to this velocity of my ship, but I guess simply adding my speed to speed of my target should solve this problem with same function.