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 ToastandBananas · Jun 13, 2018 at 04:20 AM · 2dunity 2dshootingbulletssidescroller

How to Make Character Shoot Where Aiming with Mouse

I've looked around and none of the stuff I've found seem to help me. Here's my code so far. Right now it just shoots to the right. Now how do I adjust my code to make the bullet go where I'm pointing with the mouse?

 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerShooting : MonoBehaviour {
 
     public GameObject projectilePrefab;
     private List<GameObject> Projectiles = new List<GameObject>();
     private float projectileVelocity;
 
     // Use this for initialization
     void Start () {
         projectileVelocity = 10;
     }
     
     // Update is called once per frame
     void Update () {
         Shoot();
     }
 
     void Shoot()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             GameObject bullet = (GameObject)Instantiate(projectilePrefab, transform.position, Quaternion.identity);
             Projectiles.Add(bullet);
         }
 
         for (int i = 0; i < Projectiles.Count; i++)
         {
             GameObject goBullet = Projectiles[i];
             if (goBullet != null)
             {
                 goBullet.transform.Translate(new Vector3(1, 0) * Time.deltaTime * projectileVelocity);
 
                 Vector3 bulletScreenPos = Camera.main.WorldToScreenPoint(goBullet.transform.position);
                 if (bulletScreenPos.y >= Screen.height || bulletScreenPos.y <= 0)
                 {
                     Destroy(goBullet);
                     Projectiles.Remove(goBullet);
                 }
             }
         }
     }
 }
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 crawniik · Jun 13, 2018 at 05:23 AM 0
Share

Your code seems a little more complicated than $$anonymous$$e so I really can't help you fully but the game I'm making is a top down shooter built in 3D the following is my $$anonymous$$ouseLook.cs script and the 2 lines that instantiate and shoot my prefab bullets. maybe by looking at those you can get an understanding of what's happening in $$anonymous$$e and help you out with yours. I hope so at least.

$$anonymous$$ouseLook.cs

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
  
  
  private Vector3 mouse_pos;
  public Transform target; //Assign to the object you want to rotate
  private Vector3 object_pos;
  private float angle;
  
  void FixedUpdate (){
      mouse_pos = Input.mousePosition;
      mouse_pos.z = 0; //The distance between the camera and object
      object_pos = Camera.main.WorldToScreenPoint(target.position);
      mouse_pos.x = mouse_pos.x - object_pos.x;
      mouse_pos.y = mouse_pos.y - object_pos.y;
      angle = $$anonymous$$athf.Atan2(mouse_pos.y, mouse_pos.x) * $$anonymous$$athf.Rad2Deg + 270;
      transform.rotation = Quaternion.Euler(new Vector3(0, -angle, 0));
  }
 }


Instantiate

         var clone = Instantiate(bullet, bulletSpawn.position, Quaternion.Euler(randomRotation, randomRotation, randomRotation)); //instantiate bullet and call it clone
         clone.velocity = transform.TransformDirection(new Vector3(0, 0, Speed));  //Send it on it's way


I hope this helps.

avatar image crawniik crawniik · Jun 13, 2018 at 05:26 AM 0
Share

in the $$anonymous$$ouseLook.cs script, I had to add the + 270 to the angle variable to get it to look at my mouse. That may be a similar fix in your script, I don't know.

If you can't tell, I'm a complete nub at this stuff. I'm trying to get good, but that's gonna take a while. Only been at this about a month now.

0 Replies

· Add your reply
  • Sort: 

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

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

Sidescroller Bullets Follow Mouse After Being Shot 2 Answers

Optimizing Code 1 Answer

Need help with screen resolution and character size 1 Answer

2D Shooting Prefab 1 Answer

Shooting around defense position without contact 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