Hi,
I’m hoping that someone can help me.
I am new to Unity Javascript so the solution may be easy or obvious.
I am trying make the camera move randomly to look at one of a group of 2D planes.
The planes depict events in the game world and are called event01, event02, etc.
Thanks in advance!
Yours
Miriam
ps. my attempt so far:
var targetCamera : Transform;
function OnMouseDown () {
//set up array of all possible events
var allPossibleEvents : Transform[ ];
var allPossibleEvents.Push(event01);
var allPossibleEvents.Push(event02);
var allPossibleEvents.Push(event03);
//etc. continues up to 24
//choose randomly from list
var x : int;
x = Random.Range (1 : int, allPossibleEvents.length);
x = x-1;
var nextEvent = allPossibleEvents[×];
//set camera over event
targetCamera.position.x = nextEvent.position.x;
targetCamera.position.z = nextEvent.position.z;
//look at event
targetCamera.LookAt(nextEvent);
}
Does the code you have not work? What exactly do you need help with? Using Random.Range() to get a random array position seems like an acceptable solution.
Hi,
thanks for replying,
I need help because it’s not working and I can’t figure out what I’m doing wrong. I think it’s just small syntax things rather than the basic approach, but any suggestions are appreciated.
I’m getting errors when I try preview it.
I think the problems are with these two lines, but I’m not sure how I should write them:
is this how you add the transform of the event01 object to the array?.
do I need both “x : int” and “1 : int”? or do I just need the first one. (ie, x is an integer so the random number chosen will be an integer)
thanks
Miriam
What errors are you getting? That’s always the easiest way to figure out what’s going on.
Two things:
- You probably want to use the Array class rather than builtin arrays. That is, create a new array with var allPossibleEvents = new Array(). This will allow you to use the Push() function.
- When you add a transform to the array, you don’t need to put var in front of it. That would suggest you’re creating a new variable, which you’re not.
Specifying a variable’s type isn’t absolutely necessary in JavaScript, but in my opinion it’s a good practice. So x : int is just fine.
1 : int on the other hand will throw an error. You can’t specify a parameter’s type when supplying it to a function. 1 is an integer, and the compiler knows that. x = Random.Range (1, allPossibleEvents.length); is the code you want.
Thanks heaps for your help.
I’t’s working now.
The fixed code in case it helps anyone else:
setNextEvent.js
(used to randomly change the nextEvent)
static var nextEvent : Transform;
function Update () {
var allPossibleEvents = new Array ();
allPossibleEvents.Add(GameObject.Find(“event01”));
allPossibleEvents.Add(GameObject.Find(“event02”));
allPossibleEvents.Add(GameObject.Find(“event03”));
allPossibleEvents.Add(GameObject.Find(“event04”));
allPossibleEvents.Add(GameObject.Find(“event05”));
allPossibleEvents.Add(GameObject.Find(“event06”));
//etc
var x = Random.Range(0, allPossibleEvents.length -1);
nextEvent = allPossibleEvents[×].transform;
}
goToEvent.js
(attached to the trigger object)
var targetCamera : Transform;
function OnMouseDown () {
var event : Transform = setNextEvent.nextEvent;
targetCamera.position.x = event.transform.position.x;
targetCamera.position.z = event.transform.position.z;
targetCamera.position.z -= 12;
targetCamera.LookAt(event);
}