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 kekeoki · Jun 10, 2015 at 09:07 AM · velocitybullettopdownshootermovment

Top down shooter, bullet movement issues

this is the code ran at instantation of the object.

 public class Shoot : MonoBehaviour {
     public float speed;
     Rigidbody2D rb;
 
     // Use this for initialization
     void Start () {
         rb = gameObject.GetComponent<Rigidbody2D>();
         Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Get mouse position
         Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward); // set rotation to mouse position
         transform.rotation = rot; //rotates game object to correct position
         transform.eulerAngles = new Vector3(0,0,transform.eulerAngles.z); //remove x and y rotation along the x and y axis
         Debug.Log(Input.mousePosition.normalized);
         rb.AddForce(mousePosition.normalized*speed);
 
 
     }
 
 }


when the object is shot, sometimes the shot goes wide of the mouse cursor, as though some outside force is affecting it

 if(Input.GetKeyDown(KeyCode.Space))
         {
                 Instantiate(toFire,shotSpawn.position,shotSpawn.rotation );
         }

This is the instantation code in my playeralt text

and I cant figure it out. If anyone has any top down 2d shooter tutorials that explain a better way to shoot let me know. Thanks, Sammy

heirarchy.png (78.4 kB)
Comment
Add comment · Show 1
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 fzawd · Jun 10, 2015 at 11:34 AM 0
Share

Take a look at this question. It explains how to rotate 2D objects properly. As for your ai$$anonymous$$g, raw mouse position will not suffice. You need to perform raycasting from the camera, based on mouse position.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by saravanan-P · Jun 11, 2015 at 05:42 AM

use this code for rotating the gun at the direction of the mouse pointer,

public int rotation_on_set = 0;

     // Update is called once per frame
     void Update ()
     {
             // subtracting the position of the player from the mouse position
             Vector3 diff = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
             diff.Normalize ();        // normalizing the vector. Meaning that all the sum of the vector will be equal to 1
     
             float rotZ = Mathf.Atan2 (diff.y, diff.x) * Mathf.Rad2Deg;    // find the angle in degrees
             transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotation_on_set);
     }

for the bullet, create it as a prefab and give this script with a collider 2D,

public float bul_speed;

 void Update () 
 {
     transform.Translate(Vector3.right * bul_speed);

     Destroy(this.gameObject, 3.0f);
 
 }


And for shooting the bullet when mouse button is pressed, Create a gameobject with the name "Shooting" to eject the bullet from that place, keep this "Shooting" gameobject as a child gameobject of the gun, so that bullet can be seen as it is coming from the gun..

 public float fireRate = 0;
 
     float timeToFire = 0;
 
 Transform Shooting;
     
 void Awake () 
 {
     Shooting = transform.FindChild ("Shooting");
 }
             
 void Update () 
     
 {
     
     if (fireRate == 0)        // Fire rate is single burst
     {
         
         if (Input.GetButtonDown ("Fire1"))

         {
         
             Shoot();
         }
     
     }
     
     else
     {
         
         if (Input.GetButton ("Fire1") && Time.time > timeToFire)
 
         {
             timeToFire = Time.time + 1/fireRate;

 
             Shoot();
             
                 }
         
     }
     
 }
 
 public GameObject Bulletq;
     
 void Shoot ()
 {
     
     Vector2 ShootingPosition = new Vector2 (Shooting.position.x, Shooting.position.y);
     
     Instantiate(Bulletq, new Vector3(ShootingPosition.x, ShootingPosition.y, 0), transform.rotation);

}

Comment
Add comment · Show 1 · 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 kekeoki · Jun 11, 2015 at 07:41 AM 0
Share

I'll try your code to see of it works better than my fix, thanks!

avatar image
0

Answer by Yuanfeng · Jun 11, 2015 at 08:53 AM

screentoworldpoint function require the camera type set to orthogonal , if not it may be return the vector3.zero. Check If you had set the right type of camera. Secondly. Adforce need call each per frame. it isn't work with simple one call. Maybe you can had a try by change the velocity directly.

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 kekeoki · Jun 11, 2015 at 09:05 AM

My fix void Start () {

         rb = gameObject.GetComponent<Rigidbody2D>();
         Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Get mouse position
         Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward); // set rotation to mouse position
         transform.rotation = rot; //rotates game object to correct position
         transform.eulerAngles = new Vector3(0,0,transform.eulerAngles.z); //remove x and y rotation along the x and y axis
         Debug.Log(Input.mousePosition.normalized);
         rb.velocity =transform.up*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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Tanks Shooting in right direction 0 Answers

2D Shoot at Mouse Position; how to rotate towards the mouse? 1 Answer

2D Directional top down movement,Topdown 2d Directional Movement 0 Answers

Should I use raycasting or colliders? 1 Answer

How do I make a laser pointer? 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