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..
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"); } }
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?
Sorry this is my first time posting here obviously, Well this doesn't include pathfinding, and it can just fly out of the map
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): using System.Collections; using System.Collections.Generic; using UnityEngine; public class flight : MonoBehaviour { Vector3 target; bool facing = false; Rigidbody rb; Vector3 m_EulerAngleVelocity; float timeToChange = 0; float angle; public float speed = 7f; public float notFacingTurnRate = 15f; public float facingTurnRate = 2f; public float maxChangeDirectionTime = 1000f; void Start () { findValues(); findTarget(); } void findValues(){ timeToChange = Random.Range(0, maxChangeDirectionTime); if (!facing) { angle = Random.Range(-notFacingTurnRate, notFacingTurnRate); }else{ angle = Random.Range(-facingTurnRate, facingTurnRate); } Debug.Log(angle); } void findTarget(){ target = new Vector3(0f,0f,0f);//(Random.Range(0F, 300.0F), 0, Random.Range(0F, 300.0F)); Debug.Log(target); } private void Update() { timeToChange -= 1; if(timeToChange<=0){ findValues(); } if (Vector3.Angle(transform.forward, target - transform.position) < 10f){ facing = true; Debug.Log("facing"); angle = 0; }else{ facing = false; } } 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); //transform.LookAt(target); this bad //Debug.Log("flyaway"); } }
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.