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 /
This question was closed Jun 24, 2016 at 03:42 PM by Mr_Truz for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Mr_Truz · Jun 24, 2016 at 12:12 PM · 2d game2d-gameplay

Throwing a sword

Hey guys, I'm making a 2D RPG for a school class. One of the things I need to do it's that my player can throw an object (a sword in my "game"), so, I have this two scripts (first the player movement script, second the sword prefab script); When the player is facing right the sword goes right, when is facing left, the sword goes left, how can I make it? Thank you so much.

 public class playermovement : MonoBehaviour {
     
     int contador;
     public Text Puntuacion;
 
     Rigidbody2D rbody;
     Animator anim;
 
 
 
 
     // Use this for initialization
     void Start () {
         
 
         rbody = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
         contador = 0;
         Puntuacion.text = "o: " + contador;
 
     }
     
     // Update is called once per frame
     void Update () {
     
         Vector2 movement_vector = new Vector2 (Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
 
         if (movement_vector != Vector2.zero) {
             anim.SetBool ("iswalking", true);
             anim.SetFloat ("input_x", movement_vector.x);
             anim.SetFloat ("input_y", movement_vector.y);
         } else {
             anim.SetBool ("iswalking", false);
         }
 
         rbody.MovePosition (rbody.position + movement_vector * Time.deltaTime);
 
 
 
     }
 
     void OnTriggerEnter2D(Collider2D other) {
         if (other.gameObject.tag == "moneda") {
             DestroyObject (other.gameObject);
             contador = contador + 1;
             Puntuacion.text = "o: " + contador;
         }
     }
 
 
 
     }
 


 public class sword : MonoBehaviour 
 {
 
     [SerializeField]
     private float speed;
 
     private Rigidbody2D myRigidbody;
 
     private Vector2 direction;
 
     // Use this for initialization
     void Start () {
 
         myRigidbody = GetComponent<Rigidbody2D> ();
         direction = Vector2.right;
     
     }
 
     void FixedUpdate()
     {
         myRigidbody.velocity = direction * speed;
 
     }
     
 
 
     void OnBecameInvisible()
     {
         Destroy (gameObject);
     }
 }
 
 

Comment
Add comment · Show 1
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 Mr_Truz · Jun 24, 2016 at 12:13 PM 0
Share

I put both scripts together (second script begins on 56), sorry for that, and for my english.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by LMan · Jun 24, 2016 at 02:00 PM

Check for input on the button you want to control the sword throwing.

Instantiate the sword prefab with the same rotation as the character

Use Rigidbody2D.AddForce to move the sword object and set the force mode to do what you want- don't set the velocity directly. If you want to skip the physics engine, manipulate transform.position instead of the Rigidbody.

have the sword script check for a collision with enemies, destructables, ect using OnTriggerEnter. Unless you are doing physics things after the sword collision, set the sword collider to be a trigger.

Comment
Add comment · 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
0

Answer by Mr_Truz · Jun 24, 2016 at 02:52 PM

Ok, I'm throwing swords adding this to my player script:

 public Transform firePoint;
     public GameObject sword;
 
 void Update () {
 
         if (Input.GetKey (KeyCode.F))
         {
             Instantiate (sword, firePoint.position, firePoint.rotation);
 
         }

I create an empty object attached to my player (firePoint) and added the prefab sword. When I'm facing right I can shoot a sword to the right buy when I'm facing left the sword also goes to the right from the back of my player. Other question, if I hold down the "F" key (throwing key) never stop throwing swords, I want a "press and release" mechanism, in other words, one push one sword. Any ideas? Thank you.

Comment
Add comment · Show 2 · 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 LMan · Jun 24, 2016 at 03:09 PM 0
Share

There's an "add comment" button at the bottom of answers to comment things like this- if you add another answer to ask more questions it just gets confusing.

you can get the direction to move the sword from firePoint.right, assu$$anonymous$$g you want to move the sword in the positive x axis, relative to the firePoint. right now, if you don't change the value of direction, it's going to go the same direction every time.

Have a look at the Input methods to find a method that does what you want. Get$$anonymous$$ey returns true for as long as the button is down. Get$$anonymous$$eyDown returns true only once when the key is pressed.

avatar image Mr_Truz LMan · Jun 24, 2016 at 03:41 PM 0
Share

Works perfect now, thank you so much.

Follow this Question

Answers Answers and Comments

62 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

Related Questions

Unity 2D 5.2.2 How can I aim with keys towards an object? 2 Answers

How can I program a select level Map like the Super mario bro 3 or Shovel Knight? 0 Answers

I'm making a 2D game with no levels like flappy bird. How many scenes will I need? 1 Answer

Is it possible to add an Input Counter during Splash Screens? 0 Answers

C# 2D top down shooter game, How do I get the player to shoot towards the mouse cursor? 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