- Home /
Script problem : beat'em up
Hello, I'm quite a noob using C# and I want to create a melee attack in a beat them up game. I use a script from a 2d platformer but i've got 2 problems, the first one is that I put a coroutine in my script and this coroutine fails to reload after one attack, the other problem is that my attack represented as a gameobject is created on the same location as my character and I want it to spawn in front of my character. This is the script I use : using UnityEngine; using System.Collections;
public class Player : MonoBehaviour { // Déclaration de la variable de vitesse public float m_speed = 0.1f; public int jumpPower = 350; // Puissance saut private bool canJump = true; // Peut on sauter ?
bool canFire = true;
fire scriptFire;
enum dir
{
LEFT = -1,
RIGHT = 1
};
dir direction = dir.LEFT;
// Use this for initialization
void Start () {
scriptFire = GetComponentInChildren<fire>();
}
// Update is called once per frame
void Update()
{
// Création d'un nouveau vecteur de déplacement
Vector3 move = new Vector3();
// Récupération des touches haut et bas
if (Input.GetKey(KeyCode.UpArrow))
move.z += m_speed;
if (Input.GetKey(KeyCode.DownArrow))
move.z -= m_speed;
// Récupération des touches gauche et droite
if (Input.GetKey(KeyCode.LeftArrow))
move.x -= m_speed;
if (Input.GetKey(KeyCode.RightArrow))
move.x += m_speed;
// On applique le mouvement à l'objet
transform.position += move;
if (Input.GetKeyDown(KeyCode.Space))
{
if (canJump)
{
canJump = false;
StartCoroutine(jump()); // On saute
}
}
if (Input.GetKeyDown(KeyCode.LeftControl) && canFire == true)
{
canFire = false;
StartCoroutine("reload");
scriptFire.shoot((int)direction);
}
}
IEnumerator jump()
{
GetComponent<Rigidbody>().AddForce(Vector3.up * jumpPower);
yield return new WaitForSeconds(1);
canJump = true;
yield return new WaitForSeconds(0.5f);
canFire = true;
}
}
And this is the second part of the script :
using UnityEngine; using System.Collections;
public class fire : MonoBehaviour {
public GameObject missile;
public void shoot(int dir)
{
GameObject go = Instantiate(missile, transform.position, Quaternion.identity) as GameObject;
go.GetComponent<Rigidbody>().velocity = new Vector2(4 * dir, 0);
if (dir == -1)
go.transform.localScale = new Vector3(go.transform.localScale.x * -1, go.transform.localScale.y, 1);
Destroy(go, 0.33f);
}
}
Thank you Mojo
Your answer
Follow this Question
Related Questions
how to make 2D platformer AI for a skeleton 0 Answers
Help with making special attacks? 0 Answers
how to make an AI damage a third person controller ? 1 Answer
Melee Attack 1 Answer