Search Unity

Bird critter help

Discussion in 'Scripting' started by ricardoaffonso, Mar 19, 2018.

  1. ricardoaffonso

    ricardoaffonso

    Joined:
    Feb 21, 2018
    Posts:
    17
    Hi! does anyone know how to make a script for a bird critter?

    What I'm looking for is just a simple script for a bird (already animated) to fly around, including rotations and all, and also if possible pathfinding so it doesn't hit trees. I have looked in the asset store and all over the internet but most flight scripts are for planes or 2d things..
     
  2. ricardoaffonso

    ricardoaffonso

    Joined:
    Feb 21, 2018
    Posts:
    17
    This is what I have but it doesn't quite do what I want

    public class flight : MonoBehaviour {

    Vector3 target;

    Rigidbody rb;
    Vector3 m_EulerAngleVelocity;

    float timeToChange = 0;
    float angle;

    public float speed = 1;

    void Start () {
    findValues();
    findTarget();
    }
    void findValues(){
    timeToChange = Random.Range(0, 1000);
    angle = AngleBetweenVector2(transform.position, target);
    }

    void findTarget(){
    target = new Vector3(Random.Range(0F, 300.0F), 0, Random.Range(0F, 300.0F));
    Debug.Log(target);
    }
    private float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
    {
    Vector2 diference = vec2 - vec1;
    float sign = (vec2.y < vec1.y) ? -1.0f : 1.0f;
    return Vector2.Angle(Vector2.right, diference) * sign;
    }

    private void Update()
    {
    timeToChange -= 1;
    if(timeToChange<=0){
    findValues();
    }
    }

    void FixedUpdate()
    {
    rb = GetComponent<Rigidbody>();
    rb.MovePosition( transform.position + transform.forward * Time.deltaTime*speed);

    m_EulerAngleVelocity = new Vector3(0, angle, 0);
    Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * Time.deltaTime);
    rb.MoveRotation(rb.rotation*deltaRotation);
    //Debug.Log("flyaway");
    }

    }
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Please use code tags when posting code.

    And what does "doesn't quite do what I want" mean in this case? What exactly is the difference between the behavior this gives you, and what you want?
     
  4. ricardoaffonso

    ricardoaffonso

    Joined:
    Feb 21, 2018
    Posts:
    17
    Sorry this is my first time posting here obviously,
    Well this doesn't include pathfinding, and it can just fly out of the map
     
  5. ricardoaffonso

    ricardoaffonso

    Joined:
    Feb 21, 2018
    Posts:
    17
    So this is what I have now, it makes the bird fly around randomly and when it's facing the centre of the scene it doesn't change direction too much, meaning it flies around but doesn't go too far.

    Is there any way to give it the ability to dodge trees while maintaining most of this or maintaining its functionality?

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class flight : MonoBehaviour {
    7.  
    8.     Vector3 target;
    9.     bool facing = false;
    10.  
    11.     Rigidbody rb;
    12.     Vector3 m_EulerAngleVelocity;
    13.  
    14.     float timeToChange = 0;
    15.     float angle;
    16.  
    17.     public float speed = 7f;
    18.     public float notFacingTurnRate = 15f;
    19.     public float facingTurnRate = 2f;
    20.     public float maxChangeDirectionTime = 1000f;
    21.  
    22.     void Start () {
    23.         findValues();
    24.         findTarget();
    25.     }
    26.     void findValues(){
    27.         timeToChange = Random.Range(0, maxChangeDirectionTime);
    28.         if (!facing)
    29.         {
    30.             angle = Random.Range(-notFacingTurnRate, notFacingTurnRate);
    31.         }else{
    32.             angle = Random.Range(-facingTurnRate, facingTurnRate);
    33.         }
    34.         Debug.Log(angle);
    35.     }
    36.  
    37.     void findTarget(){
    38.         target = new Vector3(0f,0f,0f);//(Random.Range(0F, 300.0F), 0, Random.Range(0F, 300.0F));
    39.         Debug.Log(target);
    40.     }
    41.  
    42.     private void Update()
    43.     {
    44.         timeToChange -= 1;
    45.         if(timeToChange<=0){
    46.             findValues();
    47.         }
    48.  
    49.  
    50.         if (Vector3.Angle(transform.forward, target - transform.position) < 10f){
    51.             facing = true;
    52.             Debug.Log("facing");
    53.             angle = 0;
    54.         }else{
    55.             facing = false;
    56.         }
    57.     }
    58.  
    59.     void FixedUpdate()
    60.     {
    61.         rb = GetComponent<Rigidbody>();
    62.         rb.MovePosition( transform.position + transform.forward * Time.deltaTime*speed);
    63.  
    64.         m_EulerAngleVelocity = new Vector3(0, angle, 0);
    65.  
    66.         Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * Time.deltaTime);
    67.         rb.MoveRotation(rb.rotation*deltaRotation);
    68.         //transform.LookAt(target); this bad
    69.         //Debug.Log("flyaway");
    70.     }
    71.  
    72.  
    73.  
    74. }
    75.  
     
  6. ricardoaffonso

    ricardoaffonso

    Joined:
    Feb 21, 2018
    Posts:
    17
    what? Sorry your comment was very hard to understand.
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    His comment's hard to understand because it's spam. Ignore it (I've already reported him).

    So, yeah, sounds like you're off to a good start. Dodging trees could be added with something like this:

    1. Cast a ray ahead of your bird. If it hits nothing, full speed ahead!
    2. If it hits something, let's see... maybe look at the angle (in the XZ plane) between your forward direction, and the line between the bird and the center of the tree you found. That will tell you if you're to the left of the center of the tree, or to the right. Steer a little further in that same direction.

    I mean, if step 2 seems complicated, you can start without it, and have your birds simply always turn to the right if there's a tree ahead. But it won't look as cool as having them turn in whichever direction is shorter (as real birds would do). So you'll want to add that next.
     
    ricardoaffonso likes this.
  8. ricardoaffonso

    ricardoaffonso

    Joined:
    Feb 21, 2018
    Posts:
    17
    Yep that was simply perfect. Thank you very much for your feedback!
     
    JoeStrout likes this.