Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Shalinor
Jun 10, 2002

Can I buy you a rootbeer?

roomforthetuna posted:

Weird thing in Unity, just freezing, and hitting pause in MonoDevelop for debugging shows it's just stuck in some completely trivial line - most often the constructor for a Vector2, in my code. (Trying to single step the code from that point just leaves it doing nothing, and hitting pause in the debug menu again shows it's still in the same line.)

Turns out it's probably some kind of out of memory issue (one of those things of accidentally inserting after the element in a list you're pointing at, while iterating through the list, making an infinite loop of expanding list), but weird since there was still plenty of system RAM available, I didn't get any sort of error message (a thrown exception would be nice), and the Vector2 in question is certainly not the biggest allocation around. Shouldn't even be an allocation at all I'd hope, since it should be being constructed into a variable that's already allocated, but maybe debug version would still call the constructor for that.

Took a long time to find the actual problem, what with the whole having to totally kill the process every time thing. Unity could really really do with a "debug your game in a different process from the editor" option, for easier sorting of infinite loop type mistakes.
I'm getting exactly the same thing, and I'm inclined to write it off as Unity-side bug in 4.X. I'd been hoping it was one of those hitches that only shows up in editor builds (I've never seen it hit in one of our web builds, for instance).

Adbot
ADBOT LOVES YOU

stramit
Dec 9, 2004
Ask me about making games instead of gains.
If you are on OSX open up Activity Monitor and sample the process you'll get a (native side) callstack that might help.

Zhentar
Sep 28, 2003

Brilliant Master Genius
I have 3 3D vectors:
-a unit vector giving the direction of the camera
-the location of the camera
-the location of an object

I want to determine the angle of the object from the center of the camera (e.g. 20 degrees above, 30 degrees to the right). I haven't had much luck figuring this out with google so far, probably because I don't remember enough about working with vectors to describe it correctly. How do I do this?

poemdexter
Feb 18, 2005

Hooray Indie Games!

College Slice

Zhentar posted:

I have 3 3D vectors:
-a unit vector giving the direction of the camera
-the location of the camera
-the location of an object

I want to determine the angle of the object from the center of the camera (e.g. 20 degrees above, 30 degrees to the right). I haven't had much luck figuring this out with google so far, probably because I don't remember enough about working with vectors to describe it correctly. How do I do this?

Short answer is atan2.

The long answer is that I'm on my phone but will let this related stack overflow talk for me. http://stackoverflow.com/questions/4879961/calculate-angle-from-two-points-and-a-direction-vector

Grocer Goodwill
Jul 17, 2003

Not just one kind of bread, but a whole variety.

Zhentar posted:

I have 3 3D vectors:
-a unit vector giving the direction of the camera
-the location of the camera
-the location of an object

I want to determine the angle of the object from the center of the camera (e.g. 20 degrees above, 30 degrees to the right). I haven't had much luck figuring this out with google so far, probably because I don't remember enough about working with vectors to describe it correctly. How do I do this?

What do you mean by above and to the right? From the camera's point of view, or from an outside observer's point of view? To put it another way: in camera space or world space?

If it's world space, you can convert the vector from the camera to the object into spherical coordinates. If it's camera space, you need two more vectors: one's that describe which way is up and to the right for the camera. One of these you must specify, the other you can compute from the previous and the camera's forward direction. You can then get the cosine of the desired angles by taking the dot product of the vector from the camera to the object against those vectors.

But do you really want to do all of that? You can, and it'll work, but angles are a pain to deal with. What do you actually need these angles for? Often there is an equivalent calculation you can do that involves only vectors which can save you a lot of headache.

poemdexter posted:

Short answer is atan2.

The long answer is that I'm on my phone but will let this related stack overflow talk for me. http://stackoverflow.com/questions/4879961/calculate-angle-from-two-points-and-a-direction-vector

You could make atan2 work, but that Stack Overflow answer is way over-thought. If you're trying to pan an audio source, you care about the cosine of the angle, not the angle itself.

Grocer Goodwill fucked around with this message at 01:55 on Dec 8, 2013

Zhentar
Sep 28, 2003

Brilliant Master Genius
In camera space. I guess I do have a fourth vector that says which way is up for the camera. I'll try out converting to spherical coordinates then.


If the object is on screen, then I want to determine where it is on the screen. If it's not, then I want to determine how far off screen (in degrees, so yeah, I do need to convert to degrees at some point).


edit: wait, it was the dot product thing I wanted to try, not the spherical coordinates one.

