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
1
Question by zahidashahid535 · Oct 07, 2021 at 06:19 AM · rangeattacking

Why arrow instantiating in wrong direction in range attack?

hi there, hope you are in good health.

I am implementing a ranged attack it is working fine when the character is moving towards the right, but the arrow is instantiating in the same direction (arrow goes back from the bow) when the character moves towards the left. Which I don't want.

I need help anyone please help me out with this. here snapsalt texthots of the game view and code.

public class Bow: MonoBehaviour {

 public float offset;
 public Transform shotPoint;
 public GameObject projectile;

 public float timeBtwShots;
 public float startTimeBtwShots;
 void Update()
 {

     Vector3 bowPosition = transform.position;
     Vector3 difference = Camera.main.WorldToScreenPoint(Input.mousePosition) - transform.position;
     float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
     Debug.Log("" + rotZ + offset);
     if (Input.GetMouseButtonDown(0))
     {
         Instantiate(projectile, shotPoint.position, transform.rotation);
     }

 }

}

screenshot-293.png (273.4 kB)
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
0
Best Answer

Answer by SamyBoyJim · Oct 07, 2021 at 06:38 AM

Hi there!

A. I think you should put all of vector calculating logic in the if statement so you aren't having to calculate that every frame and only when you click the mouse button. The bowPosition variable also seems useless, I haven't seen the whole script but don't reference it if you don't have to. These fixes are not super important but it is always good to improve performance when you can!

B. You haven't supplied much information that I can make sense of in your post but from what I can make sense of, you want to fire the arrow towards the mouse pointer. This code is correct and is doing what it is meant to do except for the offset value; I have worked with atan2 in situations like this before and i believe the perfect offset is -90 so I think if add that then it will work but can you clarify what the problem is because it wasn't clear in your post what the problem was?

Hope this helps!

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 zahidashahid535 · Oct 07, 2021 at 07:33 AM

Thanks for your answer,

The problem was with the prefab it was not flipping when the player moves in the left direction. now it is working fine.

but a new issue was raised while fixed this.

an arrow (instantiated) shot in the right direction, but at that moment when the player changes its direction previously shot or instantiated arrows came back towards the point of the shoot. it's due to the script attack to prefab. value changes also on previously instantiated arrows.

Would help me with this. If you are getting my point.

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

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

How to auto attack like in lol or other moba games? 1 Answer

Ai that applies damage when in range? 1 Answer

Missing a Method? Random.Range? 1 Answer

GameObject add/remove children without causing errors. 1 Answer

Limit cameras near range ? 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