• 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 akalinxy · Jan 08, 2015 at 07:26 PM · transformobjectlerpaxismove

Lerp a Object along X and Z axis only.

Im new to Unity (still). Sorry if this is a .. stupid question ..

What I want to know, is how/with what you can make a gameobject transform to another postition in space in straight lines.

Here is a image of what I mean.alt text

I know how lerp would work, but that seems to take the most direct way.

unityhelp.png (110.6 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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by hypnoticmeteor · Jan 08, 2015 at 09:38 PM

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
 
     public Vector3 startPos;
     public Vector3 endPos;
     public GameObject gameObjectOne;
     float angleT;
     float distanceH;
     Vector3 pointThree;
     float x;
     bool moveObjectPosOne,moveObjectPosTwo;
 
     public int speed;
     IEnumerator Start () {
         moveObjectPosOne = false;
         moveObjectPosTwo = false;
         yield return StartCoroutine(CalculateVariables());
     }
 
     double DistanceBetweenPoints(Vector3 vectorOne, Vector3 vectorTwo) {
          double distance = Vector3.Distance(vectorOne, vectorTwo);
          return distance;
     }
 
     
     float GetAngleBetweenPoints(Vector3 vectorOne, Vector3 vectorTwo) 
     { 
         float xDiff = vectorTwo.x - vectorOne.x; 
         float yDiff = vectorTwo.z - vectorOne.z; 
         return Mathf.Atan2(yDiff, xDiff) * (180 / Mathf.PI); 
     }
 
     float CalculateX(float angle, float distance) 
     {
         float x = distance * Mathf.Cos(angle);
         return x;
     }
 
     IEnumerator CalculateVariables() {
         gameObjectOne.transform.position = startPos;
         angleT = GetAngleBetweenPoints(startPos, endPos);
         distanceH = (float)DistanceBetweenPoints(startPos, endPos);
         x = CalculateX(angleT, distanceH);
         pointThree = new Vector3(startPos.x - x, startPos.y, startPos.z);
         yield return null;
     }
     
     // Update is called once per frame
     float VecOne;
     float VecTwo;
     void Update () {
         VecOne = Vector3.Distance(gameObjectOne.transform.position,pointThree);
         if (moveObjectPosOne) {
             gameObjectOne.transform.position = Vector3.MoveTowards(gameObjectOne.transform.position, pointThree, speed * Time.deltaTime);
             if (VecOne <= 0) 
             {
                 moveObjectPosOne = false;
                 moveObjectPosTwo = true;
             }
         }
         if (moveObjectPosTwo)
         {
             gameObjectOne.transform.position = Vector3.MoveTowards(gameObjectOne.transform.position, endPos, speed * Time.deltaTime);
             
         }
 
     }
 }

Sixth grade algebra. I have cooked this up real quick. Not sure if this would work exactly but you can start here. Hopefully I am not doing homework. :D

Comment
Add comment · Show 2 · 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 jpthek9 · Jan 08, 2015 at 09:44 PM 0
Share

You learned trig in sixth grade? Props :D

avatar image hypnoticmeteor · Jan 08, 2015 at 09:55 PM 0
Share

Yup, Basics. Nothing great. :D

avatar image
0

Answer by jpthek9 · Jan 08, 2015 at 07:33 PM

Make a queue of positions to lerp to then lerp to the next position if the object reached the last position. There's not much of a way to do this except with hard coding and lots of plumbing code.

The good part is that the solution is intuitive so no fancy maths involved.

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
0

Answer by DanSuperGP · Jan 08, 2015 at 07:50 PM

Lerp is a linear interpolation, so it will always travel along the line between two locations.

If you want to only travel along the y axis first, then travel along the x axis... you need to split it up into two motions. One that does the y motion, and another that does the x motion once the y motion is completed.

There isn't a built in way to do it, you're going to have to write your own.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Vector 3 - move on only one axis 0 Answers

How do you make an object go left and right? 1 Answer

Move a object and then return , like loop 2 Answers

using Transform on a Object will not move it 100% exactly... 1 Answer

move a gameobject to left/right while going up 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