Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075919 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:11:11 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Unity is great... Why don't I 'get it'?
Pages: [1]
Print
Author Topic: Unity is great... Why don't I 'get it'?  (Read 2935 times)
mickmaus
Level 1
*


walkying waylking


View Profile
« on: September 21, 2012, 07:56:26 PM »

Hey there,

I've made a couple of games in a couple of languages / environments. I've done the basic platformer thing in game maker, some top down asteroids stuff in Processing (~Java) and some things from scratch in AS3/FlashBuilder. I'm also a web developer by day, so it's safe to say I have a fair amount of programming experience.

I feel like I just don't 'get it' when it comes to doing stuff from scratch in unity. My ideal first project would be a 2d platformer. Just a guy jumping around and landing on boxes and ramps. Here's some problems i'm running into:

1. Resolving collisions step by step - If i'm making a sidescroller with ramps, i need to be able to do a lot of hit checks against the environments. I've written working code like this in other places, but I just don't get what parts of unity's environemnt to use! Should I cast four rays out from the corners of my cube? should I attach a second collider that's offset from my position and receive callbacks when that bonks into the walls? None of these solutions seem right, but obviously neither does using the built in physics because it's so fiddly and boils down to just shoving cubes.

2. Dispatching events to objects after collision - When I worked in AS3, i'd have each object in a collision call a hit function on the other object and pass it's self in as an argument. This let me do type checking and special cases galore. How should I approach this in Unity? I found references for calling functions on other objects but it seems like you have to know the Class (script?) name where the function lives. Obviously i want this to be generic so I can do something like calling otherCollider.hit(this); without knowing who the other collider was.

3. Control design patterns - In other environments I'd put objects into a few arrays and iterate over them every frame. Because I was manually drawing them to the screen I had to be pretty sure about the order things were happening. I haven't seen any higher level guides to control structures in unity. Should I try to put as much of the code in individual objects and just let them 'fight it out'? What options would I have if I tried to centralize control and store more information in empty game objects?

I realize this is long but I can't find any guides to the 'how' of unity. I'm happy to read the reference material (and do!) but simply having the pieces doesn't tell me a good way to using them. I'd love any pointers other than RTFM.

-Mick
Logged

Haite
Level 0
**


View Profile WWW Email
« Reply #1 on: September 21, 2012, 09:21:28 PM »

Doing things from sketch in Unity is something very troublesome, somethings it allow you to edit, somethings don't. I already had some very nasty problems with it, it is a wonderful engine, specialy for beginners, but if you want to go more deep, you may enconter some problems.

That said, my suggestions:
1. Unity have a tutorial about 2D games http://unity3d.com/support/resources/tutorials/2d-gameplay-tutorial that the character uses a character controller. It isn't from sketch, but because just began to use Unity I think it will save you a lot of headache.
2. There is an alternative that is send a message with the function that you need. http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html You can also tell Unity to do nothing with objects that have tags that you don't want, the others probably share the same script that you need.
3. You say that because of performance? That I can't be sure what is the best, I think that by the environment it suggest that you put the scripts in each objects, but I don't think that other ways are "wrong".
Logged
bateleur
Level 10
*****



View Profile
« Reply #2 on: September 22, 2012, 02:50:17 AM »

I'd recommend that you avoid fighting against the way that Unity does things. Sure, you could jump through awkward hoops to make it more like the previous framework you used... but that's not generally the best plan.

The way to get collision information in Unity for handling collisions manually is to add Colliders to the two objects you want to detect collisions between. Then set isTrigger to true for each of them and add an OnTriggerEnter function to a script attached to one of the objects.

Generally there's no need to mess around with events or messages or anything like that. Just using function calls is fine. At least keep it simple for your first project until you're a bit more comfortable with Unity.

If you want to exercise control over execution order of (eg.) FixedUpdate calls, that's easy to do: keep a list of objects, then give whatever holds the list a FixedUpdate function and none of the others. From within FixedUpdate, iterate over the list manually calling a function of your own (effectively "myFixedUpdate", but you can call it whatever seems clearest to you).
Logged

mickmaus
Level 1
*


walkying waylking


View Profile
« Reply #3 on: September 22, 2012, 10:23:26 AM »

Thanks both of you, the send message functionality answers some questions.

Maybe I should explain my specific problem:

I want two players who can move/ump and push a block around. When the block gets pushed into a ramp, I want it to slide up the ramp and I'd like to be able to set the horizontal speed on a case by case basis. (maybe 2:1 ramps don't slow it down but 1:1 there's a noticable speed reduction?).
Secondly, if the block is pushed off a cliff, I want to make it fall with a normal gravity feeling. However, If it's pushed off the edge of a normal block onto a ramp I want it to 'stick' to the ramp, meaning moving it down until it's touching the ramp instead of waiting a few frames for it to land there with gravity. (sort of 'cheating it' to make it feel heavy).

