- Home /
Is there a better way to simulate a vacuum or kirby like suction that doesn't kill frame rate?
Hello,
I'm trying to create the shrink/suction mechanic from Kirby or Luigi's Mansion. The Main character has a vacuum that he uses as a gun. Right now, we are using a Raycast that generates when the player presses the right mouse button down. If the Raycast hits a collider with a RigidBody, The ray shrinks the object, and draws it closer to the player. Here's the code: The great thing is, it works. The problem is if there is more than one enemy on screen, the frame rate falls way down.
My Question: Is there a better way to simulate a vacuum or kirby link suction that doesn't kill frame rate? Any Help would be amazing! Thanks.
if(lastAtk + rateOfFire<=Time.time)
{
lastAtk=Time.time; //reset last time we attack to put gap between attacks. (AKA rateof fire)
print("bang!");
//...draw a Ray cast forward
var hit : RaycastHit;
ray = new Ray (arm.transform.position, (targetPoint-arm.transform.position).normalized);
Debug.DrawLine(arm.transform.position, targetPoint, Color.yellow,1);
if (Physics.Raycast (ray, hit, range))
{
//print(hit.transform);
//if ray collides with an enemy
var hitTag = hit.collider.tag;
if(hitTag=="Enemy")
{
//Debug.Log("enemy hit");
hit.transform.gameObject.SendMessage("ApplyDamage",10,SendMessageOptions.DontRequireReceiver);
print(transform.root.name+" attacked!");
}
//apply a force to the enemy to "suck"; them up
// if (hit.collider.attachedRigidbody)
// {
// var dir : Vector3 = arm.transform.position -hit.transform.position ;
// dir = dir.normalized;
// hit.collider.attachedRigidbody.AddForce((dir) * power);
// //shrinks enemy
// var locScale: Vector3 = hit.transform.localScale;
// if(locScale.x>=.1){hit.transform.localScale -= Vector3.one*Time.deltaTime*shrinkSpeed;}
// else{hit.transform.localScale=Vector3(.1,.1,.1);}
// }
}
}
Here is the 'shrink' part of the code
//apply a force to the enemy to "suck" them up
// if (hit.collider.attachedRigidbody)
// {
// var dir : Vector3 = arm.transform.position -hit.transform.position ;
// dir = dir.normalized;
// hit.collider.attachedRigidbody.AddForce((dir) * power);
// //shrinks enemy
// var locScale: Vector3 = hit.transform.localScale;
// if(locScale.x>=.1){hit.transform.localScale -= Vector3.one*Time.deltaTime*shrinkSpeed;}
// else{hit.transform.localScale=Vector3(.1,.1,.1);}
// }
Answer by VIDesignStudios · Dec 08, 2016 at 10:17 AM
How about dropping the raycast altogether and having a simple trigger box or sphere always out in front of the character, waiting for collisions with tagged enemies, and this trigger box collider only turns on / game object activates when the right mouse is pressed? After a hit is detected, you could remove the Rigidbody right before shrinking so it doesn't pain the physics engine.