benArcen

Unity Questions Thread

Recommended Posts

Ooh, great resources.

@clyde: That seems more 3D focused and in depth but maybe I can extract and simplify it for a 2D plane mesh... I'll probably need a quick C# lesson first

@itsamoose: That is a great demonstration of random principles from Spelunky. I had no idea how limited the range technically is. Even though I noticed the rooms, I had thought the layout of the rooms were almost totally random. I must now ponder the logistics of making this work in a non tile based system, I'm certain it can be done I just need to discern how to go about it.

Share this post


Link to post
Share on other sites

Do you know what you want to ground's shape to look like?

You could probably get a decent amount of terrain variation in a 2D game by drawing some large sprites with odd shapings on all sides and then overlapping them and rotating them random amounts. This would require you to use only one color for the ground though (if you don't want the player to see that the ground is made by the same shapes rotated different amounts).

Another similar way to cheat would be to make an oddly shaped 3D column, lay it on it's side and then give it a random rotation on the world's x-axis before the game starts. If the two poles of the column are identical in width, you could probably use the same one over and over again, attached end-to-end.

Share this post


Link to post
Share on other sites

Even if the world isn't tile based, it helps to think of the world in that structure.  From my experience random level generations comes in 2 general varieties.  The first being like spelunky, diablo, etc where you have a series of pre-made pieces that you scale, rotate, and position as necessary.  The second would be like Civilization, Banished, etc where you generate a world then adjust or modify portions of it in order to get a more organic effect.  Either way you'll need to be able to define what a "world" is and what elements can or should be contained within that world.  From what I can tell the first scenario is more common in action games (so the play spaces can be carefully defined) and the second is more common in management or larger scope games.

Share this post


Link to post
Share on other sites

Also, minor gripe about much less important thing. Unity seems to not support regular Arrays? I don't understand how that works, but apparently if I have an array full of numbers I can't evaluate them mathematically because it automatically just thinks an array is full of objects. Am I doing it wrong or is unity just kinda weird here?

 

What do you mean exactly? Arrays work like they should in C# (http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx), it has nothing to do with Unity really. Or maybe you mean something else, e.g. some Unity editor integration issue?

Share this post


Link to post
Share on other sites

Oh I meant in Javascript, so it might be language specific. Basically making a JS array has special syntax in Unity (if you use the normal syntax it produces an error) and when I call an element from an array in Unity it's declared all the elements as objects, even if the array is just full of numbers.

Share this post


Link to post
Share on other sites

Has anyone else had trouble trying to attach multiple audio sources to the same object?

I have 3 different song tracks tagged onto a camera each in their own separate audio source component.

My intention is to switch between them depending on the current game state.

 

I can add all three components, and then I declare variables that reference each individual component. But then when I try to call for one track to stop and another to start, it breaks entirely and no music plays. If I comment out the calls to stop one track and start another then the initial play command works fine, so I think there's some weird iffy thing where either Unity breaks or I'm meant to take some special step that I've missed out on.

 

Has anyone dealt with this before? Maybe there's a better way I could be approaching this regardless.

 

Just in case, here's my code

 

var script: PlayerControl;
script = FindObjectOfType(PlayerControl);
var waveMusic: AudioSource;
var gameMusic: AudioSource;
var endMusic: AudioSource;

function Start () {
	waveMusic.Play();
}

function Update () {
	if (!script.endWave){
		waveMusic.Stop();
		gameMusic.Play();
	}
}

 

Share this post


Link to post
Share on other sites

Has anyone else had trouble trying to attach multiple audio sources to the same object?

I have 3 different song tracks tagged onto a camera each in their own separate audio source component.

My intention is to switch between them depending on the current game state.

 

I can add all three components, and then I declare variables that reference each individual component. But then when I try to call for one track to stop and another to start, it breaks entirely and no music plays. If I comment out the calls to stop one track and start another then the initial play command works fine, so I think there's some weird iffy thing where either Unity breaks or I'm meant to take some special step that I've missed out on.

 

Has anyone dealt with this before? Maybe there's a better way I could be approaching this regardless.

 

Just in case, here's my code

 

var script: PlayerControl;
script = FindObjectOfType(PlayerControl);
var waveMusic: AudioSource;
var gameMusic: AudioSource;
var endMusic: AudioSource;

function Start () {
	waveMusic.Play();
}

function Update () {
	if (!script.endWave){
		waveMusic.Stop();
		gameMusic.Play();
	}
}

 

 

 

If you are trying to do what I think you are trying to do, then I would just have one audio-source and change the audioclip when you want to switch songs or sound effects.

 

http://docs.unity3d.com/ScriptReference/AudioSource-clip.html

Share this post


Link to post
Share on other sites

Oh, thatd probably work! It actually didn't occur to me to switch the clip itself. Though, it wouldn't be possible to play two tracks at once then, would it? To crossfade from one track to another. That's not the most important feature but I had intended to do so, to blend smoothly between songs.

Share this post


Link to post
Share on other sites

Oh, thatd probably work! It actually didn't occur to me to switch the clip itself. Though, it wouldn't be possible to play two tracks at once then, would it? To crossfade from one track to another. That's not the most important feature but I had intended to do so, to blend smoothly between songs.

 

http://forum.unity3d.com/threads/audio-crossfade-how.144606/

Share this post


Link to post
Share on other sites

I'll stop posting here eventually, I promise.

 

But right now I have a conundrum and I think it's either too easy to appear on google or too much to ask for it to be simple.

 

Basically I have a 2D rigidbody (the player) that reacts to collisions with both its normal velocity and angular velocity. I also have a bunch of other 2D rigidbodies moving around the player and often colliding with them, but these other rigidbodies don't get affected by angular velocity, they're just constantly facing the angle I first set them to when they were spawned. I haven't turned on fixed angle.

I'm not actually sure why this is occuring, if it's normal default behaviour and I'd need to enable the angular response or I somehow adjusted a setting to make them rigid this way.

 

Anyone else got thoughts on what I could do to fix this or ways I might have accidentally screwed myself with it?

Share this post


Link to post
Share on other sites

I'll stop posting here eventually, I promise.

 

But right now I have a conundrum and I think it's either too easy to appear on google or too much to ask for it to be simple.

 

Basically I have a 2D rigidbody (the player) that reacts to collisions with both its normal velocity and angular velocity. I also have a bunch of other 2D rigidbodies moving around the player and often colliding with them, but these other rigidbodies don't get affected by angular velocity, they're just constantly facing the angle I first set them to when they were spawned. I haven't turned on fixed angle.

I'm not actually sure why this is occuring, if it's normal default behaviour and I'd need to enable the angular response or I somehow adjusted a setting to make them rigid this way.

 

Anyone else got thoughts on what I could do to fix this or ways I might have accidentally screwed myself with it?

 

I would play with the masses of the different objects, but I've never done anything like your submarine game, so I'm not sure if there is something weird about how collisions effect angular velocity.

 

Personally, I don't mind your questions at all. It's interesting to see what you are working on in such detail.

Share this post


Link to post
Share on other sites

I actually did get it to randomly vary the mass a bit, but it still seems like the fish literally never rotate except for when I have them coded to rotate to an angle within the start function.

 

I mostly feel bad that I've been the one to ask the last three questions in a row, haven't I? Though it's funny to now imagine a devblog where each post explains a problem to be solved but instead of laying out how I solve it, it ends with me saying "But I dunno how to do that. Can any of you reading this help?"

Share this post


Link to post
Share on other sites

An unsatisfying update to my issue, I found out that it's an issue with using the LookAt function (which adjusts an object's rotation to point it at a certain point) that makes it rotate the collider into a different plane and renders it useless. My googling reveals that it's a bit of a mess trying to use LookAt in 2D space, so I'll probably manually code a system to calculate the angle. Which requires a bit more thought back to Maths class and rejiggery in the 3D space as I sort through other people's solutions but at least I understand it totally now..