Zhentar fucked around with this message at 04:52 on Dec 8, 2013

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
Another Unity debugging bastard thing, that the debug log frame doesn't update very frequently at all, making even the equivalent of the "print that you got here" debugging method unhelpful for these freezey bugs - I was outputting a y coordinate to find where the problem was, and the last one output on screen at the time of the freeze was 1.2, but the actual problem was several frames later, like Debug.Log was subsequently called on 1.2, 1.3, 1.35, 1.4, 1.5, 1.55 when I was single-stepping the code, before the crash during 1.55's processing. Makes it hard to frame a recreation for debugging when you can't get the bastard to output the moment before it happens.

I might have to implement a GUI console sort of thing just for sticking debug values on the screen a bit more reliably. I'm out of bugs that are reliably enough reproduced to detect otherwise. Which is good, I suppose!

Corbeau
Sep 13, 2010

Jack of All Trades
That particular instance is why I frequently use the build and run function, then check the last line of the debug text file.

Shalinor
Jun 10, 2002

Can I buy you a rootbeer?

roomforthetuna posted:

Another Unity debugging bastard thing, that the debug log frame doesn't update very frequently at all, making even the equivalent of the "print that you got here" debugging method unhelpful for these freezey bugs - I was outputting a y coordinate to find where the problem was, and the last one output on screen at the time of the freeze was 1.2, but the actual problem was several frames later, like Debug.Log was subsequently called on 1.2, 1.3, 1.35, 1.4, 1.5, 1.55 when I was single-stepping the code, before the crash during 1.55's processing. Makes it hard to frame a recreation for debugging when you can't get the bastard to output the moment before it happens.

I might have to implement a GUI console sort of thing just for sticking debug values on the screen a bit more reliably. I'm out of bugs that are reliably enough reproduced to detect otherwise. Which is good, I suppose!
Get DebugConsole. It's a way better output target for debug spew (it doesn't freeze your execution), and it also gives you support for customizable console commands - which I've been abusing lately, since I have no UI still :3:

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Shalinor posted:

Get DebugConsole. It's a way better output target for debug spew (it doesn't freeze your execution), and it also gives you support for customizable console commands - which I've been abusing lately, since I have no UI still :3:
Thanks, that's pretty handy and nice and easy.

At last, stage one of my lighting is complete and working - 2D 'volumetric' lighting using neither raycasts nor premium features. Its main weakness versus other options right now is it can't do coloured lights or lights adding up when they overlap (both of which would be dead easy with Pro, but require insane complexity without it). Its strengths include being super precise (no flickery edges as many raycasting options have), maybe relatively fast (I suspect slower when there's many complex objects casting shadows, but faster in sparse or simple conditions), and being able to cast lights 'onto' surfaces, with a "penetration distance" value. And being able to be properly "you can't see anything" dark - not sure whether the 2D Volumetric Lighting asset store guy's one can do that or not, it doesn't do it in the demos and I suspect it operates on some shader tomfoolery that means it can't do it.

One more relatively simple feature to add, making the light do an area of full brightness before the fadeoff (separating "inner radius" and "outer radius" - currently it only does either full brightness throughout or constant fadeoff) and it'll be ready to be seen.

