• 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 rct3fan24 · Aug 27, 2014 at 01:56 AM · bulletlookat

Troubles with 2D projectiles

So, this is my first time working with 2D, and it's proving to be difficult for me. So here's the deal:

I have a script attached to the player that basically tells it to instantiate a bullet object at its position. Then that bullet has this script attached:

 var speed:float = 2.0;
 
 function Start()
 {
     var dir = Input.mousePosition;
     transform.LookAt(dir);
 }
 
 function Update()
 {
     transform.position += transform.forward * speed * Time.deltaTime;
 }

This script doesn't work how I want it to. At all. The bullet always moves to quadrant 1, if this were a graph, with the player being 0,0. That happens 100% of the time. To make matters worse, the bullet rotates so that the face of the sprite, with the texture on it, is facing the direction it's moving, so you can't see it at all from the bird's-eye perspective the game has. Does anyone have an explanation for this, and maybe a way to fix it so that it fires properly?

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 siddharth3322 · Aug 27, 2014 at 02:00 AM 0
Share

You can get projectile creation code from here: How to display projectile trajectory path in Unity 3D?

You have to change code to meet your requirement.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Aug 27, 2014 at 03:53 AM

I'm assuming an Orthographic camera facing towards positive 'z', and the projectile is a Sprite where the front of the sprite is facing right when the Sprite's rotation is (0,0,0).

Change Start() as follows:

 function Start()
 {
     var pos = Camera.main.WorldToScreenPoint(transform.position);
     var dir = Input.mousePosition - pos;
     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 }

Note the 'WorldToScreenPoint()' call. The mouse lives in Screen coordinates, and your Sprite lives in world coordinates. You need to convert between the two when writing code like this.

Comment
Add comment · Show 2 · 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 rct3fan24 · Aug 27, 2014 at 04:24 AM 0
Share

Now... the rotation is correct, so thanks for that! The problem now, is that "transform.forward" makes it go along the Z axis, resulting in the bullet going underneath the player and staying in the same spot. How can I go about making it go... forward?

Edit: Never$$anonymous$$d! I've got it figured out. The answer was obvious, I'm just dumb. I have to use "transform.up" ins$$anonymous$$d. Also, I had to add "- 90" to the end of the angle variable, since the sprite texture is facing north.

avatar image robertbu · Aug 27, 2014 at 04:31 AM 0
Share

Since the front of the projectile is the right side, use transform.right ins$$anonymous$$d of transform.forward:

  transform.position += transform.right * speed * Time.deltaTime;

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Fire Bullet To Mouse Position Problem 2 Answers

Making an object follow another object (bullets following a player) 1 Answer

shot delay in between bullets 1 Answer

How to make Cannon ball fly in arc 2 Answers

Destroy Turret with machine Gun 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