Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Thunderball91 · Dec 03, 2013 at 01:11 AM · c#graphics

2D Firing an object in the direction the Player is facing!

Hi I am wondering how to get an object to fire in the direction the player is facing. I am currently firing an arrow from a bow object which is attached to the player object in order to maintain movement and to rotate the placeholder sprite with the player.

I have attached my bow script below. I know that my code in the Bow is what is causing the arrow to fire in a straight line but I am unsure on how to make it fire in the same direction the player is facing.

 // BOW SCRIPT
 
 public class Bow : MonoBehaviour {
 
     public Rigidbody2D arrow;                // Prefab of the arrow.
     public float projSpeed = 20f;            // The speed of the projectile
     public Vector3 Rotation;                // Vector 3 to hold rotation
     private PlayerMovement playerMove;        // Reference to the PlayerControl script.
     //private PlayerRotation playerRot;        // Reference to the Player Rotation Script
     
     void Awake()
     {
         // Setting up the references required
         // playerMove = transform.root.GetComponent<PlayerMovement>();
     }
 
     // Update is called once per frame
     void Update () 
     {
         // Check to see if Fire 1 is pressed
         if(Input.GetButtonDown("Fire1"))
         {
             // Instantiate an arrow!
             Rigidbody2D arrowInstance = Instantiate(arrow, transform.position, Quaternion.Euler(new Vector3(0, 0, 0))) as Rigidbody2D;
             arrowInstance.velocity = new Vector2(projSpeed, 0);
         }
     }
 
 }


Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Dec 03, 2013 at 01:14 AM

Assuming the object the script avove is attached to is facing the correct direction to fire the arrow, you can change line 25 to:

 arrowInstance.velocity = transform.forward * projSpeed;

Comment
Add comment · Show 3 · 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 Thunderball91 · Dec 03, 2013 at 04:31 PM 0
Share

Hi,

This has helped shoot it in the direction that the player is facing! Thank you so much!

I ended up copying the players rotation to the bow object (which is attached to the player object) via code. Is there a way to do it so the bow will inherit the direction from the player without the use of code? (sorry for all the questions, I am a unity noob).

avatar image robertbu · Dec 03, 2013 at 10:08 PM 0
Share

Not sure without more info. Typically the spawn point would be a child of the player. Assu$$anonymous$$g the child has a rotation of (0,0,0), then the child's rotation and the parent's rotation will match. But getting the rotation from another game object is perfectly okay.

avatar image taylordustin · Sep 15, 2014 at 12:48 AM 0
Share

Using arrowInstance.velocity = transform.up * projSpeed; got this code working properly for me. $$anonymous$$y 2D game has bullets firing out onto the Z plane.

avatar image
0

Answer by simke.nys · Jan 08, 2014 at 06:04 PM

 using UnityEngine;
 using System.Collections;
 
 public class DanteControllerScript : MonoBehaviour 
 {
     public float maxSpeed = 10f;
     bool facingRight = true;
                 
     public Rigidbody2D bullet;
     
     Animator anim;
 
     // Use this for initialization
     void Start () 
     {
         anim = GetComponent<Animator> ();
     }
 
     // Fixedupdate: do all physic controls here
     void FixedUpdate () 
     {
         float hMove = Input.GetAxis ("Horizontal");
         float vMove = Input.GetAxis ("Vertical");
 
         anim.SetFloat ("hSpeed", Mathf.Abs (hMove));
         anim.SetFloat ("vSpeed", vMove);
 
         rigidbody2D.velocity = new Vector2 (hMove * maxSpeed, rigidbody2D.velocity.y);
         rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, vMove * maxSpeed);
 
 
         if (hMove > 0 && !facingRight)
             Flip ();
         else if (hMove < 0 && facingRight)
             Flip ();
 
         if (Input.GetButtonDown ("Fire1")) {
             Rigidbody2D bulletInstance = Instantiate (bullet, transform.position, Quaternion.Euler(new Vector3(0, 0, 0))) as Rigidbody2D;
             bulletInstance.velocity = transform.forward * maxSpeed;
         }
     }
 
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

I am having the same issue.

Anyone knows what the problem might be? My bullet shoots down all the time instead of the players facing direction.

Thanks

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

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

17 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

Related Questions

What is the best way to return a deformed mesh to its initial shape? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Is there anyway to have an advanced graphics option in my game? 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