I'm not sure how i'd do the latter if I'm using a box collider that's tied to the game object. How do I check if the collider WOULD BE hitting something if I moved down from it's current position. (or in the case of moving into ramps, checking how many units i'd have to move up to be pushed up out of the ramp).

Am I crazy?
Logged

bateleur
Level 10
*****



View Profile
« Reply #4 on: September 22, 2012, 12:15:54 PM »

How do I check if the collider WOULD BE hitting something if I moved down from it's current position.

To be honest, that's all such simple geometry that I'd be inclined to just write my own collision checks by doing maths on the coordinates. However, if you want to use built-in functions you can always use raycasts downwards from the block's current position to work this out.
Logged

rhys_vdw
Level 0
***



View Profile Email
« Reply #5 on: September 22, 2012, 05:21:41 PM »

1. Resolving collisions step by step - If i'm making a sidescroller with ramps, i need to be able to do a lot of hit checks against the environments. I've written working code like this in other places, but I just don't get what parts of unity's environemnt to use! Should I cast four rays out from the corners of my cube? should I attach a second collider that's offset from my position and receive callbacks when that bonks into the walls? None of these solutions seem right, but obviously neither does using the built in physics because it's so fiddly and boils down to just shoving cubes.

Firstly, you don't need to calculate those collisions yourself. Just use a CharacterController or a kinematic Rigidbody. If you want to get a callback when a collision with a wall occurs, do something like this:

Code:
void OnCollisionEnter(Collision c) {
if (c.point.x > collider.center.x) {
OnHitRight(c);
} else if (c.point.x < collider.center.x) {
OnHitLeft(c);
}
}

Using the built in Collisions objects does not mean you need to use the physics system. Roll your own platformer physics. Here is a platformer script I wrote for Unity, it has lots of dependencies so you wont be able to run it, but you can look at the logic. Check out how I see which wall it's hitting in the Act() method.

2. Dispatching events to objects after collision - When I worked in AS3, i'd have each object in a collision call a hit function on the other object and pass it's self in as an argument. This let me do type checking and special cases galore. How should I approach this in Unity? I found references for calling functions on other objects but it seems like you have to know the Class (script?) name where the function lives. Obviously i want this to be generic so I can do something like calling otherCollider.hit(this); without knowing who the other collider was.

Actually that is already a feature on Unity3d. Every object that experiences a collision will automatically have its OnCollisionEnter method fired, it receives a Collision object with all the data you need in it. You can access the objects through the Collision object. ie Collsion.gameObject or Collision.transform.

However the exact functionality you're describing is available, although I never use it. Look at the methods SendMessage, SendMessageUpwards and BroadcastMessage (in MonoBehaviour).

So... Something like this:

Code:
public class Bomb : MonoBehaviour {
void OnCollisionEnter(Collision c) {
// call every method Disintegrate() in all scripts attached to other object
c.gameObject.SendMessage("Disintegrate");
Explode();
}

void Explode() {
print("BOOM!");
Destroy(gameObject);
}
}

3. Control design patterns - In other environments I'd put objects into a few arrays and iterate over them every frame. Because I was manually drawing them to the screen I had to be pretty sure about the order things were happening. I haven't seen any higher level guides to control structures in unity. Should I try to put as much of the code in individual objects and just let them 'fight it out'? What options would I have if I tried to centralize control and store more information in empty game objects?

The "Update" method on each objects is a callback from the engine iterating through all instantiated GameObjects. You don't need to write an engine in Unity because it is already a dedicated games engine. Basically the operation you're describing is already part of Unity.

In one of my first projects we had the entire game loop firing from a single Update method in one script. This is kind of double handling, and also makes your design far less flexible. There is definitely some adjusting to do, but once you get used to Unity idioms and patterns it becomes quite powerful.

Could you give me some examples of what you're trying to achieve that you don't see how to do in Unity?

edit: Don't know how to format hyperlinks apparently
« Last Edit: September 26, 2012, 04:10:06 AM by fecal_brunch » Logged

-Rhys
Danmark
Level 7
**



View Profile
« Reply #6 on: September 22, 2012, 06:55:35 PM »

Regarding 3, there's Edit -> Project Settings -> Script Execution Order, if you need to enforce an order of component updates.

Seconding fecal_brunch (best username) on 1 and 2. You'll never hack an existing realistic physics engine through its public interface to do exactly what you want. Roll your own.
Logged
mickmaus
Level 1
*


walkying waylking


View Profile
« Reply #7 on: September 22, 2012, 09:08:34 PM »

AWESOME! Reviewing your code right now Mr fecal_brunch!
Logged

Wilson Saunders
Level 5
*****


Nobody suspects the hamster


View Profile WWW
« Reply #8 on: September 24, 2012, 12:26:22 PM »

Thanks both of you, the send message functionality answers some questions.

Maybe I should explain my specific problem:

