Freeze Gun

i made a particle gun to freeze people, i have one problem. i can’t make enemies freeze. is there a way to make the enemies slow down at set amount till they can’t move? :frowning:

Your enemy should move by a Time.deltaTime parametric algorithm. So, you can have a variable like timeScale but independent for every enemy:

private var OwnTimeScale = 1.0;

Now, the movement function should multiply the velocity per deltaTime per OwnTimeScale:

transform.position += direction * velocity * Time.deltaTime * OwnTimeScale;

You can access now the OwnTimeScale by scripting from 1.0 to 0.0 to make it slow down till freeze. And after a while to 1.0 again to make it walk again.

.ORG

1 Like

i don’t know how to set that up.

What Omar means is:(I think)

private var OwnTimeScale = 1.0;
function OnCollisionEnter(){
//I dont know how to test that the collision is between the enemy/player and the particle projectile-anyone?Just try this anyway


transform.position += direction * velocity * Time.deltaTime * OwnTimeScale;
}

That doesnt sort the objects, but it shows what to do next by putting it inside the OnCollisionEnter function. When someone throws a line of code, try to figure out what function it belongs to from the inbuilt ones. You can make your own functions, but they just take a bit to get setup. Cool Ay?
Try it. Make sure you’ve got a particle collider attatched to freeze gun particles :wink:
HTH
AC

Put this on an enemy and follow the comments.

var mFreezingSpeed = 1.0; // Seconds to freeze

private var mFreezing = false; // Is freezing or not
private var mTimeScale = 1.0; // Own time scale
private var mFreezeTime = 5.0; // How many seconds left to recover from freezing, use this in case the enemy need to move again after some seconds

private static var FT = 5.0; // Duration of freezing effect

function Update ()
{
	if ( mFreezing )
	{
		// Diminish local time scale until reach 0.0
		if ( mTimeScale > 0.0 )
		{
			mTimeScale -= (1.0 / mFreezingSpeed) * Time.deltaTime;
			
			if ( mTimeScale < 0.0 )
				mTimeScale = 0.0;
		}
		else
		{
			mFreezeTime -= Time.deltaTime;
			
			if ( mFreezeTime <= 0 )
			{
				UnFreeze ();								
			}
		}

	}
	
	
	// Your code to move the enemy
	// ...
	
	// Calculate movement direction
	var movement = Vector3 (0, 0, 1);	
	// End of your code
	
	
	// Finally move the enemy
	transform.position += movement * Time.deltaTime * mTimeScale;
}


function OnCollisionEnter () // Or whatever code you have to check an event when the enemy starts freezing
{
	Freeze ();
}


function Freeze () // Start freezing
{
	mFreezing = true;
	mFreezeTime = FT;	
}

function UnFreeze () // Stop freezing, called automatically when mFreezingTime reach 0, or arbitrarily by an script
{
	mFreezing = false;
	mTimeScale = 1.0;
	mFreezeTime = 0.0;
}

Untested!

.ORG

1 Like

the game accepts the script but how does it register the freeze gun?

This part is a big hint:

function OnCollisionEnter () // Or whatever code you have to check an event when the enemy starts freezing
{
   Freeze ();
}

It will freeze when anything hits it or when it hits anything.

If you don’t understand that script and yet you are trying slightly more advanced topics like this, I recommend taking a break and doing the scripting tutorials. Also if you have a quick question a good place to ask is on the IRC channel:

#otee-unity on irc.freenode.net

i don’t know where to get (or whatever) that kind of script… and i don’t know where to find “scripting tutorials”. :cry:

http://unity3d.com/documentation.html is a good place to start.

I see that you are crying and I’d like to help. I don’t understand what you are asking for, though.

There isn’t a big gigantor repository of scripts pre-made for every possible game situation there ever could be. There is a wiki that has a bunch of great and useful scripts and shaders and so on, and that can help save time, but you are either going to need to learn to make scripts yourself or hire/coerce someone to do it for you.

This community is very generous when it comes to scripts and code and helping, but you’ll see that people quickly tire of just producing specific scripts unless the person using them is also trying to LEARN to make their own.

Hopefully, you’ve already started! :slight_smile:

Have you completed the tutorials just yet? If not then I highly recommend them as a way to get familiar with these sorts of issues. You’ll ultimately have to take the code others have generously provided and mold them to the exact shape you want because each game is unique and so it will be hard to create a one-size-fits-all freeze gun script for you. :stuck_out_tongue:

Tutorials

Note that most of the tutorials list a time to complete (2 hours or so), none of them took me quite that long so don’t be put off by that sort of thing. :slight_smile:

Edit: and to comment a bit further, before worrying about freezing people, have you gone so far as to have a gun that fires and registers when it hits an opponent? If not then don’t yet worry about freezing them as you should first get the firing-hit detection part down first (which is covered in the FPS tutorial at the link above).

Thank you so much!!! I’ve been stuck on my freeze gun for over a week and I finally got it together thanks to this!