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 MicDaRoc · Jun 08, 2015 at 08:42 AM · quaterniondirectionangles

How to shoot in the same direction my character is facing?

Hi,

I'm new to Unity and trying to program a script for my TopDown-Shooter I'm working on. I have some issues concerning the direction the bullet is spawning and flying. I made a Prefab of my Bullet, named it "Bolt" and added the following Script:

 using UnityEngine;
 using System.Collections;
 
 public class Mover : MonoBehaviour {
 
     public float speed;
 
 
 
     void Start ()
 
     {
          GetComponent<Rigidbody2D> ().velocity = transform.forward * speed;
     }
 }
 
   

And here's the script, which handles the character movement and shooting part:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public float speed1; 
     public GameObject shot;
     public Transform shotSpawn;
     public float fireRate;
 
     private float nextFire;
 
     
     void Update () {
 
         if (Input.GetButton ("Fire1") && Time.time > nextFire) {
             nextFire = Time.time + fireRate;
             Instantiate (shot, shotSpawn.transform.position, shotSpawn.transform.rotation);
             
         }
 
         if (Input.GetKey (KeyCode.D)) {
             transform.Translate (Vector2.right * speed1);
         }
         if (Input.GetKey (KeyCode.A)) {
             transform.Translate (-Vector2.right * speed1);
         }
         if (Input.GetKey (KeyCode.W)) {
             transform.Translate (Vector2.up * speed1);
         }
         if (Input.GetKey (KeyCode.S)) {
             transform.Translate (-Vector2.up * speed1);
 
                                 
         }    
     
     }
 }


There are 2 problems with this script:

Pressing the Firebutton results in "laying" shots, they're not flying, they only spawn and remain at the same position.

The other problem is, that the shots don't spawn in the direction my character is facing.

What am I doing wrong?

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
2

Answer by applemaniac · Jun 08, 2015 at 08:48 AM

You can use Vector3.forward to know what direction your character is facing, and use it in something like yourBullet.GetComponent().AddForce(transform.forward * power)

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 MicDaRoc · Jun 08, 2015 at 10:24 AM 0
Share

There is a script attached to the Prefab of my bullet, which contains a similar code to what you suggested:

 void Start (){
     GetComponent<Rigidbody2D>().velocity = transform.forward * speed1;

In the Player$$anonymous$$ovement-Script I added something like shotSpawn.transform.rotation = Quaternion.Euler(0,0,-180);

every time a certain $$anonymous$$ey is pressed. The shotSpawn seems to rotate, I can see the Z-valuable change in the Inspector of the shotSpawn everytime I press a certain key, but the shot keeps flying down.

Any suggestions?

And Thanks in advance!

avatar image applemaniac · Jun 09, 2015 at 06:09 AM 0
Share

Check the usual problems : colliders, frozen degrees, and try with AddForce()

avatar image
1

Answer by MicDaRoc · Jun 11, 2015 at 08:53 AM

Ok, i solved it. I set the Gravityscale to 0 and added this line of code to the moverscript:

GetComponent().AddForce(-transform.up * speed1);

Comment
Add comment · Show 1 · 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 Oozy · Mar 10, 2017 at 03:17 PM 0
Share

Can you post your full code, i just cant seem to get $$anonymous$$e to work

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

[SOLVED] Receive a float from a quaternion? 1 Answer

How do I rotate an object to face a direction. 1 Answer

Instantiated objects always facing the center 2 Answers

Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 1 Answer

How to apply parent based rotation? 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