Share this post


Link to post
Share on other sites

An unsatisfying update to my issue, I found out that it's an issue with using the LookAt function (which adjusts an object's rotation to point it at a certain point) that makes it rotate the collider into a different plane and renders it useless. My googling reveals that it's a bit of a mess trying to use LookAt in 2D space, so I'll probably manually code a system to calculate the angle. Which requires a bit more thought back to Maths class and rejiggery in the 3D space as I sort through other people's solutions but at least I understand it totally now..

 

I had to do this yesterday and ended up using (more or less) the 1st answer here. This code specifically rotates to the mouse but is easy to change. http://answers.unity3d.com/questions/585035/lookat-2d-equivalent-.html

 

Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

diff.Normalize();

 

float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;

transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);

 

In the past I've also done stuff like use LookAt() normally, then reset the X/Y rotations in the euler angle to 0 (or whatever their starting value was. 

 

Anyway, I was about to start working on UI for my game yesterday, but I can't decide if I should be working in the 4.6 beta or not. I'm hoping to release early next year sometime, so I'm concerned that I'll want to release while Unity is still unreleased. Anyone have thoughts on this? My feeling is that probably I should be responsible and use the released UI stuff but I...don't want to, cause it's terrible?

Share this post


Link to post
Share on other sites

I've tried using those methods but had issues trying to do that while making AddForce(transform.forward) work correctly. It seems to push the forward vector to the entirely wrong plane, meanwhile up and right (the other two potential directions) are no use to me. I think it might be easier if I just readjust the whole system to use 3D colliders and physics instead of 2D. Part of my googling suggests that the performance loss is actually pretty minimal anyway.

