Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 MrFalseEye · Jul 30, 2015 at 08:37 AM · 2dphysicsrotate

Rotate and move a 2D object

I'm making a 2D game in Unity. Basically there's just a rocket that goes where ever the user touches.

 Vector3 dir = targetPos - transform.position;
 float rotZ = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
 rb.MoveRotation(-rotZ);
 thrust = 10.0f;
 rigidbody2d.AddForceAtPosition(transform.up * Thrust, transform.up);

This code is placed in the Update function. With this code that i have right now, the rocket instantly rotates towards the point and moves there straight.

I want the rocket to start moving in which ever direction It's currently facing and slowly rotate towards the point as it goes there.

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
Best Answer

Answer by OrbitGames · Jul 30, 2015 at 09:09 AM

First off, only change dir if the user touches the screen, otherwise it might turn around after the rocket went past targetpos. You can use Quaternion.RotateTowards to slowly rotate towards rotz. (untested)

 float rotationSpeed = 1;
 transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.euler(0,0,rotZ), rotationSpeed);

Last Bug is, don't use

 rigidbody2d.AddForceAtPosition(transform.up * Thrust, transform.up);

the position is actually around the 0 point. You need to use

 rigidbody2d.AddForceAtPosition(transform.up * Thrust, transform.position - transform.up);

to get a point below your rocket

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 MrFalseEye · Aug 01, 2015 at 04:01 PM 0
Share

Thanks! That was exactly what i was looking for. Though i had to change rotZ to -rotZ in Quaternion.RotateTowards. Thanks again!

avatar image
0

Answer by NeverHopeless · Jul 30, 2015 at 09:09 AM

You are observing the right behavior since RigidBody.MoveRotation rotates immediately and RigidBody.MovePosition moves immediately too.

To move the object you should instead use RigidBody.Velocity.

 rigidbody2d.velocity = new Vector3( x, y, z );

For rotation you have two options as i see:

  1. Use Update event to rotate the body w.r.t Time.deltaTime.

  2. Use LeanTween plugin (free, available on Asset store) or Animator component (with two state based animation: Rotate, Steady) to rotate.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate to look at with physics 2 Answers

Objects stuck dragging across floor 1 Answer

How do I make my Projectile rotate towards its direction? 1 Answer

Calculate initial velocity for projectile to reach point at a given angle and calculate angle to reach point at a given velocity. 1 Answer

Making a sphere move and rotate like a real ball on ground 2 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