I’m new to programming, so excuse my ignorance on what is probably a mish-mash of code. I Have done a lot of searching through questions etc. And haven’t been able to find an answer.
I’m trying to make game characters wander, stop & turn to look at target, then start wandering again by modifying [this][1] unity script.
So far I can’t seem to get it to work…
Currently, Unity is telling me: Unexpected symbol `PictureTransforms’ (in the else statement in the update method).
Is it because I’m trying to put a method inside a method?
Any help I could get on this would be hugely appreciated.
My Code is Below:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(CharacterController))]
public class Wander : MonoBehaviour
{
public float speed = 5;
public float directionChangeInterval = 3;
public float maxHeadingChange = 75;
public List<Transform> PictureTransforms;
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 Start()
{
foreach(GameObject picture in GameObject.FindGameObjectsWithTag("Picture")) {
PictureTransforms.Add(picture.transform);
}
}
Transform FindClosestEnemy(List<Transform> PictureTransforms){
Transform bestTarget = null;
float closestDistanceSqr = Mathf.Infinity;
Vector3 currentPosition = transform.position;
foreach (Transform potentialTarget in PictureTransforms)
{
Vector3 directionToTarget = potentialTarget.position - currentPosition;
float dSqrToTarget = directionToTarget.sqrMagnitude;
if (dSqrToTarget < closestDistanceSqr)
{
closestDistanceSqr = dSqrToTarget;
bestTarget = potentialTarget;
}
return bestTarget;
}
}
void Update ()
{
if (speed > 0){
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval);
} else {
transform.LookAt(FindClosestEnemy(List<Transform> PictureTransforms).position, Vector3.up);
}
var forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * speed);
}
IEnumerator NewHeading ()
{
while (true) {
NewHeadingRoutine();
yield return new WaitForSeconds(directionChangeInterval);
}
}
void NewHeadingRoutine ()
{
var floor = Mathf.Clamp(heading - maxHeadingChange, 0, 360);
var ceil = Mathf.Clamp(heading + maxHeadingChange, 0, 360);
heading = Random.Range(floor, ceil);
targetRotation = new Vector3(0, heading, 0);
}
}
NOTE: I haven’t put in a method to change speed at different intervals yet, as I’m just experimenting by changing the speed value in the inspector during runtime at the moment.