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 jeffmadriaga · Feb 15, 2013 at 05:51 PM · rigidbodybulleteulerangles2d rotationshmup

how to rotate to face camera point in 2D

Hi all,

Just to set the scene... i'm working on a top down shooter as is the norm when starting unity :D Ive been searching many many topics going over the same thing

I've a player object, controlled with cursor keys with rigid body and collider components. this instantiates a bullet object created at the players center and then calls a bullet.getComponent.INIT() method and passes two Vector3 params namely a force and a rotation.

The force i add with AddForce is got from the normalised vector of two points drawn from the current player and the mouse pos in 3d space, then finally knocking out one of the axis. (as its 2D)


Vector3 finalforce = (transform-mousePos).normalized;

finalforce.y = 0


What i want to find is rotation.

Currently in the bullet object I'm doing:


mousePos.y = 0;

bullet.LookAt(mousePos);


but i want to optimise to not use rigid body and move the projectiles with translate and rotation. however im stuck trying to understand 3d principals of rotation. Euler vs rotation.

I come from a flash background and to create directional vectors vx = Math.sin(angle)*speed vy = Math.cos(angle)*speed and the angle is atan(diffy/diffx) angle is always an absolute one

if i try to get the angle and set it then translate position i have the projectile flying off in all different kinds of axis. I simply want this 2d and calculate the absolute angle and set the absolute rotation once on Start - the movement vector should take care of direction and speed during Update..

Any help appreciated! Thanks

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
0

Answer by robertbu · Feb 15, 2013 at 06:38 PM

If you have something that you can represent in 2D, you can use Mathf.Atan2() to convert it into an absolute rotation, and you can assign that rotation to one of the axis of your object. Your equation for directional vectors is just a polar to rectangular conversion, and works in Unity for 2D conversions. But I can't help thinking you are making this too hard.

First, is there any reason why you don't want to use LookAt()? It has nothing to do with the Rigidbody component. Second, if you just want to shoot from the forward of your character, it is common to create an empty game object at the spawn point of the bullet (typically just in front of the character or the gun with the same rotation) and make that object a child of the character or gun. Then transform.forward vector will always point the direction you want to make the bullet go. No calculation needed.

Question: Are you converting your mouse positions to world positions when you are doing your calculations?

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 jeffmadriaga · Feb 15, 2013 at 09:51 PM

Thank you for your feedback, much appreciated! It's all quite new to me thinking about things in 3d space, after years of 2d programming.

I'll stick to LookAt if it doesn't have anything to do with rigid body. Am i right in thinking the vector3 works because i knock out one axis (Y Set to 0, as i deal with XZ) and have a freeze rotation constraint on the Y axis of the bullet Gameobject.

Secondly the character never rotates, the game is top down imagine zelda, but has 360 degree firing (towards the mouse). I like that idea of parent-child instantiation to allow for only a toward vector, very efficient! would the bullet translation not be grouped to players movement? so when player moves left it adds the translation to the bullet making it move left?

Could you run/comment a quick example please?

Comment
Add comment · Show 1 · 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 robertbu · Feb 16, 2013 at 05:19 PM 0
Share

The spawn point is used as the position for the bullet, but the bullet is not made a child of the spawn point, so the bullet will not swing with the camera. I posted a sample gun script here:

http://answers.unity3d.com/questions/396394/shooting-bullets-from-a-ship.html

It's not a match to your situation, but you can play with it to get an idea.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

vector3.forward doesn't work when used in Rigidbody? 2 Answers

Rigidbody (2D) stays in fixed angle even when colliding 1 Answer

2D bullet changes direction based on player's world position? 2 Answers

Rigidbody of empty bullet shell collision problem. 2 Answers

Problem with fast bullets (tunnelling effect) 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