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 Amrtronic · May 16, 2017 at 04:18 PM · 2d2d game2d-platformer2d-gameplay2d rotation

2d shooting problem

im new to unity 1 month ago , im creating a one sided game shooting to the left side , i seen some scripts in tutorials and made my player shoot succesfully , but problem is in some angels of 360 the bullet go above my mouseposition and its very noticable , thanks in advance ;) heres my script for arm rotation:

 // Update is called once per frame
 void Update () 
 {
     Vector2 mouse = Camera.main.ScreenToViewportPoint(Input.mousePosition);        //Mouse position
     Vector3 objpos = Camera.main.WorldToViewportPoint (transform.position);        //Object position on screen
     Vector2 relobjpos = new Vector2(objpos.x - 0.5f,objpos.y - 0.5f);            //Set coordinates relative to object
     Vector2 relmousepos = new Vector2 (mouse.x - 0.5f,mouse.y - 0.5f) - relobjpos;
     float angle = Vector2.Angle (Vector2.up, relmousepos);    //Angle calculation
     if (relmousepos.x > 0)
         angle = 360-angle;
     Quaternion quat = Quaternion.identity;
     quat.eulerAngles = new Vector3(0,0,angle-102); //Changing angle
     transform.rotation = quat;
 }

}

and my weaponPistole script too: void Awake () { firePoint = transform.FindChild ("FirePoint"); if (firePoint == null) { Debug.LogError ("no fire point noob");

     }
 }
 
 // Update is called once per frame
 void Update () {
     

     if (fireRate == 0) {
         if (Input.GetButtonDown ("Fire1")) {
             Shoot ();
         }
     }
         
     
 
 else {

         if (Input.GetButton ("Fire1") && Time.time > timeToFire){

             timeToFire=Time.time + 1/fireRate;
             Shoot();

         }

 }
 }

 void Shoot(){
     Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
     Vector2 firePointPosition = new Vector2 (firePoint.position.x,firePoint.position.y);
     RaycastHit2D hit = Physics2D.Raycast (firePointPosition,mousePosition-firePointPosition,whatToHit);
     if (Time.time >= timeToSpawnEffect) {
         Effect ();
         timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
     
     }


     Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100,Color.green);
     if (hit.collider != null) {
         Debug.DrawLine (firePointPosition, hit.point, Color.red);
         Debug.Log ("We hit" + hit.collider.name + "and did " + Damage + "Damage");

     }
 
 }

 void Effect(){
     Instantiate (BulletTrailPrefab,firePoint.position,firePoint.rotation);
 
 }

}

Comment
Add comment · Show 2
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 Amrtronic · May 18, 2017 at 05:30 PM 0
Share

why nobody replied ? whats wrong...

avatar image JxWolfe Amrtronic · Jan 02, 2019 at 10:22 PM 0
Share

true... has the issue been solved by now, or do you still need help?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by triis4924 · Jan 02, 2019 at 10:26 PM

maybe try this : https://www.youtube.com/watch?v=bY4Hr2x05p8&t because I think the code is easier and there aren't bugs.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I Can't Jump, And If I Remove if(isGrounded) I Can Do More Then 10 Jumps IN The Air 1 Answer

Instantiate a GameObject with a specific Z rotation 2 Answers

so I'm trying to make a 2D platform but the first animation i make is the only one that registers 0 Answers

Destroy a gameObject in the scene if instantiate the same gameObject 0 Answers

Death Counter dont work when changing scenes 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