Question by
Snakox · Aug 02, 2016 at 03:05 PM ·
scripting problemmovement scriptfly
fly movement
Hello,
This is my first time on unity and I have some probleme in this script ( the fly can't fly in all the area) #pragma strict
var displacement:Vector3 = Vector3(5,2,5);
var desplVel:float = 1;
private var pos0:Vector3;
private var iniRndX:float;
private var iniRndY:float;
private var iniRndZ:float;
private var audioOn:boolean=false;
private var audioSourceMax:int = 4;
private var audioSourceCurr:int = 0;
function Start () {
iniRndX = Random.Range(Random.Range(0,200), 500);
iniRndY = Random.Range(Random.Range(0,200), 500);
iniRndZ = Random.Range(Random.Range(0,200), 500);
desplVel *= Random.Range(0.5, 2);
pos0 = transform.localPosition;
transform.localEulerAngles.y = Random.Range(1,360);
}
function Update() {
//// Delete this Lines if you haven't an Audio Source attached to the GameObject
if (!audioOn){
if (Random.value < 0.01) {
if(CheckAudioSourcesMax()) {
GetComponent.<AudioSource>().Play();
audioOn= true;
} else {
GetComponent.<AudioSource>().enabled = false;
audioOn = true;
}
}
}
////
}
function FixedUpdate () {
UpdateDespl();
}
function UpdateDespl() {
var x:float = (Mathf.PerlinNoise(Time.time*desplVel +iniRndX, iniRndX) -0.46525) *displacement.x;
var y:float = (Mathf.PerlinNoise(iniRndY, iniRndY+ Time.time*desplVel) -0.46525) *displacement.y;
var z:float = (Mathf.PerlinNoise(iniRndZ, iniRndZ+ Time.time*desplVel) -0.46525) *displacement.z;
var despl:Vector3 = Vector3(x,y,z);
var rot:Quaternion = Quaternion.LookRotation(despl, Vector3.up);
rot.eulerAngles.x = 270;
rot.eulerAngles.z = 0;
rot.eulerAngles.y += 90;
GetComponent.<Rigidbody>().AddForce(despl, ForceMode.Force);
transform.rotation = Quaternion.Lerp(transform.rotation, rot, 0.09);
if ((transform.localPosition-pos0).sqrMagnitude>displacement.sqrMagnitude) {
GetComponent.<Rigidbody>().AddForce((pos0 -transform.localPosition)*2, ForceMode.Force);
}
}
function CheckAudioSourcesMax():boolean {
var maxNum:int = 0;
var flyScripts:FlyBehav[] = FindObjectsOfType(FlyBehav);
for(var flyScript:FlyBehav in flyScripts) {
maxNum = Mathf.Max( flyScript.IncreaseAudioSourcesCurr(), maxNum);
}
if(maxNum<=audioSourceMax) return true;
else return false;
}
function IncreaseAudioSourcesCurr():int {
audioSourceCurr ++;
return audioSourceCurr;
}
and I would like to know how to add a limit border to stay on the screen( maxX, minX, maxY, MinY) and the fly can fly in all the area like in this code
#pragma strict
var maxX = 6.17;
var minX = -6.06;
var maxY = 3.08;
var minY = -5.63;
private var tChange: float = 0; // force new direction in the first Update
private var randomX: float;
private var randomY: float;
var moveSpeed = 10;
function Start () {
}
function Update () {
// change to random direction at random intervals
if (Time.time >= tChange){
randomX = Random.Range(-2.0,2.0); // with float parameters, a random float
randomY = Random.Range(-2.0,2.0); // between -2.0 and 2.0 is returned
// set a random interval between 0.5 and 1.5
tChange = Time.time + Random.Range(0.5,1.5);
}
transform.Translate(Vector3(randomX,randomY,0) * moveSpeed * Time.deltaTime);
// if object reached any border, revert the appropriate direction
if (transform.position.x >= maxX || transform.position.x <= minX) {
randomX = -randomX;
}
if (transform.position.y >= maxY || transform.position.y <= minY) {
randomY = -randomY;
}
// make sure the position is inside the borders
transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX);
transform.position.y = Mathf.Clamp(transform.position.y, minY, maxY);
}
Can anyone help me please ?
Thank's
Comment