I want two players who can move/ump and push a block around. When the block gets pushed into a ramp, I want it to slide up the ramp and I'd like to be able to set the horizontal speed on a case by case basis. (maybe 2:1 ramps don't slow it down but 1:1 there's a noticable speed reduction?).
Secondly, if the block is pushed off a cliff, I want to make it fall with a normal gravity feeling. However, If it's pushed off the edge of a normal block onto a ramp I want it to 'stick' to the ramp, meaning moving it down until it's touching the ramp instead of waiting a few frames for it to land there with gravity. (sort of 'cheating it' to make it feel heavy).

I'm not sure how i'd do the latter if I'm using a box collider that's tied to the game object. How do I check if the collider WOULD BE hitting something if I moved down from it's current position. (or in the case of moving into ramps, checking how many units i'd have to move up to be pushed up out of the ramp).

Am I crazy?

What you want to do can be accomplished with the basic unity components, if you know how to work with them.

For character movement use the CharacterController and its Move() function instead of transform.position . This function will attempt to move the character along the vector you provide until it hits a collider. If a collider is encountered this function will respond in a pretty natural way (I could try and explain it, but it is better if you just learn through experimenting). If you want pushable boxes in the game world, attach a box collider and a RigidBody to the box's GameObject and it will take advantage of Unity's physics engine. You will have to do some tweaking of Physics.gravity, and the RigidBody's drag values to get the feel right, but it is a lot simpler than writing your own physics code.
Logged

Play my games at http://monkeydev.com/
bateleur
Level 10
*****



View Profile
« Reply #9 on: September 25, 2012, 04:32:09 AM »

You will have to do some tweaking of Physics.gravity

You shouldn't need to change this value. If you find yourself doing this, it's probably because your objects are the wrong sizes. For example, if you find you need double gravity to make things feel right, instead leave gravity where it is and make all your objects half the size.

(Changing gravity may seem like the easier option, but other parts of the physics engine make less obvious assumptions about object size too.)
Logged

rhys_vdw
Level 0
***



View Profile Email
« Reply #10 on: September 26, 2012, 04:07:58 AM »

AWESOME! Reviewing your code right now Mr fecal_brunch!

I wrote that platformer script at a game jam, so it's lacking comments and stuff. I think the Update() function should be fairly informative though.
Logged

-Rhys
forwardresent
Level 5
*****


UNINTENTIONALLY DEADPAN.


View Profile Email
« Reply #11 on: September 26, 2012, 05:13:08 AM »

Unity is interesting. It's what I'm currently being taught to use. I like how it has multi language support and the tagging/prefab features are very handy. There is also plenty of help available online, in books, et cetera.

I've had my fair share of UNITY WHAT THE HELL moments though.
Logged
mickmaus
Level 1
*


walkying waylking


View Profile
« Reply #12 on: October 22, 2012, 01:37:41 AM »

Thanks all! I've been playing with unity and making neat stuff!

http://www.youtube.com/watch?v=Ip5upEMwmxI

http://www.youtube.com/watch?v=S8gIk7_2_D0
Logged

Chromanoid
Level 10
*****



View Profile
« Reply #13 on: October 22, 2012, 09:13:27 AM »

Maybe interesting for you: http://www.altdevblogaday.com/2011/05/23/the-top-6-misconceptions-i-had-about-unity/
Logged
B_ill
Level 0
***


View Profile WWW Email
« Reply #14 on: October 23, 2012, 07:28:21 PM »

Unity is interesting. It's what I'm currently being taught to use. I like how it has multi language support and the tagging/prefab features are very handy. There is also plenty of help available online, in books, et cetera.

I've had my fair share of UNITY WHAT THE HELL moments though.

I've had (and still discover) quite a few UNITY WHAT THE HELL moments, too.

The good news is...usually it means I wasn't doing something the "proper" way, and by researching the issue I've found there is already an elegant and robust solution lurking in the shadows and waiting for me in every case.
Logged

Game Programmer and Designer
Latest Release: Chemical Cubes for Android and Kindle Fire (iOS coming soon)
Hima
Level 4
****


OM NOM NOM


View Profile WWW Email
« Reply #15 on: October 24, 2012, 09:56:01 AM »

Sometime the proper way is so tiring though. There are times that I wish I could just instantiate an object normally instead of having to go through all the prefab creations and linkings :-/

As for control flow, you can still do that though. Although, this would mean you ignore the MonoBehaviour's Update method completely. This is require in some games though, especially if you need more control over the order of things or you are looking for a deterministic game loop. There is a 2d game framework in Unity called Futile that does this. The framework ignore Unity system and implements its own event system based on Unity. It's open source so you can take a look at how it works.
Logged

mickmaus
Level 1
*


walkying waylking


View Profile
« Reply #16 on: October 27, 2012, 01:44:46 PM »


THIS! Thanks!
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic