• 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 /
  • Help Room /
avatar image
Question by SirusDoma · Aug 11, 2017 at 06:50 AM · transformscript.vector3mathdirection

Applying direction into transform position

Hello! I and my team are new in Unity and I would ask a simple question. I'm developing an action game which involving music as the core of its gameplay, it still under prototype, but I'm facing serious problem regarding to core gameplay.

It contains a set of entities (game objects) that move into several directions; top, right, left, bottom and diagonal moves (e.g: top-left, etc etc).

The calculation of these entities position require special calculation and I able to produce it in Unity (these position involving tempo of a music), fyi this position is calculated in Update() function.

And I use following function to determine the position of transform of the entities:

     private Vector3 _position;
 
     private void DeterminePosition(float offset)
     {
         // In actual code, the _position is initialized under Start() method
         // But for this simplification sake, I'll just put it here
         _position = _position == null ? new Vector3(0, 0, 1f) : _position;
 
         if (Direction == Direction.Up || Direction == Direction.RightUp || Direction == Direction.LeftUp)
         {
             _position.y = offset;
         }
         if (Direction == Direction.Down || Direction == Direction.RightDown || Direction == Direction.LeftDown)
         {
             _position.y = -offset;
         }
         if (Direction == Direction.Left || Direction == Direction.LeftDown || Direction == Direction.LeftUp)
         {
             _position.x = -offset;
         }
         if (Direction == Direction.Right || Direction == Direction.RightDown || Direction == Direction.RightUp)
         {
             _position.x = offset;
         }
 
         transform.position = _position;
     }

The code works perfectly as intended, however the game need to be change. Instead of fixed direction (e.g: Up, Right, Left, Bottom, RightUp, DownLeft etc etc) we decide to use value of degree for the direction (0 to 360 degree).

And now I've no idea how to impement this. I've tried to use following codes, but it doesn't work:

     _position = new Vector3(offset, offset, transform.position.z);

     // Where the direction is between 0 .. 360
     transform.position = Quaternion.Euler(0, direction, 0) * _position;

Can anybody else come up with solution? Thanks in advance!

Comment

People who like this

0 Show 0
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

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

91 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 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 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 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

How to properly make an object follow the edge of another object? 0 Answers

Problem with aiming Script(Only Some Code Works) 0 Answers

Gameobjects transform position minus range 1 Answer

is it possible for velocity vectors to be added on the players axis? 1 Answer

Creating a simple character controller (Script provided) 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