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 /
  • Help Room /
avatar image
0
Question by Nathuram · Feb 11, 2016 at 10:23 AM · 2d gameshootingmouse positiontop down shootercamera.main

2D shooting script fixed camera

Hello everyone I'm working on a 2D top down shooter game with fixed camera in the middle of each rooms but this leads me to an issue to shot towards the cursor mouse postion
I'm currently having this script on my bullets prefab :

     public float amount;
     Vector3 direction;
 
     void Start()
     {
         Vector3 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         transform.rotation = Quaternion.LookRotation(Vector3.forward, direction - transform.position);
         direction = direction.normalized;
         GetComponent<Rigidbody2D>().AddForce(direction * amount);
     }

Then I have a little piece of code to instanciate my bullets on left click in my player script. But when I move my character if I shoot between the camera and the player there's some troubles. (going in a wrong direction)
I guess it is because of the way I'm getting the mouse position but
I have no idea on how to fix that. Hope you could help me :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by TreyH · Feb 12, 2016 at 11:55 PM

@Nathuram You can do this in a few different ways. A raycast would be the most straight forward, but it would require your mouse cursor actually hovering over something with a collider (which is not typically an issue as you can just set them to a physics layer specifically for this kind of thing).

A collider-free implementation of raycasting for your problem:

 // Assumptions:
 //    - We are in 2D mode
 //    - The main Camera is in the negative Z-Axis looking forward
 //
 // We'll return similarly to how a Raycast works
 bool MouseRelativeForward(Transform focus, out Vector3 relativeForward)
 {
     // Create a ray from the camera in the direction of our mouse position
     Ray worldRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 
     // Vertical distance between us (this will be Z in 2D games)
     float heightDelta = focus.position.z - worldRay.origin.z;
 
     // Make sure  we're above the object
     if (heightDelta > 0)
     {
         // Need the proportion of this distance relative to the height component
         float rayMagnitude = heightDelta / worldRay.direction.z;
         // Project this point onto the same horizontal plane as the transform
         Vector3 planarPoint = worldRay.origin + worldRay.direction * rayMagnitude;
         // Look at the object
         relativeForward = (planarPoint - focus.position).normalized;
         // Return it here
         return true;
     }
 
     // If we didn't have a correct height
     relativeForward = Vector3.zero;
     // Let user know we didn't calculate anything
     return false;
 }    
 
 // Debug for confirmation
 void OnDrawGizmos()
 {
     // Check our function
     Vector3 relative;
 
     // This returns whether or not we have  a valid direction
     bool success = MouseRelativeForward(this.transform, out relative);
 
     // If so, print the direction
     if (success)
     {
         Gizmos.DrawLine(this.transform.position, this.transform.position + relative * 100);
     }
 }
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 gorbit99 · Feb 11, 2016 at 05:28 PM

direction += yourCamera.transform.position * new Vector3(1, 0, 1);

Comment
Add comment · Show 3 · 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 Nathuram · Feb 12, 2016 at 08:34 AM 0
Share

I don't understand what this should achive, multiply the values of the X and Z by 1 in the Camera position? By the way the syntaxis of this doesn't work for me the transform.position seems to need {get, set}, moreover the operator * doesn't exist for camera and neither for transform from what i saw in the doc.

avatar image gorbit99 · Feb 12, 2016 at 09:12 AM 0
Share

You add the cam position to the direction after you set it to the mouseposition, and the multiplication is to negate the y value of the vector

avatar image Nathuram · Feb 12, 2016 at 02:47 PM 0
Share

I tried with the * but as I said this operator doesn't exist so i used the Vector3.scale to multiply my direction by a Vector (1,0,1) this doesn't change anything at all to my problem.
I think that it's because of the fact that the camera position is involved to get the mouse cursor position.
I belive that using my BulletSpawn game object to create my direction vector would be the key to solve that but so far I have no clue on how to do so because I didn't succed to get back the mouse position without the Camera.main.ScreenToWorldPoint(Input.mousePosition).

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I make my player's gun point at the cursor, not the player itself? 1 Answer

how do i make my immovable 'empty gameobject spawnpoint' shoot bullets in the direction of my crosshair? 0 Answers

Shoot until i release the button, but with a fire ratio 2 Answers

Shooting animation and bullet generation issue 0 Answers

Fire projectile towards mouse position top-down 2D 0 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