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 Ebenvranken · May 01, 2020 at 04:06 PM · 2d gameshooting

How to make player shoot projectiles towards mouse position.

for some reason, I know it's easy, I can't manage to do it
every tutorial I can find, makes the player rotate to the mouse position
like a top-down shooter, I do not require this game mechanic
I know, just make something like movetowards(mousePos) or Translate(mousePos)
but I can't seem to get the mousePosition


if I do Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); I get a static Vector 2 with variables X = 0 and Y = 1

Thanks in advance

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 icehex · May 16, 2020 at 03:08 PM 0
Share

@Ebenvranken did my answer help you or it doesn't work?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by icehex · May 01, 2020 at 05:09 PM

Hi, ScreenToWorldPoint takes a Vector3 which is the x and y of the mouse, and z for the depth you wish to observe. When you pass it Input.mousePosition, that gives it a vector that looks like Vector3(x, y, 0). Since the third value is 0, it looks at your mouse's position and interprets its x, y world coordinates at z = 0.


What you want to do is use the x and y component from Input.mousePosition, but set the depth yourself to the z-plane that your user is observing at the time. Here's some code that works, I used passed it the camera's far clipping plane so with this example you'll see the world coordinates at the maximum view distance of the camera, they will definitely be more than 0 and 1 that you observed. Change the farClipPlane to the depth of your objects or scene, and it should work as needed for your application. If you don't want to attach the camera to this, just comment out the camera.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MouseTracker : MonoBehaviour
 {
     public Camera cam;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         Vector2 screen_mouse_pos = Input.mousePosition;
         Vector2 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(screen_mouse_pos.x, screen_mouse_pos.y, cam.farClipPlane));
         //Debug.Log(Input.mousePosition);
         Debug.Log("Mouse: " + mousePos);
     }
 }
 


Since the title of your question asks how to make the player shoot projectiles towards that position, here's an expanded version of the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public float game_z = 8;
     public GameObject bullet;
     public float velocity = 30f;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         Vector2 screen_mouse_pos = Input.mousePosition;
         Vector2 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(screen_mouse_pos.x, screen_mouse_pos.y, game_z));
         
         if (Input.GetButtonDown("Fire1"))
         {
             GameObject fired_bullet = Instantiate(bullet, transform.position, Quaternion.identity);
 
             Vector3 bullet_direction = new Vector3(mousePos.x, mousePos.y, game_z) - transform.position;
             fired_bullet.GetComponent<Rigidbody>().velocity += velocity * bullet_direction;
         }
     }
 }
 

I'm using a bullet gameobject projectile with a Rigidbody attached to it. You can change it to a Rigidbody2D and just modify the code to use a Vector2 bullet_direction, but this will work in 2D and 3D. If you didn't want to use a rigidbody, you'll have to have a script attached to your bullet, access it using GetComponent(), and tell it the direction and velocity to go at. Then in that ScriptName script, add some code under Update() that changes the position of the transform over time at the velocity you want to use.

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

162 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

Related Questions

Why will the bullets sometimes not show up in the game view when the player shoots? 0 Answers

Shooting a bullet object toward the mouse position in a 2D. 1 Answer

How do I stop a 2D projectile when it reaches a point 3 Answers

Target Lookat not working . I want Z axis to rotate only. 2 Answers

how to shoot bullets from circumference of a circle ? 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