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 /
avatar image
0
Question by Mojo-san · Nov 29, 2015 at 09:53 AM · scripting problemattack

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

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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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

I want to use Scriptable objects to create enemy attacks and swap them out each turn. How do I access another SO and replace the one in the object? 0 Answers


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