Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Mrroundtree22 · Feb 04, 2016 at 11:45 PM · animationspritetriggerfpsscriptingbasics

First person shooter : 2d sprites

This is a bit hard to explain but bare with me. So I just played a game called "Ritual For Coakkan".

Here's a gif from the game : https://www.freegameplanet.com/wp-content/uploads/2015/08/ritual-for-coakkan.gif

As you can see the arms appear to be a 2D sprite animating infront of the camera reminisant of games like "Doom" and "Wolfenstein 3D".

I apologize if this sound flimsy I will try to explain better if you ask me. Is there a basic script that when prompted by a button press plays the sprite animation and at the same interact with the world around.

Also I am a beginner so please explain it to me as simple as you possibly can.

Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Al_Goyle · Feb 09, 2016 at 08:08 AM

Hello! Some basics:

to detect a button press from the player you can do this in the Update loop of a script:

 void Update () {
     // whenever you press A...
     if (Input.GetKeyDown(KeyCode.A)) {
          // the commands inside this statement are executed
     }
 }

to detect a mouse press you want to use Input.GetMouseButtonDown() All the ways to retrieve players input are in the Input class: http://docs.unity3d.com/ScriptReference/Input.html It would be a good idea to browse the Unity Scripting API docs to familiarize with the codes and basic commands.

To play a sprite animation, you can use an array of images (.png), and you will cycle between the images over time.

For example, in unity you can create a new 3D Objects > Quad. It is a planar mesh that you can dispose in front of your camera. The Quad has a material. We are going to put a script on this object that will make the material Texture change over time, through an array of Texture. Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class sr_AnimTexture : MonoBehaviour {
     
     public Texture[] textures;
     public bool pingpong;
     public bool oneShot;
     public bool andDestroy;
     public float intervale;
     public bool doSpecificIntervales;
     public float[] specificIntervales;
     
     private float timer = 0f;
     private float interv;
     private int total;
     private int current;
     private bool reverse = false;
 
         private Material myMaterial;
     
     void Start () {
                         myMaterial = GetComponent<Renderer>().material
             myMaterial.mainTexture = textures[0];
             total = textures.Length;
             current = 0;
             interv = 0.01f;
         }
     }
     
     void Update () {
 
         timer += Time.deltaTime;
         if (timer >= interv) {
             timer = 0f;
             myMaterial.mainTexture = textures[current];
             if (doSpecificIntervales) {
                 interv = specificIntervales[current];
             }else {
                 interv = intervale;    
             }
             if (!reverse) {
                 current += 1;
             }else {
                 current -= 1;    
             }
             if (current == total) {
                 if (!pingpong) {
                     current = 0;
                     if (oneShot) {
                         if (andDestroy) {
                             if (transform.parent.gameObject != null) {
                                 Destroy(transform.parent.gameObject);
                             }else {
                                 Destroy(gameObject);
                             }
                         }
                         GetComponent<Renderer>().enabled = false;
                         isEnabled = false;
                         timer = 0f;
                     }
                 }else {
                     reverse = true;
                     current = total-2;
                 }
             }
             if (current == 0) {
                 reverse = false;    
             }
         }
     }
 }

This script is more convenient for environmental looping animation (like a torch animating) or instantiated FX objects that get destroyed once played (like a blood burst animation)... but you can play with it to understand the code.

But what you want is the input to affect the texture the Arms Quad is playing, so put this script on the Arms Quad object:

 using UnityEngine;
 using System.Collections;
 
 public class sr_PlayerArms : MonoBehaviour {
 
     //exposed in inspector
     public Texture[] anim_idle; 
     public Texture[] anim_action1;
     public float frameInterval;
     
     //anim vars
     private float frameTimer = 0f;
     private int currentFrame = 0;
     private Texture[] currentAnim;
 
     //references of components or other objects than myself
     private Material myMaterial;
 
 
 
     void Start () {
         myMaterial = GetComponent<Renderer>().material;
         currentAnim = anim_idle;
     }
 
 
     //Executed every frame
     void Update () {
         //Detect input left mouse button
         if (Input.GetMouseButton(0)) {
             currentAnim = anim_action1;
         }else {
             currentAnim = anim_idle;
         }
 
         //Update the display every frame
         MaterialDisplay();
     }
 
 
 
     private void MaterialDisplay ()
     {
         frameTimer += Time.deltaTime;
         if (frameTimer >= frameInterval) {
             frameTimer = 0f;
             currentFrame += 1;
             if (currentFrame >= currentAnim.Length) {
                 currentFrame = 0;
             }
         }
         myMaterial.mainTexture = currentAnim[currentFrame];
     }
 
 
 }

This is a super basic implementation and untested (but hopefully should work... only issue would be getting over the array index if I screwed up the timing... well...) What it does is Maintained mouse left click plays the action1 animation. Not clicking plays idle animation.

For better implementation of inputs and player actions in relation to animation it is generally necessary to have more control over the timing: exactly when during the animation a sound, animation, or any logic consequence should happen... knowing when the animation ends so that we can go back to the idle state. For all those things, I found Coroutine to be really handy. You wan check that in the unity docs. They allow great timing control of an action, especially when you know this action has no reason to be interrupted.

Sorry for a somewhat vague answer but your question is too! You'll have to explain what you mean about 'interact with the world around'. There is a million ways to do so.

Good day!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mrroundtree22 · Feb 11, 2016 at 09:07 PM 0
Share

Thank you very much

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

78 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Playing a multistage door animation with triggers. 0 Answers

Animations Bad Scripting | Controller Script Buggy | Collider Problem | Beginner Here! 0 Answers

Interchangeable animations in a script? 0 Answers

How do I find the direction I'm going for my blend tree? 0 Answers

Animation Styles: Pre-created sprites or rigging individual sprites? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges