[QUOTE=Asgard;44809643]What I posted would lerp between two colours, so if your health was 100% it would choose 100% the alive color, no mix. If your health was 0%, it would've chosen the death color without mixing. 50% would mix between the two, etc.
If you have pro you could use:
[url]http://docs.unity3d.com/Documentation/Components/script-GrayscaleEffect.html[/url]
But I'm not sure if you can choose the amount of grayscale with that.[/QUOTE]
Ah I see!
Yeah I looked into grayscale but it doesn't have any variable to alter the amount or anything. You wouldn't know how to alter the shader to allow it at all?
Right now I have it so that once you take damage, a DOF effect is applied and worsens as your closer to death. When your health is below 50 I add in a heart beat sound effect and once its below 30 I add in some motion blur. So it is quite indicative of your health, but I think the desaturation would really sell it even more.
I'm starting to think there is no way of getting enjoyable physics synchronization across the network.
I'm still a pretty big noob, I understand the basics of networking.. I think.
So let me ask, as an example. Would creating a physics sandbox type of game even possible, er well, getting the physics properly synced to everyone, possible?
Take an example of source, players can move little and big physics objects with seemingly no lag.
I'd love some input on this. Its a bit worry some, because this is really my only problem.
[QUOTE=sarge997;44810305]I'm starting to think there is no way of getting enjoyable physics synchronization across the network.
I'm still a pretty big noob, I understand the basics of networking.. I think.
So let me ask, as an example. Would creating a physics sandbox type of game even possible, er well, getting the physics properly synced to everyone, possible?
Take an example of source, players can move little and big physics objects with seemingly no lag.
I'd love some input on this. Its a bit worry some, because this is really my only problem.[/QUOTE]
Your only hope is making the server the one true authority. As in, players don't ever get direct control over physics, they sit and wait for the server to update them on position & velocity.
Otherwise, physics are really super complicated and there's no good way to handle multiplayer physics the same way you could handle characters in a server authoritative game (Source engine style)
Is using System.Math functions faster than UnityEngine.Mathf functions, especially if I'm doing a [i]ton[/i] of calculations?
Any ideas on how I can get particle collisions working? I have a flame thrower spell and I'm trying to have a Torch detect when a particle hits it to light it up but it isn't working correctly.
Apparently you can only do it with the old Particle System and not the new Shurikien system? How do I know if I'm doing the Shurukien system?
EDIT
Got it turns out I was only allowing collisions with planes and not the world.
[QUOTE=Pelf;44811436]Is using System.Math functions faster than UnityEngine.Mathf functions, especially if I'm doing a [i]ton[/i] of calculations?[/QUOTE]
Doubt it. Unity's Sqrt function was actually faster than a pure C# implementation of Carmack's Inverse Square Root.
Anyone know how to make a raycast continue after it has hit an object?
I want the raycast to return all hits.
[QUOTE=Over-Run;44809949]Ah I see!
Yeah I looked into grayscale but it doesn't have any variable to alter the amount or anything. You wouldn't know how to alter the shader to allow it at all?
Right now I have it so that once you take damage, a DOF effect is applied and worsens as your closer to death. When your health is below 50 I add in a heart beat sound effect and once its below 30 I add in some motion blur. So it is quite indicative of your health, but I think the desaturation would really sell it even more.[/QUOTE]
Prepare your anus (made a very simple change to a existing shader)
[URL]https://gist.github.com/CptAsgard/edfffdcd73f2979bb09a[/URL]
Create a .shader file named Greyscale.shader, create a .cs file called Greyscale.cs. Create a material named anything you want, and set the shader to Custom/Greyscale. Attach Greyscale.cs onto your main camera, and drag in the material file.
Check Greyscale.cs for how you can set the value, you want to base this on your current health inbetween the range 0.0 and 1.0 (like I did for the lerp, currentHealth / maxHealth). Good luck!
[editline]15th May 2014[/editline]
[QUOTE=FalconKrunch;44814740]Anyone know how to make a raycast continue after it has hit an object?
I want the raycast to return all hits.[/QUOTE]
You'll have to raycast again from the hit point.
[QUOTE=FalconKrunch;44814740]Anyone know how to make a raycast continue after it has hit an object?
I want the raycast to return all hits.[/QUOTE]
Not sure if there's an easier way but you could do a second raycast from the hit pos with the ray dir.
Apparently Physics.RaycastAll is a thing and it returns all objects it hits. Just what I needed.
I really didn't want to have to make continuous raycasts.
[QUOTE=FalconKrunch;44814776]Apparently Physics.RaycastAll is a thing and it returns all objects it hits. Just what I needed.
I really didn't want to have to make continuous raycasts.[/QUOTE]
Remember, RaycastAll does not return hits in any particular order. You'll have to sort them by distance.
[QUOTE=Asgard;44814745]Prepare your anus (made a very simple change to a existing shader)
[URL]https://gist.github.com/CptAsgard/edfffdcd73f2979bb09a[/URL]
Create a .shader file named Greyscale.shader, create a .cs file called Greyscale.cs. Create a material named anything you want, and set the shader to Custom/Greyscale. Attach Greyscale.cs onto your main camera, and drag in the material file.
.[/QUOTE]
Wow thanks man that worked perfectly! Thanks so much.
Anybody had issues with Mesh collides? When I add a rigid body to objects and add the mesh collider, the item still falls through the world. I set the model to generate colliders and set the mesh collider to Convex but no good :S
I really wanted moving planets to I took another stab at it.
[vid]https://dl.dropboxusercontent.com/u/13781308/Videos/Moving%20Planets.mp4[/vid]
Now you can land on them without sliding off or experiencing weird sticky effects. And even better, it doesn't need some huge convoluted math system that eats up lots of system resources.
[QUOTE=KillaMaaki;44815434]Remember, RaycastAll does not return hits in any particular order. You'll have to sort them by distance.[/QUOTE]
Doesn't matter, I just need to find the path (on 2 axis) with the most objects, so it works perfectly.
OK, so I just came across this code.
I've seen code like this all over the internet. I've also seen plenty of tips telling you to do this.
DON'T! It's a total myth!
What am I talking about?
This:
[code]
public Transform backLeft;
public Transform backRight;
public Transform frontLeft;
public Transform frontRight;
// THESE GUYS RIGHT HERE
// -----------------------------------------
public RaycastHit lr;
public RaycastHit rr;
public RaycastHit lf;
public RaycastHit rf;
public Vector3 upDir;
// -----------------------------------------
void Update () {
Physics.Raycast(backLeft.position + Vector3.up, Vector3.down, out lr);
Physics.Raycast(backRight.position + Vector3.up, Vector3.down, out rr);
Physics.Raycast(frontLeft.position + Vector3.up, Vector3.down, out lf);
Physics.Raycast(frontRight.position + Vector3.up, Vector3.down, out rf);
upDir = (Vector3.Cross(rr.point - Vector3.up, lr.point - Vector3.up) +
Vector3.Cross(lr.point - Vector3.up, lf.point - Vector3.up) +
Vector3.Cross(lf.point - Vector3.up, rf.point - Vector3.up) +
Vector3.Cross(rf.point - Vector3.up, rr.point - Vector3.up)
).normalized;
Debug.DrawRay(rr.point, Vector3.up);
Debug.DrawRay(lr.point, Vector3.up);
Debug.DrawRay(lf.point, Vector3.up);
Debug.DrawRay(rf.point, Vector3.up);
transform.up = upDir;
}
[/code]
See where the code defines temporary variables outside of the function that uses them?
I've seen tips for Unity urging people to do this for all variables possible because "it reduces garbage collection".
But actually, that's a complete myth borne out of a misunderstanding of the difference between the heap and the stack in .NET, and value types vs reference types.
A value type is passed around by value, as copies. Examples of this would be floats, ints, bools, etc. Structs are also value types. Value types are allocated on the [B]stack[/B] and are [B]not garbage collected[/B] - when they go out of scope, the memory is immediately freed.
A reference type is passed by reference, rather than copies of the same value. Classes are reference types. Reference types are allocated on the [B]heap[/B], and [B]ARE garbage collected[/B]. The garbage collector has to keep track of when and where they are referenced and deallocate memory when an object is no longer referenced.
RaycastHit and Vector3 are both [B]structs[/B], and therefore are both [B]value types.[/B] Thus, they are [B]not garbage collected[/B] and this technique of declaring the temporary variable does absolutely nothing for memory or performance and just serves to make your code less readable.
[B]/rant over[/B]
[QUOTE=KillaMaaki;44819195]OK, so I just came across this code.
I've seen code like this all over the internet. I've also seen plenty of tips telling you to do this.
DON'T! It's a total myth!
What am I talking about?
This:
code
See where the code defines temporary variables outside of the function that uses them?
I've seen tips for Unity urging people to do this for all variables possible because "it reduces garbage collection".
But actually, that's a complete myth borne out of a misunderstanding of the difference between the heap and the stack in .NET, and value types vs reference types.
A value type is passed around by value, as copies. Examples of this would be floats, ints, bools, etc. Structs are also value types. Value types are allocated on the [B]stack[/B] and are [B]not garbage collected[/B] - when they go out of scope, the memory is immediately freed.
A reference type is passed by reference, rather than copies of the same value. Classes are reference types. Reference types are allocated on the [B]heap[/B], and [B]ARE garbage collected[/B]. The garbage collector has to keep track of when and where they are referenced and deallocate memory when an object is no longer referenced.
RaycastHit and Vector3 are both [B]structs[/B], and therefore are both [B]value types.[/B] Thus, they are [B]not garbage collected[/B] and this technique of declaring the temporary variable does absolutely nothing for memory or performance and just serves to make your code less readable.
[B]/rant over[/B][/QUOTE]
This might be related, I saw in a talk by a Unity dev that for a function that's called a lot it might boost performance to declare variables outside and simply update their variables inside the function. He said that by delcaring them outside and simply updating the values removes the need to allocate and release memory constantly saving some performance. Would that be right?
The video: [URL]https://www.youtube.com/watch?v=Ozc_hXzp_KU[/URL]
starts at 30:17 and goes for about 6 minutes
[editline]edit[/editline]
Or maybe I'm just misinterpreting what he's saying?
I could see perhaps a slight performance boost for doing so in a function that's called thousands of times per frame, but in practice I highly doubt you will ever notice (if a function is slow, the reason is almost certainly not because you are allocating value types)
[QUOTE=Pelf;44819878]This might be related, I saw in a talk by a Unity dev that for a function that's called a lot it might boost performance to declare variables outside and simply update their variables inside the function. He said that by delcaring them outside and simply updating the values removes the need to allocate and release memory constantly saving some performance. Would that be right?
The video: [URL]https://www.youtube.com/watch?v=Ozc_hXzp_KU[/URL]
starts at 30:17 and goes for about 6 minutes
[editline]edit[/editline]
Or maybe I'm just misinterpreting what he's saying?[/QUOTE]
He says to cache the array instead of reallocating it, which is valid advice since that's an object.
However, since this array has a size known at compile time and is really small, it would be a much better idea to just use five variables.
(If the loop had more content it would make sense to make a method of it and call it for each iteration, passing large structs by reference).
[editline]16th May 2014[/editline]
[QUOTE=KillaMaaki;44819912]I could see perhaps a slight performance boost for doing so in a function that's called thousands of times per frame, but in practice I highly doubt you will ever notice (if a function is slow, the reason is almost certainly not because you are allocating value types)[/QUOTE]
It would make the function slower actually. Persistent and stack allocations have no running cost whatsoever.
The difference comes from the indirection when accessing fields, which can add some time depending on what the JIT does.
[QUOTE=Tamschi;44819986]He says to cache the array instead of reallocating it, which is valid advice since that's an object.[/QUOTE]
Ah, right. Was too lazy to watch the video.
Again, it's very important to understand what's a value type and what's a reference type.
Arrays are reference types. So are strings (which is super important to remember! creating strings = garbage collection)!
Those SHOULD be declared like that, rather than recreated every time.
[QUOTE=Tamschi;44819986]
The difference comes from the indirection when accessing fields, which can add some time depending on what the JIT does.[/QUOTE]
Now this is another matter altogether and depends entirely on the property in question. Simple "return private field" properties are probably(?) not an issue, but in some cases you can improve performance by de-referencing the property or indexer (especially for collections)
For instance, if I've got a dictionary and I'm doing this:
[code]
for( int i = 0; i < 1000; i++ )
{
doSomethingWith( myDictionary[ "key" ] );
}
[/code]
If this code proves to be a bit too performance heavy, I could do this as a micro-optimization:
[code]
var value = myDictionary[ "key" ];
for( int i = 0; i < 1000; i++ )
{
doSomethingWith( value );
}
[/code]
[QUOTE=KillaMaaki;44820061]Ah, right. Was too lazy to watch the video.
Again, it's very important to understand what's a value type and what's a reference type.
Arrays are reference types. So are strings (which is super important to remember! creating strings = garbage collection)!
Those SHOULD be declared like that, rather than recreated every time.
Now this is another matter altogether and depends entirely on the property in question. Simple "return private field" properties are probably(?) not an issue, but in some cases you can improve performance by de-referencing the property or indexer (especially for collections)
For instance, if I've got a dictionary and I'm doing this:
[code]
for( int i = 0; i < 1000; i++ )
{
doSomethingWith( myDictionary[ "key" ] );
}
[/code]
If this code proves to be a bit too performance heavy, I could do this as a micro-optimization:
[code]
var value = myDictionary[ "key" ];
for( int i = 0; i < 1000; i++ )
{
doSomethingWith( value );
}
[/code][/QUOTE]
I'm pretty sure it does that anyway, it's why you need manual memory fences and make stuff volatile.
I'm making a Super Monkey Ball clone wish me luck.
[QUOTE=KillaMaaki;44820061]
For instance, if I've got a dictionary and I'm doing this:
[code]
for( int i = 0; i < 1000; i++ )
{
doSomethingWith( myDictionary[ "key" ] );
}
[/code]
If this code proves to be a bit too performance heavy, I could do this as a micro-optimization:
[code]
var value = myDictionary[ "key" ];
for( int i = 0; i < 1000; i++ )
{
doSomethingWith( value );
}
[/code][/QUOTE]
Its called hoisting and it's normally done at the JIT level for simpler functions. (assuming you didn't have a debugger attached when the method was jitted because that automatically disables optimizations)
In your example the jitter can [B]not[/B] optimize that because an indexer is a method call, which might have sideeffects. Indexers being methods obviously does not apply to real array types but for everything else like list, dictionary,...
Even calling NOP stubs can have side effects like executing the static constructor (there are actually much more side effects).
As for the field access: Tamschi's explanation (manual fencing...) is right, but in some special cases that optimization can be suppressed.
I'm working with the implementation details and internals of the .NET environment for a long time now so unless you know what I know:
1. Measure whats faster before optimizing.
2. When measuring: Only measure release builds, only start them without debugger attached and always discard the first 101 iterations (or more) of the code you want to measure (yes the lower limit of 101 is not random)
3. Never optimize small things when you don't have performance problems. Chances are that the stuff you're doing is already done at the compiler or jitter level.
[QUOTE=Felheart;44822647]
I'm working with the implementation details and internals of the .NET environment for a long time now so unless you know what I know:
1. Measure whats faster before optimizing.
2. When measuring: Only measure release builds, only start them without debugger attached and always discard the first 101 iterations (or more) of the code you want to measure (yes the lower limit of 101 is not random)
3. Never optimize small things when you don't have performance problems. Chances are that the stuff you're doing is already done at the compiler or jitter level.[/QUOTE]
1.) Agreed. If I haven't measured and don't know if something is the bottleneck, then I shouldn't be expending the effort on optimizing it.
2.) Interesting, hadn't heard of this before. I've always discarded the first iteration to account for JIT compilation.
3.) The last times I did the aforementioned micro-optimization were to make my code more readable, and I think also a voxel engine where it actually did result in a small performance boost (in that situation, every bit of speed counts)
[QUOTE=KillaMaaki;44808665]Wee, I got a community Learn module published :)
[URL]http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coding-practices[/URL][/QUOTE]
Thanks Alan :)
I indeed have big state machine for NPC and it would make sense to make modules..
Just one question! I use those states both in Update and FixedUpdate, so I can't really use one delegate for each, which means I need to write double delegates correct?
Also (I never really worked with delegates), how do you compare which state is it currently (in IF statement)
With enum I just do if(currentState == State.idle) { ... do shit ... }
_______________
Oh shit, I think I understand.. it is possible to call multiple functions at once with delegate which means there are possible multiple states at once?
Wow... :D
So here's what I would do.
Let's say your NPC has the states Patrol, Attack, and Call For Help.
By default, your NPC is in the state Patrol.
First, the basis of our state machine.
[code]
public class MyNPCBehaviour : MonoBehaviour
{
System.Action currentState;
void Update()
{
if( currentState != null ) currentState();
}
}
[/code]
Next, we'll create the methods for each of our states:
[code]
public void Patrol()
{
// but actually, take a nap ;)
}
public void Attack()
{
// threateningly wave a stick at the opponent
}
public void CallForHelp()
{
// this is called when your NPC is royally screwed and knows it.
}
[/code]
So then we assign a default state in Start:
[code]
void Start()
{
// set an initial state
currentState = this.Patrol;
}
[/code]
And if we ever need to switch state we can just do:
[code]
currentState = this.Attack; // or this.CallForHelp, or whatever other states you've defined.
[/code]
[QUOTE=KillaMaaki;44824420]So here's what I would do.
-[/QUOTE]
What's System.Action? Is it a type that stores methods or something?
[editline]16th May 2014[/editline]
Wait, its being called like a function too?
[QUOTE=Pelf;44825474]What's System.Action? Is it a type that stores methods or something?
[editline]16th May 2014[/editline]
Wait, its being called like a function too?[/QUOTE]
[code]
Action action;
action = () => { /* stuff */ };
action();
[/code]
[QUOTE=Pelf;44825474]What's System.Action? Is it a type that stores methods or something?
[editline]16th May 2014[/editline]
Wait, its being called like a function too?[/QUOTE]
System.Action holds a reference to a function with no return type or parameters. System.Func can handle parameters, and I believe also return a value.
It's a [url=http://msdn.microsoft.com/en-gb/library/ms173171.aspx]delegate[/url] with no parameters and no return value.
[editline]16th May 2014[/editline]
[QUOTE=Asgard;44825510]System.Action holds a reference to a function with no return type or parameters. System.Func can handle parameters, and I believe also return a value.[/QUOTE]
Also, I believe you can use System.Action<T>, System.Action<T1,T2> etc to specify parameters.
Oh wow that's cool, thanks.
Sorry, you need to Log In to post a reply to this thread.