Search Unity

trigger different animations based same keypressed like mining,gathering, chopping using f key

Discussion in 'Scripting' started by voc007, Apr 19, 2019.

  1. voc007

    voc007

    Joined:
    Nov 18, 2012
    Posts:
    8
    How i can I write a script using same key (example "f") to play animation based on what the trigger is.
    Example a rock trigger plays mining animation with the f key
    or a tree trigger plays chopping animation with the f key
    or gather flowers with f key plays gathering animation

    so basically using same key for different animations / actions.

    I can not find any info. thanks
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Your question is more than just playing the animation. I assume you'd need to do various things like give the user some of the resource, check whether they're allowed to farm that resource (maybe they don't have an axe), etc. What you're asking for overall can get fairly complicated, depending on how you want things to feel. Do you automatically align the player with the resource and lock them in place while they farm it? Is it instantaneous, or do you only reward the resource when the animation is complete? Can the player interrupt the gathering and run away without getting anything?

    So, this is a non-trivial bit of work. But to get started, the simplest thing would probably be to perform a raycast in your player's Update method that checks whether any farmable objects are immediately in front of the player. The object you hit would have a script on it explaining what kind of resource it is. If you hit something, you'd GetComponent() to get that script and check what kind of resource it is. Then you can listen for player input, and start gathering that resource based on the kind of resource it is.

    There are quite a few moving pieces to this. I'd start by doing the raycasting and simply detecting when different kinds of objects are nearby.
     
  3. voc007

    voc007

    Joined:
    Nov 18, 2012
    Posts:
    8
    Okay thanks for your information, pretty much gathering resources with same key pressed, chop the tree with an axe, mine the rocks with the pickaxe, gather pumpkins with no tools , all using same key, as 'f' key, that is what I am trying to achieve. All this will be in 3d, and have different animations for each action, the character would have chopping animation if doing woods, etc. And of course what ever the said item spawns, example 2 woods, that character gets the loot, and goes into the inventory for crafting.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Well, you're basically asking how to build a complete major component of a game. Like I said, start small. Figure out how to raycast in front of the player to detect different kinds of objects (tree, rock, etc) and go from there.
     
  5. voc007

    voc007

    Joined:
    Nov 18, 2012
    Posts:
    8
    will do, thanks