Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

Sign in to follow this  
geckoo

Tips tips tips (and more)

Recommended Posts

Hi Fellows! Sometimes we use always the same functions or commands in order to code an action or a behavior. These lines are often used in scripts, but it is possible to optimize code. I would like to share some tips here just to start a new section. If you have an useful idea (maybe just one line) and if you want to share it with us, give us a brief explanation and why this alternative is better than the previous. Don't forget that sometimes the simplest things are better than hardest. Best regards ++

  • Like 2

Share this post


Link to post
Share on other sites

Don't use Vector3.Distance to check distance between two objects. It could be more efficient to use an alternative.

Here an explanation about this tip - Please login or register to see this link.

Please login or register to see this code.

Share this post


Link to post
Share on other sites

One of the best way to play all sounds in a project. Create an empty object which uses Please login or register to see this link. . Use the function below.

Then find it as a reference using Please login or register to see this link. in your scripts which have sounds.

One object, one AudioSource, one function... All sounds in my project Please login or register to see this link. use this way. It allows easily to play variations with Random.Range ++

Please login or register to see this code. Please login or register to see this code.

Share this post


Link to post
Share on other sites

There is a little bug on the FPSController which is embedded in Unity. When you jump (exactly when you are jumping) if you press again the space bar, so touching the ground, the player jumps again. This is not litterally a bunny jump, but obviously this behavior is not required. There is something wrong. In order to fix this bug, you must check another condition. Take a look at the script FirstPersonController - line 66. Add a parameter which checks events - so when the player is jumping, it doesn't allows any jump. See below :

Please login or register to see this code.

Share this post


Link to post
Share on other sites

I'll throw in some things that I see very frequently. I know a lot of ppl get too familiar with using things like this:

 

someVector = new Vector3(...);

 

which is all fine and dandy when your only using it on occasions. But if you are using it every frame, it can be slightly taxing. What ppl aren't familiar with and you don't see to often is the Set method. The set method allows you to set a vector without creating unnecessary instantiations of a class (garbage collection) and is used just as easy:

 

someVector.Set(...);

 

another garbage collector is the heavy use of things like "transform" or "gameobject". Things like that need to be cached whenever possible. What new unity users don't realize is that those arent typical variables, they return copies of the original that can tax your CPU similar to the example above with vectors. Instead, cache it once and use it to avoid garbage collection like this:

 

private var t : Transform;

 

function Start(){t = transform;}

function Update()

{

t.position.x += 0.0001;

}

 

similarily, position is also a copied vector as is rotation and scale. You can tell because the Set method doesn't work... bc you can't set a copied class because it's thrown away. There is nothing we can do about that at the moment as unity has yet to add a method for it (as of unity 4 atleast, haven't heard otherwise for unity 5 so I assume it hasn't changed).

  • Like 2

Share this post


Link to post
Share on other sites
Please login or register to see this quote.

 

From experience i can say - always do your own tests. He did test in 2013 (unity 4), but in unity 5 results can be opposite.
Like in this case Vector3.Distance is faster than your example, but difference is very small, so it doesn't really matter how you will check distance - you will not feel any difference.

 

And instead of GameObject.Find() better:

Please login or register to see this code.

 

Then from any script you can:

Please login or register to see this code.

 

Share this post


Link to post
Share on other sites

Another good method I like to use, primarily for larger amounts of orientating objects in the scene like coins or animated foliage or tv uv animations for multiple tvs or similar, is using the OnWillRender method instead of the update method. Update will run reguardless of culling while OnWillRender will only be called if the mesh will be rendered that frame.

 

so let's say you have 500 coins in a level and each coin is rotating or even animated. Instead of having an animation component or update loop running on every coin in the scene, have one coin (the master coin) doing the animation or update and every other coin mimic its orientation if it's going to be rendered. Here is how it's done:

 

 

public var masterCoin : Transform;

 

//Don't forget to cache this transform

private var t : Transform;

 

function Start(){t = transform;}

 

function OnWillRender()

{

t.rotation = masterCoin.rotation;

}

Share this post


Link to post
Share on other sites
Please login or register to see this quote.

Hi OMA. Of course, I talk about my own experience. I did many tests using Vector3.Distance or its alternatives which we quoted. My project Please login or register to see this link. uses many mechanics - and one of them allows to rescale some objects according player distance (2'02''). In another scene, with more than 2500 objects (a grid 50x50) I had some bad result using Vector3.Distance checking colliders. For me, magnitude value was very more smooth and efficient. I have no doubt about your skill, but I noticed something else. It could be interesting to check them ++

Share this post


Link to post
Share on other sites
Please login or register to see this quote.

Thank you OcularCash. I have never heard anything about OnWillRender() :)

Share this post


Link to post
Share on other sites

A simple function to control automatically all colliders which has been created with a ragdoll.

It allows to set tag and isKinematic value using a boolean. It uses only one tag, but I assume that it could be improved for head shot and more ++

Please login or register to see this code.

Share this post


Link to post
Share on other sites

Maybe you know that, but I only today understand that we can use rich text :

 

Debug.Log("<color=red>Alert message</color>");

Share this post


Link to post
Share on other sites

Mathf.Lerp linearly interpolates between a and b by time - and our object goes slowly at the end.

To get the same speed between a and b, we can increase it using percentage accomplished (0 to 1) ++

Please login or register to see this link.

 

Please login or register to see this code.
  • Like 1

Share this post


Link to post
Share on other sites

StopCoroutine("CoroutineName") doesn't stop a coroutine on its own. You have to cache it and stop the cached routine.

Otherwise you'll just create a copy of the named routine and stop that one.

Please login or register to see this code.

 

  • Like 1

Share this post


Link to post
Share on other sites

Cache your WaitForSeconds() to reduce garbage collection.

Please login or register to see this code.

 

Edited by UnityGamesRoland
  • Like 1

Share this post


Link to post
Share on other sites

A little function which is really useful - just to check in a string some forbidden characters and its lenght ++

Please login or register to see this code.

Share this post


Link to post
Share on other sites
Please login or register to see this quote.

Actually I am working on a game which integrates many coroutines. For this reason I tested different ways so as to reduce ressource wasting. When you use Unity's default coroutines you will have to be very careful about how you use them or else they will allocate memory that the garbage collector later has to clean up. So  I assume that your explaination is good. However testing different ways I noticed that the best way to use coroutines is this one - MEC (free version) :

Please login or register to see this link.

Easy and intuitive I saved many resources - and now I don't use anymore the classical Unity coroutine process...

If you want to optimize your project I recommend it ++

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...