Search Unity

Performance Enhancing

Discussion in 'Editor & General Support' started by AaronC, Jun 1, 2006.

  1. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hey Guys,
    I have just done a nice virtual gallery where if you fire at a painting it takes you to another"room"
    I've got a few things like particle effects and some hi res wave files. (stereo16bit) Basically things are slowing down, and on my windows webplayer version, it even crashes. I have two questions:
    Where are the sound files held during gameplay?-If I have a 50mb song and a 64mb video card, and the audio is temp stored on the video card, then Im in the stcky stuff, right? so theroretically reducing soundfile size will help(?)Maybe the audio is played from the webfile location, and not from the videocard?and therefore reducing audiofilesize wont change anything?
    Q number2: How do I use the object destroy, -I have a shootball thing goin on, and my level fulls up with balls. If I use a timed object destroy script, when the designated time is reached, I cannot shoot any more balls. I really need the clones to have timed object destroy on them, not my source ball...

    Just trying to get better performance, if anyone has any tips?
    The school web browser filter has started blocking my visits to otee.dk so my call and response will be a bit slow. Thanks everyone.
    AC
     
  2. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    Audio should be in .ogg that will help size, and audio is not stored on the graphics card, thus the name. :p I don't think audio will affect performance at all, not really sure though. If it does its very little.

    Bill
     
  3. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    To destroy the clone instead of the original do something like...

    Code (csharp):
    1.  
    2. theClone = Instantiate (theOriginal,transform.position,Quaternion.identity);
    3. Destroy (theClone,time);
    4.  
    I imagine one single stereo audio shouldn't be a big deal (no matter what the size), but if you had several audio clips/channels going at once I can see that starting to impede performance. But I bet that the fact that all your shooting objects are never destroyed is the reason you're seeing a performance slowdown.
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hey thanks
    I tried yer code, Antennas' but to no avail. I assume its Java rather than boo or c#First it says some thing about "time" so I changed that to a value(2) then it says "Identity "is not part of Unity Quaterion, so I've tried changing Identity to sphere, no luck there, so Im stuck. Any tips?I just copy and paste the code you gave me and it should work?
    Thanks in adv. :roll:
     
  5. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Firstly the APIs are same in both JavaScript and C#, the ony difference is in the syntax ans handling of types.

    Secondly it's Quaternion.identity (note, lowercase i). I have no idea why you substituded sphere instead.
     
  6. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Ok
    If someone was dying I'd try to save them
    Unless they were nasty b@#ards

    Same w code -I'll backhack it somehow

    I made the adjustment
    Now it says position is not a member of UnityEngine Transform
    ?
     
  7. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    I bet it should just be spelled correctly :) transform.position The original snippet has it spelled as "postion"
     
  8. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Ok freyr, typo fixed :wink:
     
  9. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Targos, the code snippet was just a generic representation of how to destroy the cloned object instead of the original. Check out the "Fire Projectile Explosion" scene in the "Script Tutorial" project to see a full explanation of how to destroy clones once they hit their target.

    HTH
     
  10. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Ok cool. Its clear that not only that I need to learn code, but I want to...
    Thanks Antenna, I will follow yr advice and have another swipe at the script tut. If anyone has a suggeston on a website where I can start learning code from the absolute beginner point of view, then that would be gratefully appreciated...
    Getting to grips with code would be handy.
    AC
     
  11. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Hey Targos,
    I don't have any good links for you unfortunately, I did a quick google on it and it is difficult to find JaveScript tutorials that aren't web-oriented.

    It is easier to find C# tutorials that are more easily applied to Unity, but I really don't recommend you learn C# as your first ever programming language. JS is the way to go there.

    You don't need to learn the browser-specific stuff, just the basics of the syntax and how basic programming works. You can learn a lot by looking at other people's code and trying little changes to see what they do. You don't want to start out writing scripts until you can modify other people's scripts effectively.

    Good luck and don't get discouraged...it's worth it ;)

    -Jeremy
     
  12. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Jeremy-
    Cheers-you know a little while ago I wouldve been content with pretty(3D and Interactive)pictures but now I need more. Im sure everyone who reads these forums has some version of that. So cheers for the encouragement, it does help. Im really into it too, its not like I feel I'm too thick to get it, I've just never been exposed to it. Now Im prepared to spend a long time to get really good at writing code, just as I couldnt build a level or firstperson shooter with unity a few months ago. Its really exciting. Ive just got to find a way to get stuck in, and revisiting the scripting tut is a start, if daunting to the beginner. At my training college it looks probable to develop our maya based animation into a game design course next year. A great leftbrain/right brain experience. Half modelling etc, half programming. We'll be using Unity, so I'll be showing potential tutors the scripting reference and as much code as I can find to give them an idea of what would be ideal to learn. My girlfriend is aching to move back to her home of Sydney but I have such a fantastic opportunity to stay here and learn Maya and Unity that shes prepares to wait...the Darling

    Rock on guys, your help is valuable-I just wish scripting clues were helpful to me now rather than next year.-but please keep em coming! :p
     
  13. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Hey, Targos.

    I wrote you a tutorial, it got deleted with the other stuff when the forums crashed, but David posted a backup so I shall replace it.

    I am glad you are willing to give coding a shot. In the words of a wise man; This stuff is just too cool to give up on. icon_wink.gif ¨

    Here is the classic rotating cube script. You create a cube by clicking GameObject->Create Other->Cube, then you drag this .js script onto it.

    Ok, first simply making the cube rotate:
    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.   transform.Rotate(0, 10, 0);
    5. }    
    6.  
    The first word "function" tells the compiler that the next code block is a function. Code blocks start with an { and end with an }.

    So after "function" we have our function name "Update". "Update" is a function that get's called by unity every frame. Remember, case is important.

    After our function name we have (). The () brakets are used to send data to the function if needed. Since we aren't sending anything, we do (). Every function needs at least ().

    Ok, We have finished telling the compiler that we have a function named Update that we are not sending any data to, now the next line is our {. Remember that { starts a function code block? The { is telling the compiler where your function code begins.

    Next we have this line: transform.Rotate(0, 10, 0);
    transform is a class is that manages the objects position, rotation, scale, etc. Since we are putting this script right onto our object, we just call transform. Next is a . and the word Rotate. Rotate's first letter is uppercase because it is a function. After Rotate we have this (0, 10, 0). Remeber how I said the () are used to send data to a function? Well, here we are sending z,y and z to the function. The first number is the x axis, we set it to 0 because we don't want to rotate on the x axis. The next number is the Y axis, and we set it to 10. 10 units per frame. Next is Z, and again we set it to 0. We just told the Rotate function to rotate around the Y axis (the middle number) 10 units per frame.

    Right after the transform.Rotate line is ";" The semi colon is put at the end of most code you write. Take a look at other people's code to see where it is used and where it isn't.

    Next comes the }. That closes the function code block, it tells unity that you have finished writing your function code.

    So when you put the script on a cube and hit play, the cube will rotate.

    But, what if we rand the code on a super slow machine? Since it is rotating 10 units every _frame_, wouldn't lower framerates cause the cube to rotate slower? Yes it would. But there is a fix:
    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.   transform.Rotate(0, 10 * Time.deltaTime, 0);
    5. }
    6.  
    We multiplied (*) 10 by Time.deltaTime. Time is a _class_ that provides...you guessed it, time functions. Time.deltaTime is float (number with a decimal point: 1.5, 2.333452) that contains the amount of time that has passed since the last frame. So by multipliying our speed number, 10, by Time.deltaTime, we ensure that the cube will spin at the same speed regardless of framerates.

    Now try making the cube rotate on another axis, ot two at once.

    I wish I could write more, but I have to get back to programming myself.

    HTH,
    -Jeremy
    PS: Thanks for the backup David. ;-)
     
  14. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hey Jeremy I just backtracked on the dumped server files and found your post-Thanks heaps man, thats absolutely wicked.
    I know it must have taken you a wee bit of time to write that, and I appreciate it. I will apply it ASAP. I look forward to trying it on a sphere colliding sphere, so it randomly rolls across the land...Theres prob a few things to learn, ie the transform function may conflict with the physics equations of moving across a landscape..I'll let you know...Thanks againAC
     
  15. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Moving an object by transform.Rotate just rotated the object, it does nothing with physics. If you wanted to make a sphere rolling across the ground you woula need to attach a rigidbody to the sphere, and write a script that applies torque. I highly recommend that you take your time learning the basics (like the example I just posted) before you jump ahead.

    Try the tut I posted, then try to make the cube move using Transform.Translate(x,y,z) instead. Then try adding your own variables (var mySpeed = 10; ) and so on. Jumping ahead will get frustrating quickly belive me.

    Good luck,
    -Jeremy
     
  16. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    I

    Cool I got the rotate and translate functions happening. Can you show me how(where in the script) to put the above "Var" part into it?
    Thanks
    AC
     
  17. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Code (csharp):
    1.  
    2. var mySpeed = 10;
    3.  
    4. function Update()
    5. {
    6.   transform.Rotate(0, mySpeed, 0);
    7. }
    8.  
    Have fun ;)
    -Jeremy
     
  18. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2. var mySpeed = 10;
    3.  
    4. function Update()
    5. {
    6.   transform.Rotate(0, mySpeed * Time.deltaTime, 0);
    7. }
    8.  
    Using Time.deltaTime will also make the script independent of framerate, so it will rotate at the same speed no matter how many frames per second the machine is rendering with.
     
  19. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Heh, thanks Joe. I forgot to add that from my first example.

    -Jeremy
     
  20. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Very Cool.Thanks. Got it.
    So a varible(var) is always at the very beginning?

    After our function name we have (). The () brakets are used to send data to the function if needed.What kind of data could be sent to the function?as in, what could exist inside these brakets

    I think Im getting it- :eek:
    AC
     
  21. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    1. You have to declare a variable before you use it. It doesnt have to be at the very top of the script, but it does have to be above anything that uses it.

    For example this is wrong:
    Code (csharp):
    1.  
    2. myAge = 20;
    3. var myAge;
    4.  
    But this will work fine:
    Code (csharp):
    1.  
    2. var myAge;
    3. myAge = 20;
    4.  
    2. It all depends on the function. Different functions take different data. For the Rotate function (http://www.otee.dk/Documentation/ScriptReference/Transform.html#Rotate) the docs tell us that Rotate() take _either_ Vector3, or if you look below, 3 floats seperated by commas. Remember that a float is a number with a decimal point. The script docs will tell you what takes what and how. ;-)

    Spend some time looking through the script docs and see if you can figure out what data the function is asking for.

    Here is a quick rundown of the variable types you will use most often:

    int: integer, number without a decimal point
    Code (csharp):
    1.  
    2. var myAge = 20;
    3.  
    float: number with decimal point
    Code (csharp):
    1.  
    2. var speed = 1.5;
    3.  
    string: text in quotation marks:
    Code (csharp):
    1.  
    2. var myName = "Jeremy";
    3.  
    Vector3: x,y,z, used to position objects
    Quaternion: x,y,z,w, used for rotations

    There are many more, but I wont go in to them right now.

    HTH,
    -Jeremy
     
  22. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Okay Im embracing the scripting reference with youthful enthusiam...

    Im going to try and copy paste/learn a few things. Unity supports a few lanuages, and I am going to try and stick with Java.
    So do all the script bits in the reference fit with any language? or have they been deigned to work more with one language than another?Or:
    are the script commands identical for each language, and the differences are the surrounding base code.(I wish I knew a better way of explaining)
    Try this
    var=myEggs;

    To make a python omlette,add python milk and python tomatoes
    To make a Javascript omlette,add Javascript milk and Javascript tomatoes
    To make a C# omlette,add C# milk and C# tomatoes
    IE, the varible myEggs is the same throughout , its the other ingrdients of the script that are different, but so long as the var myEggs(the info in the script reference) is the same, we have omlette?
    Code (csharp):
    1.  
    2.  
    3. am I over explaining myself?
    4.  
    5. var=mySanity(x)
    6. transform.localScale.x -= 0.1
     
  23. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    The code samples (in the gray boxes) in the script docs are in JavaScript. The actual specs of the functions are universal. You can't throw the exact lines from the docs in you code (except in the code sample boxes) because they are only meant to show you: A: what variables and in what order the function requires, and B: what variables (if any) the function returns.

    Code ,including variables, are written and used differently in the various languages. I recommend you stick with JS and don't worry about the other languages. Everything is much easier in JS than in C# (I use C#). Take this example:
    Code (csharp):
    1.  
    2. //this is a JS script to create a float number
    3. var myNum = 2.5;
    4.  
    5.  
    6. //now this is the C# code to do the exact same thing:
    7. float myNum = 2.5F;
    8.  
    In C# you hae to explicitly tell unity about your functions and exactly what they are. In JS you have a bit more freedom as you'll see later on.

    Also from your post you wrote the variable code like this:
    Code (csharp):
    1.  
    2. var=mySanity(x)
    3. transform.localScale.x -= 0.1
    4.  
    That is incorrect, but you did find out how to use a function from the docs and the easy way to decrement a value (-=). Good. Here is what the code should look like:
    Code (csharp):
    1.  
    2. var mySanity = 25;
    3. transform.localScale.x -= 0.1;
    4.  
    5. //or, if you wanted to use the mySanity variable to decrement the localScale:
    6.  
    7. var mySanity = 25;
    8. transform.localScale.x -= mySanity;
    9.  
    Notice the difference between my code and yours. First the variable, the first word is "var" which tells unity you are declaring a brand new variable, the next word is the vaiable's name. Then you can assign a value to the variable with the = operator. Notice also the semi-colons at the end of each line. They tell unity where each line of code starts and stops. Very important.

    HTH, and keep at it (I still recommend you find a book or site too. Will help a lot.)
    -Jeremy
     
  24. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Awesome Man. I believe it will become a logic. I'll try and sus out a good book at the library, theres heaps of tech books there.Thats great, thanks again
    AC
     
  25. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    No problem man. I would have hated to see you give up out of frustration. This stuff it far too cool once you get going. Unity is an amazing piece of software.

    Take care,
    -Jeremy
     
  26. Garen

    Garen

    Joined:
    Apr 24, 2006
    Posts:
    97
    This was very helpful to me as well. Code is intimidating but now it seems some what aproachable. Thanks!
     
  27. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Hey Garen, I am glad it helped you. The actual raw code syntax is usually easy to understand once you "get it". Figuring out what to do with it and how is the trick. ;)

    Good luck guys,
    -Jeremy