This brings up a question for me though - is there really no compile-time toggle (like a #ifdef) for whether you have Pro or not? I'd want to compile "lovely best I can do workaround" version for non-pro users and "much simpler and faster rendertarget-based" version for pro, but not as distinct files because most of the code is still shared, and a runtime toggle seems dumb and inappropriate for something like that.

Edit: I suppose an if (SystemInfo.supportsRenderTextures) or five every frame probably isn't all that much of a resource drain, and has the dubious extra advantage of also properly handling some sort of crazy hardware configuration that's capable of doing all this tricky rendering poo poo but somehow not capable of doing rendertargets.

roomforthetuna fucked around with this message at 06:30 on Dec 9, 2013

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
Is the promised GUI stuff update going to part of Unity 4.x, or is that going to be on another full number update? I'm dithering on whether to get Pro or not; I don't want to be spending $1500 only to have it cost another $750 later to get an update that makes it properly usable. (This makes me glad I didn't get Pro last time I was considering it, pre version 4, since now I'd be out another $750 or more to get 2D - not sure making people feel "glad I didn't pay earlier" is good marketing strategy! I suppose "surprise, you're not done paying like you thought!" is pretty effective though.)

stramit
Dec 9, 2004
Ask me about making games instead of gains.

roomforthetuna posted:

Is the promised GUI stuff update going to part of Unity 4.x, or is that going to be on another full number update?

It will be in the 4.x release cycle.

quote:

I'm dithering on whether to get Pro or not; I don't want to be spending $1500 only to have it cost another $750 later to get an update that makes it properly usable. (This makes me glad I didn't get Pro last time I was considering it, pre version 4, since now I'd be out another $750 or more to get 2D - not sure making people feel "glad I didn't pay earlier" is good marketing strategy! I suppose "surprise, you're not done paying like you thought!" is pretty effective though.)
That's a weird way to look at it... Is there enough value in the product now to pay what is being asked? If yes, buy it; if not, don't. There will always be more features going into Unity, some in minor releases some in major releases. If you wait till Unity is "done" to buy the final definitive version you will be waiting a long time I think.

FuzzySlippers
Feb 6, 2009

The new UI may not be anything special anyway. From the presentation on it I think its going to be fairly bare bones and it might end up like mecanim where its broken for a few releases after the initial.

Spending some bucks you can get NGUI now which is already the new UI but with more features, try 2DToolkit's UI which I've heard good things but have no experience, or try DF-GUI which similar to NGUI but way more straight forward in its config/setup yet also newer and rougher in some ways.

Shalinor
Jun 10, 2002

Can I buy you a rootbeer?

FuzzySlippers posted:

The new UI may not be anything special anyway. From the presentation on it I think its going to be fairly bare bones and it might end up like mecanim where its broken for a few releases after the initial.

Spending some bucks you can get NGUI now which is already the new UI but with more features, try 2DToolkit's UI which I've heard good things but have no experience, or try DF-GUI which similar to NGUI but way more straight forward in its config/setup yet also newer and rougher in some ways.
I'd go with NGUI still. Given that the dude they hired to make their new gui is the NGUI dude, it's been implied that he's trying to make it so that existing NGUI users will be able to switch over to new Unity GUI relatively painlessly.

(which won't be for months and months yet, so just use NGUI for your current project and think about Unity GUI for your next, basically)

Spek
Jun 15, 2012

Bagel!
Does anyone see any way of making a shader like this run faster on handhelds?

code:
Shader "AlphaFlickerShader" 
{
	Properties 
	{
		_Color ("_Color", Color) = (1,1,1,1)
		_MainTex ("_MainTex", 2D) = "white" {}
		_Noise ("_Noise", 2D) = "white" {}
	}
	Category 
	{
		Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
		Blend SrcAlpha OneMinusSrcAlpha
		Cull Off Lighting Off ZWrite Off
	
		SubShader
		{
			Pass
			{
				CGPROGRAM

				#pragma vertex vert
				#pragma fragment frag
				#pragma fragmentoption ARB_precision_hint_fastest
				#pragma multi_compile_particles
			
				#include "UnityCG.cginc"

				sampler2D _MainTex;
				fixed4 _Color;
				sampler2D _Noise;
			
				struct appdata_t 
				{
					half4 vertex : POSITION;
					half2 texcoord : TEXCOORD0;
				};

				struct v2f 
				{
					half4 vertex : POSITION;
					half2 texcoord : TEXCOORD0;
					half2 sc : TEXCOORD1;
				};
			
				half4 _MainTex_ST;

				v2f vert (appdata_t v)
				{
					v2f o;
					o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				
					o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
					half4 screencoord = ComputeScreenPos (o.vertex);

					o.sc = screencoord.xy * 64.0f;
					o.sc.x += _Time.x * 64.0f;
					o.sc /= screencoord.w;

					return o;
				}

				fixed4 frag (v2f i) : COLOR
				{
					fixed4 retColour = _Color;
					if(_Color.a < tex2D(_Noise, i.sc).a)
						retColour.a = 0.0f;
					else
						retColour.a = tex2D(_MainTex, i.texcoord).a;
					return retColour;
				}
				ENDCG 
			}
		}
	}
}
It's used to create a dissolve effect where each pixel in a texture's alpha of the colour is a cutoff for whether the pixel is opaque or not rather than alpha blended transparency. Like this:

It's bottlenecking me pretty hard on Android when I have a lot of things displayed as almost everything in my game uses this shader, or a near identical one that uses per vertex colours, but I can't think of any way of making the shader more efficient or coming up with some different way of getting a similar effect.

xgalaxy
Jan 27, 2004
i write code
I'm not much of a graphics guy but that branch in the fragment shader is probably not doing you any favors.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

xgalaxy posted:

I'm not much of a graphics guy but that branch in the fragment shader is probably not doing you any favors.

That's how the algorithm works though, he kinda needs the branch for what he's doing.

Hmm, I'm trying to think of some way you can precompute something but I'm not coming up with much.

But, do you really need it to be this robust? To be able to manually determine when each pixel blinks out? I'm thinking you could accomplish the same thing with like a mask or randomness or something, although I don't even know if that would be faster...

Doing that per-pixel for every object on the screen just may not be viable for mobile. :smith:

I've only poked around with shaders a bit though so what do I know :downs:

Contains Acetone
Aug 22, 2004
DROP IN ANY US MAILBOX, POST PAID BY SECUR-A-KEY

Contains Acetone fucked around with this message at 17:38 on Jun 24, 2020

Grocer Goodwill
Jul 17, 2003

Not just one kind of bread, but a whole variety.

Spek posted:

Does anyone see any way of making a shader like this run faster on handhelds?

What size is your noise texture? The shader doesn't look particularly complicated. You can try flattening the branch, like others have suggested, but if I had to guess, I'd suspect you're thrashing the texture cache with that noise texture.

As an aside, how are you using depth in your game? In general when computing a screen-space position, you need to do the w divide in the pixel shader, unless you know that all the vertices in a triangle will always have the same depth. If you're entirely 2D, you can use an ortho projection and skip the w divide altogether.

Spek
Jun 15, 2012

Bagel!
Thanks all

Grocer Goodwill posted:

What size is your noise texture? The shader doesn't look particularly complicated. You can try flattening the branch, like others have suggested, but if I had to guess, I'd suspect you're thrashing the texture cache with that noise texture.

As an aside, how are you using depth in your game? In general when computing a screen-space position, you need to do the w divide in the pixel shader, unless you know that all the vertices in a triangle will always have the same depth. If you're entirely 2D, you can use an ortho projection and skip the w divide altogether.
The noise is only 64*64. It's tiled across the screen and it's only written to once at the start. I then animate it slowly by transforming its texture coords which gives a kind of weird noise + scroll pattern effect that I find looks better than having it be completely noisy.

I do use depth but pretty much everything falls into 3 depth layers, background, play area, GUI. I'm not sure if I can take advantage of that to get more speed up. In theory the only things that need to be sorted against eachother are the things rendered in the same one of those 3 depth layers.

The w divide is just used in mapping the noise texture to screen space for pixel perfect tiling of the noise texture. I have no idea how/why it works I figured it out through trial and error months ago and didn't understand it then, I understand it even less now.

Contains Acetone posted:

If the branch is actually the bottleneck, you can get the same result doing:

code:
retColor.a = Max(Sign(_Color.a - tex2D(_Noise, i.sc).a), 0.0f) * tex2D(_MainTex, i.texcoord).a;
Assuming of course the branching is what's causing the issue. How's the performance if you remove the branch and instead just perform the two texture reads? Something like this:

code:
retColour.a = tex2D(_MainTex, i.texcoord).a * tex2D(_Noise, i.sc).a;

Prior to trying either of these I was getting 25fps in the test scene I was using. With the second snippet it went to 35fps, so the branch is clearly one of the bottlenecks. With the first snippet I get 30fps and it looks just as good as my original. 5fps gain with no loss in quality that I can see. Thanks!

I would have assumed that a max() function would contain a branch in it. What wizardry lets that be faster than if()?

Bizarro Buddha
Feb 11, 2007
Instructions like fsel (and their equivalent in the GPU instruction set) let you put one of two values in a register without inserting pipeline bubbles or dealing with threads diverging.

seiken
Feb 7, 2005

hah ha ha
Probably the biggest performance gain would be to work out which game objects actually need the shatter effect each frame, and only use this shader for the ones that are actually shattering. Use a simpler (but equivalent) shader for the ones that are completely cohesive. (I'm assuming all game objects can shatter, but not all will be in the process of doing so each frame, which seems likely from what I remember you posting. If really everything is shattering all the time, I guess this isn't helpful.)

Contains Acetone
Aug 22, 2004
DROP IN ANY US MAILBOX, POST PAID BY SECUR-A-KEY

Contains Acetone fucked around with this message at 17:37 on Jun 24, 2020

ZombieApostate
Mar 13, 2011
Sorry, I didn't read your post.

I'm too busy replying to what I wish you said

:allears:
I was told at one point that lerp can do the same sort of trick, although I think I heard later that it can depend on hardware/drivers/API/whatever if it works or not.

Paniolo
Oct 9, 2007

Heads will roll.
How do I make Unity's editor camera controls not absolute dogshit? I don't know how people work in this.

edit: Okay that wasn't particularly constructive. But WASD only works intermittently at best (and yes I'm holding down RMB), and hitting 'F' instead of focusing the camera on whatever object tends to send it careening off in random directions, making the objects invisible.

edit 2: looks like 'W' fails if you're already holding shift. But only sometimes?

Paniolo fucked around with this message at 08:02 on Dec 17, 2013

poemdexter
Feb 18, 2005

Hooray Indie Games!

College Slice

Paniolo posted:

How do I make Unity's editor camera controls not absolute dogshit? I don't know how people work in this.

edit: Okay that wasn't particularly constructive. But WASD only works intermittently at best (and yes I'm holding down RMB), and hitting 'F' instead of focusing the camera on whatever object tends to send it careening off in random directions, making the objects invisible.

