• 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 Ekta-Mehta-D · May 16, 2013 at 07:21 AM · 2dmoveangletranslate

Move Object With Angle In 2D

Hello ..

I am Developing 2D game in that I have Scene in x(horizontal)-y(vertical) Axis..

I want to move my Object From Y direction with Some random angle.. But I am not getting idea about how can i translate object with angle..

So please Help me..

Thanks for your support and help..

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by raimon.massanet · May 16, 2013 at 07:36 AM

If you want to rotate your object a certain angle, you should do something like this:

 void Start () {
     
    // Initially object is facing up (Y-axis)
    transform.localRotation = Quaternion.Euler(Vector3.up);
 }
 
     
 public void Rotate(float angle)
 {
    transform.Rotate(0, 0, angle);
 }
Comment
Add comment · Show 5 · 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 Ekta-Mehta-D · May 16, 2013 at 07:41 AM 0
Share

No i dont want to rotate my Game Object.. i just want that object to be translated with some angle so it will be some thing like cross direction..

avatar image raimon.massanet · May 16, 2013 at 07:44 AM 0
Share

I don't understand what you mean by

translated with some angle

. Do you mean that you want the object to move somewhere using polar coordinates?

avatar image Ekta-Mehta-D · May 16, 2013 at 08:54 AM 0
Share

hmm.. something like that..

avatar image raimon.massanet · May 16, 2013 at 09:08 AM 0
Share

You need to define a radius, and calculate the polar coordinates:

 private Vector3 currentPosition;
     
 public Vector3 PolarCoordinates(float angle, float radius)
 {
    // Assuming movement is on XY plane
    Vector3 relativePosition = new Vector3(radius * $$anonymous$$athf.Cos(angle), radius * $$anonymous$$athf.Sin(angle), 0);
             
    return currentPosition + relativePosition;
 }

Then simply translate to the new position.

avatar image Chintan-Kansagara · Dec 15, 2017 at 07:23 AM 0
Share

Hi my 2d character rotate in hit point angle to rotate but character rotate in turn to rotate not a direct rotate

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit = new RaycastHit(); if (Physics.Raycast (ray, out hit, 100)) { Vector3 hitPoint = hit.point;

         float dist = Vector3.Distance(hitPoint, transform.position);
         Debug.Log(dist);

         targetDir = hitPoint - transform.position;

         float angle = $$anonymous$$athf.Atan2(targetDir.y, targetDir.x) * $$anonymous$$athf.Rad2Deg;
         //transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, angle), 20 * Time.deltaTime);    
         //Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
         //transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 1);

     }

plz help me...

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 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

Related Questions

Switching Cameras at runtime 1 Answer

A node in a childnode? 1 Answer

Unity2D move + turn with a fixed angle 1 Answer

2D : Rotation of Object With x-y Axis 2 Answers

Move an object in 2D space along the angle of the object 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges