• 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
Question by orangesmasher221 · Aug 13, 2015 at 09:05 AM · animationrotationtransformscripting beginner

Bullet Script change direction with player (2D)?

Okay, I am having a little bit of an issue with a so far perfect scripting experience with unity. (W$$anonymous$$ch I am very thankful for as I am learning more and more about programming in both C# and Javascript as I go). Needless to say, I am not very literate when it comes to coding. That isn't to say I don't understand syntax and the basics of these two languages, I'm just that good. LOL But that'll improve as I do more coding later on with the development of t$$anonymous$$s game. Okay, anyways, I am trying to make it so that when my character sprite changes direction, the bullet also changes direction. T$$anonymous$$s is a 2D platformer where the character only moves from left to right. I have tried implementing some of the solutions found on these forums into my code, but alas, it didn't fix the issue. To be specific with what I have trouble with understanding is the transform.position, and the transform.rotation of the Instantiate function. Also, the script that Instantiates the script is attached to an empty gameobject under the player if that makes a difference. Thanks in advance! Here is the code:

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
     if(Input.GetKeyDown(KeyCode.LeftS$$anonymous$$ft))
     {
         Shoot ();
        }
 }
 
 var bulletPrefab : GameObject;
     function Shoot()
     {
         yield WaitForSeconds(0.2f);
         Instantiate(bulletPrefab, transform.position, transform.rotation);
     }
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Sparkels · Aug 13, 2015 at 09:52 AM

Try t$$anonymous$$s:

function Start () {

}

function Update () { if(Input.GetKeyDown(KeyCode.P)) {

     Shoot();
     }
 

}

function Shoot() { yield WaitForSeconds(0.2f);

      t$$anonymous$$s.gameObject.transform.position = GameObject.FindWithTag ("Fire").gameObject.transform.position;
      Debug.Log("pow pow pow ");
  }
Comment
9uile

People who like this

1 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 orangesmasher221 · Aug 13, 2015 at 03:20 PM 0
Share

Is this supposed to be the actual script or something to add to the script? I replaced it, and it didn't spawn the bullet prefab, and I added it, and it still didn't spawn the prefab even when I instantiated it. So, I really couldn't test if this code worked if my character was facing another direction. :/

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Top-down character running animation based on facing direction 0 Answers

Door opening in the wrong direction..... 0 Answers

Wrong rotation while swimming 2 Answers

Animating in Unity Rotation snaps to last rotation key 0 Answers

Adding child repositions parent tranform 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