Share this post


Link to post
Share on other sites

Anyway, I was about to start working on UI for my game yesterday, but I can't decide if I should be working in the 4.6 beta or not. I'm hoping to release early next year sometime, so I'm concerned that I'll want to release while Unity is still unreleased. Anyone have thoughts on this? My feeling is that probably I should be responsible and use the released UI stuff but I...don't want to, cause it's terrible?

The new UI system is so much better. I don't know when 4.6 will be released, but the only problem I've seen is the watermark on the webplayer builds. There is no watermark on the PC builds. 

I suppose another option would be using NGUI or some other current solution.

Share this post


Link to post
Share on other sites

I've tried using those methods but had issues trying to do that while making AddForce(transform.forward) work correctly. It seems to push the forward vector to the entirely wrong plane, meanwhile up and right (the other two potential directions) are no use to me. I think it might be easier if I just readjust the whole system to use 3D colliders and physics instead of 2D. Part of my googling suggests that the performance loss is actually pretty minimal anyway.

 

The issues you have sound like they could have something to do with mixing up axes maybe? 2D stuff including 2D physics is happening on the XY plane but the default 3D ground plane is XZ in Unity (in the sense the default 3D view and various 3D shapes are oriented this way by default).

 

Maybe it's a completely silly suggestion. I think some screenshots and/or code fragments would help here. It's difficult to guess what's the problem without seeing some in-depth details about what you are doing and what's happening exactly.

Share this post


Link to post
Share on other sites

but the only problem I've seen is the watermark on the webplayer builds

Which you can get rid of, anyway, by editing the HTML file.

 

EDIT: Oops just realized you might not be referring to the "this build is beta" text on the webpage. NEVERMIND.

Share this post


Link to post
Share on other sites

4.6 should be officially released before your game is ready. I'd just go with it

Share this post


Link to post
Share on other sites

Which you can get rid of, anyway, by editing the HTML file.

 

EDIT: Oops just realized you might not be referring to the "this build is beta" text on the webpage. NEVERMIND.

 

Yeah, there's also a little "made with 4.6 beta" tag in the bottom right corner of the Unity player. Maybe not a big deal since it seems to be web only and I'd be on iOS.

 

4.6 should be officially released before your game is ready. I'd just go with it

 

I read on a couple random forum threads that it'd be out this fall, it's just creeps me out a bit that Unity never puts any official release dates out.

Share this post


Link to post
Share on other sites

I've tried using those methods but had issues trying to do that while making AddForce(transform.forward) work correctly. It seems to push the forward vector to the entirely wrong plane, meanwhile up and right (the other two potential directions) are no use to me. I think it might be easier if I just readjust the whole system to use 3D colliders and physics instead of 2D. Part of my googling suggests that the performance loss is actually pretty minimal anyway.

 

Vector3 transform.forward is always (0,0,1), which in 2D is the direction the camera is facing going deeper into your scene. I think what you actually want is...transform.right? or transform.up? I kinda borked this up when I tried to implement it the other day so I can't exactly teach it, but the snippet I pasted ended up fixing things for me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now