Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by unity_68Qt_Q-7Cs99dQ · Mar 12, 2019 at 05:40 PM · movementanglecurve

How to move object in 45/30/15 degree arch.

My GameObject ( say a car) is move along in some direction and some point it need to move in from p1 to p2, (the red spot in picture, each element) in and arch like fashion and not just straight. Like a turn 45 degree turn in the road, alt text (the game is in 3d)
.

I only know where to road piece starts and ends and if it 45/30 and so forth.

So fare i have only been apple to move straight. I use Vector.Lerp.

t += Time.deltaTime / timeToMove; transform.position = Vector3.Lerp(startPos, endPos, t); transform.rotation = Quaternion.Lerp(transform.rotation, rot, t);

untitled.png (5.2 kB)
untitled.png (5.1 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 unity_68Qt_Q-7Cs99dQ · Mar 14, 2019 at 02:50 PM

I managed to find a way to calculate the origin of the arc that i wanted my little car to follow. I then used a while loop around (code snip) inside an IEnumerator method.

 t += Time.deltaTime / timeToMove;
 transform.RotateAround(getCenter(startPos, endPos, e.Direction), 
                  Vector3.up, (float) (turnDegreeFloat * Math.PI / 180) * t);

  

The getCenter is as follows:

 Vector3 getCenter(Vector3 startPos, Vector3 endPos, String direction)
     {
         Vector3 vectorBetweenPoints = endPos - startPos;
         float LengthBetweenPoints = vectorBetweenPoints.magnitude;
         Vector3 centerBetweenPoints = new Vector3((startPos.x + endPos.x) / 
         2, (startPos.y + endPos.y) / 2, (startPos.z + endPos.z) / 2);
         float centerX = 0;
         float centerZ = 0;
         if (direction == "RIGHT")
         {
            centerX = (float)(centerBetweenPoints.x - ((Math.Sqrt(Math.Pow(CurveRadius, 2) - 
           Math.Pow(LengthBetweenPoints / 2, 2)) * (startPos.z - endPos.z)) /  LengthBetweenPoints));
            centerZ = (float)(centerBetweenPoints.z - ((Math.Sqrt(Math.Pow(CurveRadius, 2) - 
           Math.Pow(LengthBetweenPoints / 2, 2)) * (endPos.x - startPos.x)) / LengthBetweenPoints));
         }
         else
         {
             centerX = (float)(centerBetweenPoints.x + ((Math.Sqrt(Math.Pow(CurveRadius, 2) - 
            Math.Pow(LengthBetweenPoints / 2, 2)) * (startPos.z - endPos.z)) / LengthBetweenPoints));
             centerZ = (float)(centerBetweenPoints.z + ((Math.Sqrt(Math.Pow(CurveRadius, 2) - 
            Math.Pow(LengthBetweenPoints / 2, 2)) * (endPos.x - startPos.x)) / LengthBetweenPoints));
         }
 
         Vector3 vector = new Vector3(centerX, startPos.y, centerZ);
         return vector;
     }

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 WarmedxMints · Mar 12, 2019 at 07:32 PM

You could create a curve along each piece and then get a point on that curve for your position and angle. The code to move it could be done better but it should serve as a starting point for you.

  using UnityEngine;
 
 public class RotateBetweenVectors : MonoBehaviour
 {
     //These could just be vectors rather than transforms if you wish.
     public Transform StartPoint;
     public Transform Midpoint;
     public Transform Endpoint;
 
     public Transform ObjectToMove;
     public float MoveSpeed = 0.01f;
 
     private float _currentPointAlongCurve;
 
     //The amount of points along the curve in the scene view
     public int CurveResolution = 16;
 
     public void OnDrawGizmos()
     {
         if (StartPoint == null || Midpoint == null || Endpoint == null)
             return;
 
         var percent = 1f / CurveResolution;
 
         Gizmos.color = Color.blue;
 
         for (var i = percent; i < 1+ percent; i += percent)
         {            
             Gizmos.DrawLine(GetPointOnCurve(StartPoint.position, Midpoint.position, Endpoint.position, i - percent),
                 GetPointOnCurve(StartPoint.position, Midpoint.position, Endpoint.position, i));
         }
     }
 
     public void Update()
     {
         if (ObjectToMove == null)
             return;
 
         _currentPointAlongCurve += Time.deltaTime * MoveSpeed;
 
         if (_currentPointAlongCurve > 1)
             _currentPointAlongCurve = 0;
 
         ObjectToMove.transform.position = GetPointOnCurve(StartPoint.position, Midpoint.position, Endpoint.position, _currentPointAlongCurve);
         ObjectToMove.transform.rotation = Quaternion.LookRotation(GetPointOnCurve(StartPoint.position, Midpoint.position, Endpoint.position,
             _currentPointAlongCurve + 0.01f) - GetPointOnCurve(StartPoint.position, Midpoint.position, Endpoint.position, _currentPointAlongCurve), Vector3.up);
     }
 
     public Vector3 GetPointOnCurve(Vector3 p0, Vector3 p1, Vector3 p2, float t)
     {
         return Vector3.LerpUnclamped(Vector3.LerpUnclamped(p0, p1, t), Vector3.LerpUnclamped(p1, p2, t), t);
     }
 }
 
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 unity_68Qt_Q-7Cs99dQ · Mar 13, 2019 at 10:49 AM 0
Share

Thank you very much for you answer. I really appreciate it. The problem is that i dont have the luxery of setting a midpoint in the editor. Actually, what is happening in the game is that i read an xml file, which tell me where to put element, and there type. The from and event log file i read where and element is in my system. Ex. car with id 3 is at element of type curve 45, or straight. each element has startPoint and EndPoint, which is what i used to move across the straight elements.

I found that actually i know the curves center radius to be 2.5. Any idea how i calculate a midpoint with this infomation.

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

157 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

Related Questions

how to move objects along a curve? 2 Answers

Make the character move along a curved road. 0 Answers

click and move script no working (as intended) 0 Answers

High speed sliding (ski/sled) from curved slopes in a 2d game. 0 Answers

Struggling to get the rotation the player is moving in. 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