Hello Everyone,
I currently have a first person underwater exploration game.
All of my fish and sharks use a type of raycast to detect if they get to close to a wall or not, or in the case of the shark, if they detect the player.
The problem is, the raycasting is SEVERLY lowering my games FPS.
Would anyone be nice enough to take a look at my scripts and help me understand how I can fix it to prevent it from lagging so much? It lags if I even have 5 or 6 sharks. If I look at the profiler it is maxing out my GPU, but reset back to normal once I turn the fish/sharks off.
Thank you for your help!
this is the sharks:
var animalSpeed: float;
var rotSpeed : float = 10;
var moveDir : int = 1;
var provoked : boolean = false;
var attacking : boolean = false;
var turning : boolean = false;
var sharkAnimator : Animator;
var sharkHitDetect :LayerMask;
var Player : Transform;
var sharkBite : GameObject;
var MoveSpeed = 4;
var MaxDist = 10;
var loseSight = 120;
var MinDist = 5;
function Start ()
{
sharkBite.SetActive(false);
sharkAnimator.SetBool("hurt", false);
sharkAnimator.SetBool("attacking", false);
sharkAnimator.SetBool("dying", false);
attacking = true;
}
function FixedUpdate()
{
///////////
if (Player == null)
{
Player = null;
provoked = false;
attacking = true;
turning = false;
}
if (provoked == false)
{
this.transform.localEulerAngles.x=0;
if(!Physics.Raycast(transform.position, transform.forward, 5))
{
moveDir = 1;
transform.Translate(Vector3.forward * animalSpeed * Time.smoothDeltaTime);
}
if(Physics.Raycast(transform.position, transform.forward, 5))
{
moveDir = 10;
transform.Translate(Vector3.forward * animalSpeed * Time.smoothDeltaTime);
}
if(Physics.Raycast(transform.position, (- transform.right), 1))
{
moveDir = 10;
}
if(Physics.Raycast(transform.position, transform.right, 1))
{
moveDir = -10;
}
transform.Rotate(Vector3.up, 90 * rotSpeed * Time.smoothDeltaTime * moveDir);
}
////////////////////////
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, MaxDist,sharkHitDetect)) {
Debug.DrawLine (transform.position, hit.point, Color.red);
if (hit.collider.tag == "Player" || hit.collider.tag == "ship")
{
provoked = true;
Player = hit.collider.transform;
}
}
else if (Physics.Raycast (transform.position, transform.forward, hit, loseSight,sharkHitDetect)) {
provoked = false;
Debug.DrawLine (transform.position, hit.point, Color.blue);
}
if (Player != null)
{
if (provoked == true)
{
if(Vector3.Distance(transform.position,Player.position) <= MinDist)
{
Attacking ();
moveDir = 0;
}
}
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
MoveSpeed = 5;
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
}
if(Vector3.Distance(transform.position,Player.position) >= MaxDist && (Vector3.Distance(transform.position,Player.position) >= MinDist))
{
provoked = false;
sharkAnimator.SetBool("attacking", false);
Player = null;
}
}
}
function Attacking ()
{
// attacking = true;
////////////
if (attacking == true)
{
sharkBite.SetActive(true);
sharkAnimator.SetBool("attacking", true);
MoveSpeed = 20;
transform.Translate(Vector3.forward * MoveSpeed*Time.deltaTime);
yield WaitForSeconds(3);
attacking = false;
sharkAnimator.SetBool("attacking", false);
sharkBite.SetActive(false);
turning = true;
}
if (turning == true && attacking == false)
{
MoveSpeed = 10;
var targetDir = Player.position - transform.position;
var step = 2 * Time.deltaTime;
var newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 2.0);
transform.rotation = Quaternion.LookRotation(newDir);
yield WaitForSeconds(3);
attacking = true;
}
if (turning == true && attacking == true)
{
turning =false;
attacking = true;
}
if (attacking == true && provoked == false)
{
sharkAnimator.SetBool("attacking", false);
}
}
this is the fish.
var animalSpeed: float;
var rotSpeed : float = 10;
var moveDir : int = 1;
function Start ()
{
}
function FixedUpdate()
{
if(!Physics.Raycast(transform.position, transform.forward, 5))
{
moveDir = 1;
transform.Translate(Vector3.forward * animalSpeed * Time.smoothDeltaTime);
}
if(Physics.Raycast(transform.position, transform.forward, 5))
{
moveDir = 10;
transform.Translate(Vector3.forward * animalSpeed * Time.smoothDeltaTime);
}
if(Physics.Raycast(transform.position, (- transform.right), 1))
{
moveDir = 10;
}
if(Physics.Raycast(transform.position, transform.right, 1))
{
moveDir = -10;
}
transform.Rotate(Vector3.up, 90 * rotSpeed * Time.smoothDeltaTime * moveDir);
}