Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 sy1997_unity · Nov 03, 2021 at 04:26 PM · rotationpositionalgorithm

How can I adjust my position to a specific point after rotating?

Hello, I have a player that moves on the platform and rotates 90 degree when it collides with a bend. After it passes the bend, it must continue moving from the middle of the next platform. I could set its position manually but I don't want it to teleport. I have tried to reach the middle by changing player's rotating speed but it is pretty hard to find the "perfect" speed. I want to know if there is a proper way to do this ? Thank you.

It must rotate like this after rotating. Equal distances When i increase rotation speed it continues moving like this. I want to keep distances between edges equal and independent from rotation speed. How can I achieve it ? Not equal distances

Here I have a video that shows how it goes out of the middle of the platform: https://files.fm/f/w9bdcswtn

Movement Controller:

 public class PlayerMovementController : MonoBehaviour
     {
         [SerializeField] private ScriptablePlayerMovementSettings _movementSettings;
 
         private Rigidbody rb;
 
         private void Awake()
         {
             Initialize();
         }
 
         private void Initialize()
         {
             rb = GetComponent<Rigidbody>();
         }
 
         private void FixedUpdate()
         {
             Move();
         }
 
         private void Move()
         {
             Vector3 movement = transform.forward * _movementSettings.MovementSpeed * Time.fixedDeltaTime;
             rb.MovePosition(transform.position + movement);
         }
     }

Rotation Controller:

 public class PlayerRotationController : MonoBehaviour
     {
         [SerializeField] private ScriptablePlayerRotationSettings _rotationSettings;
 
         private float _direction = 0;
         public float Direction { get => _direction; set => _direction = value; }
 
         private Vector3 _lastRotation = Vector3.zero;
 
         private void Update()
         {
             if (Direction != 0)
                 Rotate();
         }
 
         private void Rotate()
         {
             PlayerManager.Instance.PlayerState = PlayerState.Rotating;
             Vector3 targetRotation = GetLastRotation() + Vector3.up * Direction * 90f;
             targetRotation = new Vector3(targetRotation.x, targetRotation.y % 360, targetRotation.z);
             transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(targetRotation), _rotationSettings.RotationSpeed * Time.deltaTime);
             if (transform.eulerAngles.Equals(Quaternion.Euler(targetRotation).eulerAngles))
             {
                 SetLastRotation();
                 Direction = 0;
                 PlayerManager.Instance.PlayerState = PlayerState.Normal;
             }
         }
 
         private void SetLastRotation()
         {
             _lastRotation = transform.rotation.eulerAngles;
         }
 
         private Vector3 GetLastRotation()
         {
             return _lastRotation;
         }
     }

I get the direction value based on bend type:

 public class PlatformCollisionManager : MonoBehaviour
     {
         private void OnTriggerEnter(Collider other)
         {
             if (other.gameObject.GetComponentInParent<PlayerRotationController>() != null)
             {
                 PlayerRotationController playerRotationController = other.gameObject.GetComponentInParent<PlayerRotationController>();
                 playerRotationController.Direction = (GetComponentInParent<MiddlePlatformMono>().GetPlatformType == PlatformType.TurnLeft) ? -1f : 1f;
             }
         }
     }


jellynotequal.png (112.1 kB)
jellynormal.png (74.3 kB)
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
Best Answer

Answer by sy1997_unity · Nov 08, 2021 at 08:17 PM

Thanks for the answers. I just solved it by using RotateAround. I detect collision when it hits the platform and start rotating.

 private void Rotate()
         {
             PlayerManager.Instance.PlayerState = PlayerState.Rotating;
             Vector3 targetRotation = PlayerHelper.GetCurrentRotation() + Vector3.up * Direction * 90f;
             targetRotation = new Vector3(targetRotation.x, targetRotation.y % 360, targetRotation.z);
             transform.RotateAround(RotationPivotPoint.position, Vector3.up * Direction, _rotationSettings.RotationSpeed * Time.deltaTime);
 
             if (Mathf.Abs(transform.eulerAngles.y) <= 270f)
             {
                 transform.eulerAngles = targetRotation;
                 PlayerHelper.SetCurrentRotation(transform.rotation.eulerAngles);
                 Direction = 0;
                 PlayerManager.Instance.PlayerState = PlayerState.Normal;
             }
         }

I take corner of the bend as rotation pivot point and move my player around it. If it reaches 90 or -90 degree, it stops rotating. There is still 0.0002 ~ 0.0004 gap from middle of the platform but it is acceptable for me.

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
1

Answer by MarekRimal · Nov 05, 2021 at 04:35 PM

Hi, I think it would be really hard to figure out the perfect rotation.

I would generate a path as a sequence of points and then make the player move the path. The turn is actually a quarter circle. So the approach is to generate points on the quarter circle and make the player go through all these points while looking on each points he is going to.

I made some quick code for you.

 // This will generate 90 degree turn to the right
 // The resolution smoothens the turn
 List<Vector3> MakeRightTurn(float radius, int resolution)
 {
   List<Vector3> positions = new List<Vector3>();
 
   float t = 0;       
   float step = 0.5f * Mathf.PI / resolution;  // We are making only one quadrant of the circle so 0.5f instead of 2.0f
   for (int i = 0; i <= res; i++)
   {
     float x = radius * Mathf.Sin(t);
     float z = radius * Mathf.Cos(t);
     Vector3 pos = new Vector3(x, 0, z);
     positions.Add(pos);
 
     t += step;
   }
 
   return positions;
 }
 
 // This will generate 90 degree turn to the left
 // The resolution smoothens the turn
 List<Vector3> MakeLeftTurn(float radius, int resolution)
 {
   List<Vector3> positions = new List<Vector3>();
 
   float t = 0;       
   float step = 0.5f * Mathf.PI / resolution;  
   for (int i = 0; i <= res; i++)
   {
     float x = radius * Mathf.Sin(t);
     float z = radius * Mathf.Cos(t);
     Vector3 pos = new Vector3(x, 0, -z);      // To make left turn we just make the z negative
     positions.Add(pos);
 
     t += step;
   }
 
   return positions;
 }
 
 void Start()
 {
   // This will make player do right turn
   MovePlayer(MakeRightTurn(1, 10));
 }
 
 void MovePlayer(List<Vector3> positions)
 {
   // Go throught each point
   for(int i=0; i<positions.Length; i++)
   {
     float threshold = 0.001f
     Vector3 dir = (positions[i] - transform.positions).normalized;
     transform.LookAt(positions[i]);
 
     // Move player untill he is on the destination point
     while(Vector3.Distace(transform.positions, positions[i]) > threshold)
     {
       transform += dir * speed * Time.deltaTime;
     }
   }
 }


I didnt run the code but I think this approach should work. Like this you can assemble the whole path for the player. If you have any questions, you can ask.

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 MarekRimal · Nov 05, 2021 at 04:38 PM 0
Share

You can either make the whole player movement like this by adding straight paths between these turns as a sequence of two points.

Or you can make the player do these turns after he hit some collider as you had it before.

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

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

Player not facing the mouse correctly 1 Answer

Change object position on trigger enter 3 Answers

How to move and rotate a camera smoothly and simultaneously? 0 Answers

Treasure Chest Problems 1 Answer

Rigidbody constraints in local space? 7 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