edit 2: looks like 'W' fails if you're already holding shift. But only sometimes?

Hold right mouse button to rotate around.
Hold middle mouse button to translate around.
Shift + hold middle mouse button to translate faster.
Scroll middle mouse button for zoom in/out.
F focuses on whatever you have selected in the hierarchy.
QWER buttons are hotkeys for the various things you can do to objects in scene view.

Holding right mouse button and WASD has never been intermittent for me, and hitting F has never given me trouble. Sounds like you have some really weird issues. Are you on latest?

DancingPenguin
Nov 27, 2012

I ish kakadu.
Hey folks!
Me and a couple of friends are developing an app (for iOS and Android) during the next 4 months.
I came by to check if anyone has used xARM, a resolution 'preview' plugin.
My current line of thought is basically just that this might speed up the process pretty much, since we have a deadline. I also don't own the many different devices needed for testing.
The main benefit would probably be to make sure our GUI etc is resolution-independent, and seeing more resolutions at the same time would be a nice thing.

Anyone got any experience with this very plugin or got any other recommended plugins?

Wilko
Dec 3, 2013

:toot:

Paniolo posted:

How do I make Unity's editor camera controls not absolute dogshit? I don't know how people work in this.

edit: Okay that wasn't particularly constructive. But WASD only works intermittently at best (and yes I'm holding down RMB), and hitting 'F' instead of focusing the camera on whatever object tends to send it careening off in random directions, making the objects invisible.

