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.
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!
Your answer
Follow this Question
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