unity ai wander script
C#
var Speed= 20;
var wayPoint : Vector3;
var Range= 10;
function Start(){
//initialise the target way point
Wander();
}
function Update()
{
// this is called every frame
// do move code here
transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
if((transform.position - wayPoint).magnitude < 3)
{
// when the distance between us and the target is less than 3
// create a new way point target
Wander();
}
}
function Wander()
{
// does nothing except pick a new destination to go to
wayPoint= Vector3(Random.Range(transform.position.x - Range, transform.position.x + Range), 1, Random.Range(transform.position.z - Range, transform.position.z + Range));
wayPoint.y = 1;
// don't need to change direction every frame seeing as you walk in a straight line only
transform.LookAt(wayPoint);
Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}using UnityEngine;
using System.Collections;
/// <summary>
/// Creates wandering behaviour for a CharacterController.
/// </summary>
[RequireComponent(typeof(CharacterController))]
public class Wander : MonoBehaviour
{
public float speed = 5;
public float directionChangeInterval = 1;
public float maxHeadingChange = 30;
CharacterController controller;
float heading;
Vector3 targetRotation;
void Awake ()
{
controller = GetComponent<CharacterController>();
// Set random initial rotation
heading = Random.Range(0, 360);
transform.eulerAngles = new Vector3(0, heading, 0);
StartCoroutine(NewHeading());
}
void Update ()
{
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval);
var forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * speed);
}
/// <summary>
/// Repeatedly calculates a new direction to move towards.
/// Use this instead of MonoBehaviour.InvokeRepeating so that the interval can be changed at runtime.
/// </summary>
IEnumerator NewHeading ()
{
while (true) {
NewHeadingRoutine();
yield return new WaitForSeconds(directionChangeInterval);
}
}
/// <summary>
/// Calculates a new direction to move towards.
/// </summary>
void NewHeadingRoutine ()
{
var floor = transform.eulerAngles.y - maxHeadingChange;
var ceil = transform.eulerAngles.y + maxHeadingChange;
heading = Random.Range(floor, ceil);
targetRotation = new Vector3(0, heading, 0);
}
}
Also in C#:
- Title
- c# check if string is all numbers
- Category
- C#
- Title
- c# how to exit program
- Category
- C#
- Title
- unity load scene
- Category
- C#
- Title
- c# enum default
- Category
- C#
- Title
- unity button interactable
- Category
- C#
- Title
- C# Cast double to float
- Category
- C#
- Title
- unity custom update
- Category
- C#
- Title
- unity deactive all object in list
- Category
- C#
- Title
- convert string to decimal c#
- Category
- C#
- Title
- unity 2d platformer movement script c#
- Category
- C#
- Title
- unix time c#
- Category
- C#
- Title
- c# find element in list of list
- Category
- C#
- Title
- Check object is in layermask unity
- Category
- C#
- Title
- Unity3d GPS code
- Category
- C#
- Title
- c# number suffixes
- Category
- C#
- Title
- convert string array to int C#
- Category
- C#
- Title
- unity quaternion
- Category
- C#
- Title
- c# get every point in a line in matrix
- Category
- C#
- Title
- bool toggle unity c#
- Category
- C#
- Title
- c# properties
- Category
- C#
- Title
- c# arraylist contains
- Category
- C#
- Title
- F# convert generic.List to list
- Category
- C#
- Title
- Assets/Scripts/Snake.cs(177,25): error CS1061: Type `Snake.SnakeBodyPart' does not contain a definition for `SetGridPostion' and no extension method `SetGridPostion' of type `Snake.SnakeBodyPart' could be found. Are you missing an assembly reference?
- Category
- C#
- Title
- while loop in c#
- Category
- C#
- Title
- c# array inst working
- Category
- C#
- Title
- c# remove spaces from string
- Category
- C#
- Title
- c# get time
- Category
- C#
- Title
- exception handling c# stack overflow
- Category
- C#
- Title
- resize image c#
- Category
- C#
- Title
- public enum c#
- Category
- C#
- Title
- unity object array
- Category
- C#
- Title
- c# reverse list
- Category
- C#
- Title
- c# read all text from a file
- Category
- C#
- Title
- displayname c#
- Category
- C#
- Title
- open file in explorer c#
- Category
- C#
- Title
- unity rotate object relative to camera
- Category
- C#
- Title
- c# dictionary add
- Category
- C#
- Title
- c# resize bitmap
- Category
- C#
- Title
- c# generic abstract method
- Category
- C#
- Title
- enums as numbers c#
- Category
- C#
- Title
- C# graph api upload file one drive
- Category
- C#
- Title
- how does Pow work C#
- Category
- C#
- Title
- iframe set html content c#
- Category
- C#
- Title
- unity serializefield
- Category
- C#
- Title
- disable a component unity
- Category
- C#
- Title
- exit game unity
- Category
- C#
- Title
- how to access first child of parent unity
- Category
- C#
- Title
- debug.log
- Category
- C#
- Title
- unity how to change text in script
- Category
- C#
- Title
- string to guid c#
- Category
- C#
- Title
- parsing string to int c#
- Category
- C#
- Title
- first sentence letter capital in c#
- Category
- C#
- Title
- c# get current directory xamarin
- Category
- C#
- Title
- value is null to insert in c#
- Category
- C#
- Title
- how to create a list in c# unity
- Category
- C#
- Title
- autoresetevent
- Category
- C#
- Title
- c# get binary array from int
- Category
- C#
- Title
- freeze position unity c#
- Category
- C#
- Title
- get waht is differnt between two arrays c#
- Category
- C#
- Title
- unity find object by name
- Category
- C#
- Title
- C# check if is first run
- Category
- C#
- Title
- c# download file
- Category
- C#
- Title
- cc# sort list with list if ids
- Category
- C#
- Title
- reference to gameobject in different scene unity
- Category
- C#
- Title
- unity get velocity at point
- Category
- C#
- Title
- how to cjeck if a string has a word c#
- Category
- C#
- Title
- c# arrays of arrays
- Category
- C#
- Title
- c# odd even median
- Category
- C#
- Title
- print gridcontrol devexpress
- Category
- C#
- Title
- .net core get image from url
- Category
- C#
- Title
- clear combobox c#
- Category
- C#
- Title
- c# max two values
- Category
- C#
- Title
- c# shorten an method
- Category
- C#
- Title
- length of a string c#
- Category
- C#
- Title
- c# creating a data recovery software
- Category
- C#
- Title
- Assets/Scripts/Snake.cs(187,10): error CS0029: Cannot implicitly convert type `UnityEngine.Vector2Int' to `System.Collections.Generic.List<UnityEngine.Vector2Int>'
- Category
- C#
- Title
- c# check if type implements interface
- Category
- C#
- Title
- C# string is all zeros
- Category
- C#
- Title
- c# reverse string
- Category
- C#
- Title
- compile in one single exe c#
- Category
- C#
- Title
- unity check if gameobject is active
- Category
- C#
- Title
- loop datagridview c#
- Category
- C#
- Title
- lista generica como parametro de un metodo en c#
- Category
- C#
- Title
- c# md5 string
- Category
- C#
- Title
- what is a return statement C#
- Category
- C#
- Title
- Request.Form
- Category
- C#
- Title
- mongodb c# batch find
- Category
- C#
- Title
- Request.ServerVariables["HTTP_X_FORWARDED_FOR"] get only one ipaddress
- Category
- C#
- Title
- get enum value c#
- Category
- C#
- Title
- parse strings into words C#
- Category
- C#
- Title
- c# get country code
- Category
- C#
- Title
- how to change player cursor c# script unity
- Category
- C#
- Title
- c# stop loop in method
- Category
- C#
- Title
- x angle oes back after 90 unity
- Category
- C#
- Title
- c# docstring
- Category
- C#
- Title
- set width of rect transform unity
- Category
- C#
- Title
- c# to vb.net
- Category
- C#
- Title
- Celsius to Fahrenheit c#
- Category
- C#
- Title
- c# build string out of list of strings
- Category
- C#
- Title
- c# filter non alphanumeric characters
- Category
- C#