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 ReubenClare123 · Jul 31, 2019 at 05:14 PM · shootingdirectionfacing

How do i shoot in the direction im facing?

Hi guys I've tried everything that I could think of but sadly im a huge noob. Can anyone help figure out why I cant shoot to the left. This is my shoot script attached to the shoot point.

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.K))
     {
         Rigidbody2D instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody2D;
         instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(20, 0, speed));

         projectile.velocity = new Vector2(velX, velY);
     }
 }

}

Here is my Controller script.

 {
     rb = GetComponent<Rigidbody2D>();
     spriteRenderer = GetComponent<SpriteRenderer>();
     animator = GetComponent<Animator>();
 }

 void Update()

 {
     Vector2 move = Vector2.zero;

     float horizontal = Input.GetAxis("Horizontal"); // a or left = -1 d or right = 1

     rb.velocity = new Vector2(horizontal * Speed, rb.velocity.y);

     animator.SetFloat("Speed", Mathf.Abs(horizontal));

     if (Input.GetButtonDown("Jump") && grounded)
     {
         velocity.y = jumpTakeOffSpeed;
     }
     else if (Input.GetButtonUp("Jump"))
     {
         if (velocity.y > 0)
         {
             velocity.y = velocity.y * 0.5f;
         }
     }

     animator.SetBool("grounded", grounded);
     animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);

     targetVelocity = move * maxSpeed;
 }

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Vega4Life · Jul 31, 2019 at 05:28 PM

So when you do a flip on a spriteRenderer, it doesn't change the objects orientation. Thus when yo do something like, (transform.TransformDirection(new Vector3(20, 0, speed)); ) this will still shoot towards the right.


You could manually keep track of that flip in an int flipState, where 1 is to the right, -1 is to the left. Change the int when you flip the renderer. Then just multiply this int value to the transform.TransformDirection(new Vector3(20, 0, speed)) * flipState; Where a positive transform is right, negative is left.


Hopefully that makes sense.

Comment
Add comment · Show 10 · 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 ReubenClare123 · Jul 31, 2019 at 05:31 PM 1
Share

Thanks Helps a lot =)

avatar image ReubenClare123 · Jul 31, 2019 at 05:37 PM 0
Share

How do I write that when its -1 it = to the left.

avatar image Vega4Life ReubenClare123 · Jul 31, 2019 at 05:42 PM 0
Share

This all your doing really. You declare an int, then just change the value of it when you flip the renderer:


     // This is declared at the top of your script
    int flipState = 1;  // We start looking to the right (by default)
 
   // Then change it if you flip your renderer
     mySpriteRenderer.flipX = false;
     flipstate = 1;
 
     mySpriteRenderer.flipX = true;
     flipstate = -1
 
    // Then later, multiply the flipstate to your transform.transformdirection
 
avatar image ReubenClare123 Vega4Life · Jul 31, 2019 at 05:43 PM 1
Share

Ok thanks pal =)

Show more comments
avatar image
0

Answer by ReubenClare123 · Jul 31, 2019 at 05:16 PM

This is my flip script as well:

// variable to hold a reference to our SpriteRenderer component private SpriteRenderer mySpriteRenderer;

 // This function is called just one time by Unity the moment the component loads
 private void Awake()
 {
     // get a reference to the SpriteRenderer component on this gameObject
     mySpriteRenderer = GetComponent<SpriteRenderer>();
 }

 // This function is called by Unity every frame the component is enabled
 private void Update()
 {
     if (Input.GetKeyDown(KeyCode.D))
     {
         // flip the sprite
         mySpriteRenderer.flipX = false;
     }

     // if the A key was pressed this frame
     if (Input.GetKeyDown(KeyCode.A))
     {
         // if the variable isn't empty (we have a reference to our SpriteRenderer
         if (mySpriteRenderer != null)
         {
             // flip the sprite
             mySpriteRenderer.flipX = true;
         }
     }
 }

}

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 MounirDev · Jul 31, 2019 at 07:51 PM

@ReubenClare123 Quaternion.identity :) if you want to shoot a bullet in front of you character or gun you do simply : Instantiate(bulletPrefab, transform.position, Quaternion.identity); this will set the bullet rotation like you character's. then just give your bullet game Object a scrip to move forward.

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

114 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 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

Gun not firing in certain direction 1 Answer

Shooting script C# 1 Answer

Making laser beam point in shooting direction C# 1 Answer

Character rotation without affecting control orientation 1 Answer

Player spawns facing the wrong direction. 2 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