Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NoobCoderThatNeedsHelp · Mar 02 at 06:06 PM · bullets

How do I keep bullet speed fixed while keeping its travelling rotation? (2D Topdown)

I'm trying to shoot a bullet towards where I'm looking trough using my mouse cursors position, though the farther out I hold my mouse the faster my bullets travel. How could I make it so all my bullets travel at the same speed? The code im using is:

public class BulletMove : MonoBehaviour { [SerializeField] private GameObject bullet; [SerializeField] private GameObject ship; [SerializeField] private GameObject camera; public Vector3 mousepos = Input.mousePosition; public Vector2 shippos = new Vector2(); public Vector3 relativeship = new Vector3();

 void Start()
 {
     shippos = ship.transform.position;
     mousepos = Input.mousePosition;
     mousepos.x = mousepos.x - Screen.width / 2;
     mousepos.y = mousepos.y - Screen.height / 2;
     relativeship = camera.transform.InverseTransformDirection(mousepos - camera.transform.position);
  
 }



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

     Vector2 newPos2 = bullet.transform.position;
     newPos2.x = newPos2.x + (relativeship.x / 10f) * Time.deltaTime;
     newPos2.y = newPos2.y + (relativeship.y / 10f) * Time.deltaTime;
     bullet.transform.position = newPos2;


 }

}

and my code to spawn(if it helps) the bullet is:

public class Shoot : MonoBehaviour { [SerializeField] private GameObject bullet; [SerializeField] private GameObject ship; [SerializeField] float cooldown = 1;

 void Update()
 {
     cooldown -= (3f * Time.deltaTime);
     if (Input.GetKey(KeyCode.Mouse0) && cooldown < 0f)
     {
         Vector2 newPosition = ship.transform.position;
         GameObject shot = Instantiate(bullet, newPosition, ship.transform.rotation);
         cooldown = 1f;
       
         }

     }

 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Whitewalkergp · Mar 05 at 07:43 AM

maybe it is because you are using

  relativeship = camera.transform.InverseTransformDirection(mousepos - camera.transform.position);

and the relativeship is depended on mousepos(Mouse Position) and it also effects your bullet speed.

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

135 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooting with Limited Ammo 2 Answers

Need hep with pooling(call Cs in Js) 0 Answers

Getting Bullets to fire in Direction of Gun Barrel 1 Answer

Bullet facing 0 Answers

How to throw a RigidBody in the direction of Mouse Pointer (3D)? 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