Using Unity and C# to create a survival game

Soldato
Joined
8 Jan 2012
Posts
2,802
Okay so this has been an on going project for a while now. I've changed in a proper model compared to the first person controller capsule graphic... Now this model is animated and uses an animator controller and an animation clip. I've never used this way to animate in Unity before and it's perplexed me... I've looked all over the web to no avail.. Hoping someone here has an idea.

Also.. I want to use a mesh collider with the model however for whatever reason the mesh collider orients 90 degrees to the y axis and I'm unsure of how to fix this... If I manage to however I may be able to ditch the crappy controller I've been using and instead use physics to do movement as that would be preferred.. (currently for whatever reason the capsule collider doesn't properly move across the terrain... catches a lot and causes havock with the camera... I can fly but it's not really what I want...)

Any help is appreciated. Thanks!
 
Soldato
OP
Joined
8 Jan 2012
Posts
2,802
First person or third person? Either way creating the state machine in mecanim should be pretty straightforward....what in particular are you having trouble with?

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/animate-anything

Sure you need a mesh collider? It's usually best to use a few primitive box/sphere/capsule colliders.

I just looked everywhere on the internet (having no experience with the animator controller at all) I actually found the official unity youtube lesson on it so thats all cleared up now. Erm I could not use a mesh. However the controller capsule collider is pretty screwed up currently and I'd like to move away from it if I could (what with it not being the best in the world anyway) if I move my camera to look down it interacts with the collider (for some reason?) and boom.. Character goes flying.
 
Soldato
OP
Joined
8 Jan 2012
Posts
2,802
Hard to say without seeing your code. It's probably easiest to just make your own controller, the knowledge required to write a simple controller is the minimum you'll need to make anything in Unity so it's a worthwhile learning exercise, only a few lines of code too.

Yeah I know that much I've just been focusing on more advanced implementation. Terrain generation with noise fields etc. I think the issue is because the camera causes the player model to tilt back/forth whilst looking up and down. Bit annoying really.
 
Soldato
OP
Joined
8 Jan 2012
Posts
2,802
Okay (instead of making a whole new topic... I thought I'd just stuck this here) I have an issue. I have created an inventory (which works perfectly) and an equipment inventory (half and half working...) So, I have an item.. I right click the item, in code there is 1 call (debugged and logged out several times) ran over all relevant code pertaining to this section.. But for whatever reason some how the 1 call fills the equipment part with 5 bows... (It should be one).

Relevant code:

Inventories equip function:
Code:
private void Equip(Item item, int slot, bool deleteItem)
    {
        switch(item.itemID)
        {
            case 12:
                {
                    for (int i = 0; i < transform.GetComponent<Equipment>().equipment.Count; i++ )
                    {
                        
                        if(transform.GetComponent<Equipment>().equipment[i].type == ItemType.DEFAULT)
                        {
                            
                            transform.GetComponent<Equipment>().AddItem(item.itemID);
                            
                            RemoveItem(item.itemID);
                        }
                        break;
                          
                    }
                    break;  
                }
        }
    }

Equipments addItem function:
Code:
public void AddItem(int ID)
    {
        for(int i = 0; i < equipment.Count; i++)
        {
            if(equipment[i].type == ItemType.DEFAULT)
            {
                for(int j = 0; j < database.items.Count; j++)
                {
                    if (database.items[j].itemID == ID)
                    {
                        equipment[i] = database.items[j];
                    }
                }
                break; 
            }
        }
    }

And finally a snippet from Inventories OnGUI:
Code:
if (e.isMouse && e.type == EventType.mouseDown && e.button == 1)
                        {
                            if(slots[i].type == ItemType.WEAPON)
                            {
                                Equip(slots[i], i, true);
                            }

                            if(!slots[i].isConsumable)
                            {
                                UseConsumable(slots[i], i, false);
                            }
                            else if (slots[i].isConsumable)
                            {
                                UseConsumable(slots[i], i, true);
                            }
                            
                            
                        }

Any help appreciated thanks!
 
Soldato
OP
Joined
8 Jan 2012
Posts
2,802
First....



Bad code monkey! Magic number....

Secondly...have you stepped through the code? Chances are the problem is here...



Why are you calling transform.getComponent? I'm not sure what transform is in this context but I'm assuming it's a GameObject's member Transform transform

12's a magic number? I had stepped over the code millions of times (I haven't slept it's one of those days!) But this was driving me nuts for HOURS. Literally just redid the script and everything seems to be alright now. transform in this case relates to the player I suppose i could use gameObject.getComponent instead?
 
Soldato
OP
Joined
8 Jan 2012
Posts
2,802
12 is a magic number. It's just what coder's use to describe literals in code. 12 is a mystery number, it obviously needs to be 12, but there's no indication of what it is, why it is, what it means etc. Use an Enum to give it a meaningful name in code.

Yeah it doesn't make sense to use the transform's GetComponent<>() in this case. You can just use this.GetComponent<>() or just GetComponent<>().

It erm.. has to be an integer due to it being an ID value (I'll just cheat and change it to something else if you insist) everything seems alright now... Time to try and line up a bow with this models hand and see if I can get my animations working better.
 
Soldato
OP
Joined
8 Jan 2012
Posts
2,802
It can still be an integer, the problem is that that bit of code doesn't mean anything to anyone....it's just....'12'.

What you'd normally do is create an enum :

Code:
public enum ItemID
{
       Pitchfork = 1;
       Sword = 2;
       Gun = 12;
}

Then in your code you can write :

Code:
switch(item.itemID)
{
     case ItemID.Gun:
                {

Etc.

It relates to a different script. I should probably comment it up a bit (bad habit of not commenting I know). or enum it. I'm just doing the whole first draft thing. Okay so heres one for you. I have an itemDatabase two scripts are currently using it. It has a size of 21 (it's alist of items attatched to an empty gameObject in the scene). Now the two scripts currently using it return 21. The third script I'm trying to use it with returns 0. Referenced exactly the same way as the first two. Even tried directly attatching it to the gameObject it'll be used on. Been at this for a couple of hours now.. can't find a way to fix it for the life of me.

Edit: and as if by magic... it fixed itself.
 
Last edited:
Back
Top Bottom