edit 2: looks like 'W' fails if you're already holding shift. But only sometimes?

Try using the orthographic camera mode, it acts like a standard orbit camera then. Click the cube of the view-cube to switch between the two.

Obsurveyor
Jan 10, 2003

poemdexter posted:

Holding right mouse button and WASD has never been intermittent for me, and hitting F has never given me trouble. Sounds like you have some really weird issues. Are you on latest?

The Scene window acts differently depending on whether the mouse cursor is inside it's rectangle or not. Make sure it is if you want to do stuff like F. If you want to use WASD, you have to right click on the window first.

It took me forever to figure out why F use was so inconsistent.

SurrealityCheck
Sep 15, 2012
Hey guys! Did anybody here do ludum dare?

antinumeric and I did the 72 hour game jam and made a little action platformer in unity (although apparently it's hard as balls, who'd have thought >_>).

http://www.ludumdare.com/compo/ludum-dare-28/?action=preview&uid=30298

Give it a look if you're interested. Unity 2d actually made it really pretty easy to do - all the code was written from scratch, and stuff like a checkpoint system was surprisingly easy to implement.

Paniolo
Oct 9, 2007

Heads will roll.

Obsurveyor posted:

The Scene window acts differently depending on whether the mouse cursor is inside it's rectangle or not. Make sure it is if you want to do stuff like F. If you want to use WASD, you have to right click on the window first.

It took me forever to figure out why F use was so inconsistent.

I think I've figured it out. If you hold down shift before pressing W it does not work; if you press W first it does. It felt intermittent because I'd hit shift and W at the same time, and depending on which registered first it would work or not work.

Shalinor
Jun 10, 2002

Can I buy you a rootbeer?

Paniolo posted:

I think I've figured it out. If you hold down shift before pressing W it does not work; if you press W first it does. It felt intermittent because I'd hit shift and W at the same time, and depending on which registered first it would work or not work.
If that's how it works for you, you would appear to have a keyboard with multi-keypress issues.

It doesn't matter which order you do shift + W. What does matter is that W is a shortcut for activating the translation tool, so if you press W when the right mouse key isn't down, you're using a shortcut, not moving. (Q is the Pan shortcut, W is the translation shortcut, E is the rotate shortcut and R is the scale shortcut)

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Shalinor posted:

If that's how it works for you, you would appear to have a keyboard with multi-keypress issues.

Its called 'ghosting'. :cheeky:

But shift ghosting is weird, usually even cheap keyboards put metas on a different rail...

Paniolo
Oct 9, 2007

Heads will roll.
I've got a high end gaming keyboard and only have that issue with the W key.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
Finally got a decent demo of my "volumetric 2D lighting" working. Versus the one in the asset store, mine supports total darkness (I don't know if his does or not, but none of the demos show it and I suspect it might work in a way that precludes it) and self-illuminating objects, and, probably more importantly, mine does almost no raycasting. The asset store one beats mine in its ability to have lights combine additively and be of different colors (both of which mine could easily do with Pro, but it's not worth the insane effort required to make it do that with the way it works when there's no rendertargets).

I think the soft edges on mine are also something the other one can't do without Pro.

Demo here. You can drag and select lights from approximately their center-point. The other objects aren't in any way the only things that would work with the lights, they're just samples of the three main kinds of 2D colliders - circle, box and poly. The shadows also work with paths, but I didn't bother showing those because the implementation is identical to a simple poly anyway, and paths are arse to draw since they have no area.

Cheston
Jul 17, 2012

(he's got a good thing going)
In Unity, I want a 2D AI to know when it's up against a wall. Which would be less resource-intensive, Raycasting or putting collision triggers on either side of the AI? I generally know the steps taken to check for collision with a box/sphere in 3D, but I've never used raycasting before, so I don't know whether that has more or fewer steps.

Zizi
Jan 7, 2010

Cheston posted:

In Unity, I want a 2D AI to know when it's up against a wall. Which would be less resource-intensive, Raycasting or putting collision triggers on either side of the AI? I generally know the steps taken to check for collision with a box/sphere in 3D, but I've never used raycasting before, so I don't know whether that has more or fewer steps.

My personal preference is for using Linecasts instead. Usually as part of a character controller that is then running a state machine that movement, animation and AI can all query for their own needs.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Cheston posted:

In Unity, I want a 2D AI to know when it's up against a wall. Which would be less resource-intensive, Raycasting or putting collision triggers on either side of the AI? I generally know the steps taken to check for collision with a box/sphere in 3D, but I've never used raycasting before, so I don't know whether that has more or fewer steps.
I would guess that the math for a sphere collision is quicker than the math for a raycast, but the overhead of collision triggers (which would require two additional gameObjects with components and probably scripts) is more than the overhead of a couple of raycasts in the Update function.

It might also be viable to use one of the Physics2D.OverlapCircle or Physics2D.OverlapPoint or Physics2D.OverlapArea series rather than a raycast.

But really, the difference in resource-intensive-ness of these options, unless you're having hundreds of things checking for collisions, is so small that you almost certainly shouldn't even be worrying about it. Just do whatever is easiest to get the result you want.

Lork
Oct 15, 2007
Sticks to clorf
Does anybody know why this pursue function fails to recognize that its target is ahead and facing it if they are nearby?

code:
public static Vector3 Pursue(GameObject self, Transform target, Vector3 targetVelocity)
{
	Vector3 deltaVector = target.position - self.transform.position;

	float relativeHeading = Vector3.Dot(self.transform.forward, Vector3.Normalize(targetVelocity)); //Dot product of the headings of ourself and the target

	if(Vector3.Dot(deltaVector, self.transform.forward) > 0 && relativeHeading < -0.95) //18 degrees
		return Seek(self, target); //If the target is ahead and facing us, simply move towards it

	float lookAheadTime = deltaVector.magnitude / 5.2755f + targetVelocity.magnitude;
	return Seek(self, target.position + target.forward * lookAheadTime);
}
It seems to work in most situations, but if the target gets within a couple units, the function will estimate the target's position as being behind the AI character and run away from the target.

Adbot
ADBOT LOVES YOU

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Lork posted:

Does anybody know why this pursue function fails to recognize that its target is ahead and facing it if they are nearby?

It seems to work in most situations, but if the target gets within a couple units, the function will estimate the target's position as being behind the AI character and run away from the target.
Function looks okay to me. Are the focus-points of the two objects actually inside the objects' colliders or sprites or whatever? Can you set up a situation where it happens and then hit the debugger and step through the code to see which value is going awry? (I spent so long doing that to handle loving floating point imprecision, setting up scenes to get reproducible glitches to occur and then codewalking to find where something wrong happened.)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply