到目前为止,我正在制作一个应用程序,它的早期阶段只是一条鱼在一个小盒子周围游动。我制作了随机改变鱼类方向的代码,如果它达到某些x或y值,则使其保持在相机的可见度范围内。不幸的是我的C#知识是有限的,我无法弄清楚如何让它改变它的方向,这是我的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FishBehavior : MonoBehaviour {
public float speed = 2f;
float moveSpeed = 0f;
private Rigidbody2D rigid;
public float rotateSpeed = 2f;
public bool isPredator = false;
void Start () {
rigid = GetComponent<Rigidbody2D>();
rigid.velocity = new Vector2(speed, 0);
InvokeRepeating("FishMovement", 3f, Random.Range(2f,6f));
}
// Update is called once per frame
void Update () {
int num = Random.Range(1, 10);
if (transform.position.x >= 11.5)
{
speed = -2;
rigid.velocity = new Vector2(speed, 0);
}
if (transform.position.x <= -11.5)
{
speed = 2;
rigid.velocity = new Vector2(speed, 0);
}
if (transform.position.y >= 5.07)
{
rigid.velocity = new Vector2(Random.Range(-2, 2), -2);
}
if (transform.position.y <= -4.65)
{
rigid.velocity = new Vector2(Random.Range(-2, 2), 2);
}
}
void FishMovement()
{
int fishSpeed = Random.Range(-3, 3);
if (fishSpeed != 0)
{
moveSpeed = Random.Range(-2, 2);
rigid.velocity = new Vector2(fishSpeed, moveSpeed);
}
}
}
任何事情都有帮助谢谢