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
0
Question by $$anonymous$$ · Jul 20, 2013 at 09:08 AM · c#rotationmousebullet

2d game facing bullets to mouse

Sorry for reposting question, but i have tried many of solutions and they dont work for me. So i have a 2d plane with a texture(ship) which is shooting bullets. I need bullets to shoot in the direction of mouse. This works, but bullets have strange rotation! Thanks

 public class Bullet : MonoBehaviour 
 
 {
 
 public float speed = 25f;
     
     void Start()
     {
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = (transform.position.z - Camera.mainCamera.transform.position.z);
  
         Vector3 worldPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);
  
         transform.LookAt(worldPos);
     
     }
     
     void Update() 
     {    
           transform.Translate(Vector3.forward * Time.deltaTime * speed);            
     }
 }

alt text

alt text

bullets.png (250.3 kB)
wrong rotation.png (198.8 kB)
Comment
Add comment · Show 7
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 $$anonymous$$ · Jul 20, 2013 at 02:43 PM 0
Share

Any suggestions?

avatar image Deathcalibur · Jul 20, 2013 at 03:52 PM 0
Share

What do you mean by bullets have strange rotation? As in they move correctly toward the mouse, but the image of the bullet is rotated weird?

avatar image $$anonymous$$ · Jul 20, 2013 at 04:37 PM 0
Share

Yes you are right.

avatar image robertbu · Jul 20, 2013 at 04:38 PM 0
Share

There are a couple of different things that might be going wrong here depending on how you constructed your projectile. A screen shot of the wrong rotation would be very helpful as well as a description of how you constructed your bullet. If it is an issue of wrong rotation, then you need to change your bullet so that forward faces positive 'Z'. If it is an issue of flipping, take a look at the optional second parameter to the LookAt() function.

avatar image robertbu · Jul 20, 2013 at 05:54 PM 1
Share

I would try adding Vector3.forward or Vector3.back as the second parameter to LookAt().

Show more comments

2 Replies

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

Answer by $$anonymous$$ · Jul 20, 2013 at 06:11 PM

Thanks robertbu! This is solution :

{

public float speed = 25f;

 void Start()
 {
    Vector3 mousePos = Input.mousePosition;
    mousePos.z = (transform.position.z - Camera.mainCamera.transform.position.z);
 
    Vector3 worldPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);
 
    transform.LookAt(worldPos,Vector3.forward);
 
 }
 
 void Update() 
 {  
       transform.Translate(Vector3.forward * Time.deltaTime * 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
avatar image
0

Answer by dshung1997 · Jan 26, 2017 at 05:15 PM

I've met my solution and I immediately wrote this to share with you (I'm a newbie) My main player is a plane and the bullet is kind of thin and long rectangle. I have 2 C# scripts below and they definitely worked (after I changed Update to FixedUpdate in the BulletMotion script)

    // This is BulletSpawner script

     [SerializeField]
     private GameObject bullet;
 
     [SerializeField]
     private float fireRate;
 
     [SerializeField]
     private GameObject player;
 
     private float lastFire;
 
     // Use this for initialization
     void Awake () {
         lastFire = 0;
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetButton("Fire1") && Time.time > lastFire)
         {
             lastFire = Time.time + fireRate;
 
             Vector2 target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y));
             Vector2 curPos = new Vector2(player.transform.position.x, player.transform.position.y);
             Vector2 direction = target - curPos;
 
             direction.Normalize();
            
             GameObject projectile = (GameObject)Instantiate(bullet, curPos, player.transform.rotation);
         }
     }

.. And below is the BulletMotion script

  [SerializeField]
      public float speed;
 
     private Rigidbody2D bulletBody;
   
     // Use this for initialization
 
    void Awake () {
         bulletBody = GetComponent<Rigidbody2D>();   
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
       
         Vector3 temp = transform.position;
 
         Vector3 velocity = new Vector3(0, speed * Time.deltaTime, 0);
         
         temp += ( transform.rotation * velocity);
 
         transform.position = temp;
      
         if (transform.position.y < -6f || transform.position.y > 6f || transform.position.x < -9f || transform.position.x > 9f)
         {
             Destroy(gameObject);
         }
     }
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

16 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Rotating object with a mouse movement 0 Answers

How to rotate Character on Y axis along with the mouse rotation? 0 Answers

Third person camera; player rotation. 1 Answer

Using quaternion for mouse movement? 3 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