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 FeronsFire · Jan 08, 2014 at 07:44 AM · 2dmouse orbit

Top down rotate towards mouse

I am currently working on a 2d game in a top down perspective and I can't figure out how to get bullets to shoot towards the mouse. I've done the obvious of translating screen position to world position but any method I use to rotate the bullets transform.rotate isn't doing anything. I've been looking up information on Quaternions, EulerAngles and Vector3 rotation but I've come up with nothing so far.

Think twin stick shooter but with wasd (or arrow keys) as movement and the mouse is the direction the player shoots. Any help would be much appreciated, I've been at this for a long time now.

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 GameVortex · Jan 08, 2014 at 08:30 AM 0
Share

If you could provide some of your code to show what you have done, what specifically is not working and what you expected the code to do, we could probably help you a little better.

avatar image FeronsFire · Jan 08, 2014 at 05:49 PM 0
Share

Sorry I left out the most important bit.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerShoot : $$anonymous$$onoBehaviour {
 
     public Rigidbody2D Bullet;
     public float speed = 20f;
 
     void Update() {
 
         if(Input.GetButtonDown("Fire1")) {
             Vector3 myPos = transform.position;
             float x = Input.mousePosition.x;
             float y = Input.mousePosition.y;
             Vector3 direction = Camera.main.ScreenToWorldPoint
                             (new Vector3(x, y, transform.position.z)) - myPos;
             Rigidbody2D bulletClone = (Rigidbody2D)Instantiate
                             (Bullet, myPos, Quaternion.LookRotation(direction));
 
         }
     }
 }

As for angry robots I don't seem to have it on my machine as a demo project from the latest install of Unity, is there an alternate method to download it? From what I played it's in a 3D space, yet any methods I've tried to apply to a 2D space haven't worked.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by anniyan137 · Jan 08, 2014 at 11:40 AM

Try looking at the sample project Angry bots, that is given with Unity. I think you will find what you're looking for in there.

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 robertbu · Jan 08, 2014 at 05:56 PM

'Top Down' to me means the camera looking down the 'Y' axis onto the XZ plane. This appears to be a standard view with the camera looking towards positive 'Z' onto the XY plane. One problem that may be what is causing your issues is on line 5. The 'Z' parameter is the distance in front of the camera, not the position of your object. Assuming your 2D plane has a 'z' of 0.0, and the setup is as I assume, you can just negate the value:

  Vector3 direction = Camera.main.ScreenToWorldPoint
                             (new Vector3(x, y, -Camera.main.transform.position.z)) - myPos;

If the 2D plane has a 'Z' position other than 0.0, you can do:

Calculate the position of the 'Z' by:

 float dist = transform.position - Camera.main.transform.position.z;

Note if the distance from the camera to the 2D plane remains constant, you can do either calculation in Start().

Also when you do the look rotation, you likely want to specify the optional second (up) parameter. Given a 2D game with the camera looking forward, you want to use Vector3.forward for this prameter.

 Quaternion.LookRotation(direction, Vector3.forward);
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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to convert to 2D 1 Answer

2D PlatformerController: Cumulative drift? 1 Answer

2d Game Jump Issue 2 Answers

Problem with playmaker gui and "Get mouse button down" action. 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