- Home /
Question by
Niks4Linux · May 14, 2015 at 10:13 AM ·
c#movementgameobjectunity 4.6
Want to move 2d fish sprites in pond !
I am trying to move some 2d fish sprites in a pond.But they are not moving properly.I have attached same script to all fish objects.Can anybody tell me whats wrong with my code. Here is my code -
bool moveFish = true;
bool insideBounds = false;
public float speed =.5f;
float minY = -1.1f;
float maxY = 1f;
float minX = -2.1f;
float maxX = 2f;
void Start () {
Invoke("ChangeDirection",1);
}
void Update () {
if(!IsInsideTheBounds()){
if(insideBounds){
insideBounds = false;
transform.Rotate(new Vector3(0,0,-150));
Invoke("MakeReady",10f);
return;
}
}
if(moveFish)
transform.Translate(-Vector3.up * Time.deltaTime * speed);
}
void ChangeDirection(){
if(IsInsideTheBounds())
{
Debug.Log("IsInsideTheBounds true");
insideBounds = true;
transform.Rotate(new Vector3(0,0,Random.Range(-20,50)));
}
Invoke("ChangeDirection",10f);
}
bool IsInsideTheBounds(){
bool overlap = false;
if(transform.position.x > minX &&
transform.position.x < maxX &&
transform.position.y > minY &&
transform.position.y < maxY ){
overlap = true;
}
return overlap;
}
void MakeReady(){
insideBounds = true;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613195548im_/https://answers.unity.com/themes/thub/images/avi.jpg)