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 /
avatar image
0
Question by tubswitch · Apr 01, 2016 at 06:28 AM · transformvector3statesnormalizedbehavior

Moving one GameObject to another, and Vector Normalization issues

I am attempting to move one GameObject between its starting point and another point to create an elevator effect. However, depending on the movement speed of the object, the function that gets the Normalized vector that provides the direction to the target object will occasionally begin returning zero and stall out.

Let me try and summarize the code I'm using:

 public float DistanceToMove;
 public float SpeedToMove;
 
 private Vector3 start_point;
 private Vector3 end_point;
 private float verticalMove;
 private bool moveUp;
 
 void Start()
 {
     start_point = transform.position;
     end_point = transform.position + (Vector3.up * DistanceToMove);
     moveUp = true;
 }
 
 void Update()
 {
     UpdateElevator();
     Move();
 }
 
 void UpdateElevator()
 {
     if(moveUp)
     {
             //must check if it is greater than the target transform, not equal to it,
             if (transform.position.y >= end_transform.y)
             {
                 moveUp = false;
                 return;
             }
         MoveTowards(end_point);
     }
 
     else if (!moveUp)
     {
             if (transform.position.y <= start_transform.y)
             {
                 moveUp = true
                 return;
             }
 
         MoveTowards(start_point);
     }
 }
 
 void MoveTowards(Vector3 target)
 {
     //get the direction between position and the target
     Vector3 MoveTowardsVector = transform.position - target;
     //get the normalized vector between the two
     MoveTowardsVector = Vector3.Normalize(MoveTowardsVector);
 
 
     //because we're moving backwards along the direction between the two, we get the negative of the point
     VerticalMove = -MoveTowardsVector.y;
 }
 
 void Move()
 {
     Vector3 moveDirection = new Vector3(0, (VerticalMove * SpeedToMove), 0);
     transform.position += moveDirection * Time.fixedDeltaTime;
 }

Specifcally at this line, MoveTowardsVector = Vector3.Normalize(MoveTowardsVector); the issue seems to crop up. Generally this will return either -1 or 1, depending on whether the GameObject is above or below its target transform. My only idea is that the "state" bool might be changing at an unexpected time and simply cease updating.

EDIT:

After adding the following code to the UpdateElevator function

 Debug.Log("transform.position.y: " + transform.position.y);
 Debug.Log("start_transform.y: " + start_transform.y);

When playing my test case, I was able to find that the object would stall out when the Transform.position.y became "transform.position.y: 1.192093E-06." After that, it ceased to change the position, and therefore never matches or exceeds the target to cause a state change. Additionally, the normalized MoveTowardsVector returns 0, which indicates that it is too close to determine a direction. After that the object will simply cease to move, since MoveTowardsVector will never change.

One possible solution would be for me to first check if the normalized MoveTowardsVector returns 0; if so, then I could directly move the object's transform to match the Target's, like this

         void MoveTowards(Vector3 target)
         {
 
             //get the direction between position and the target
             Vector3 MoveTowardsVector = transform.position - target;
             //get the normalized vector between the two
             MoveTowardsVector = Vector3.Normalize(MoveTowardsVector);
  
 
             if (MoveTowardsVector.x == 0 && MoveTowardsVector.y == 0)
             {
                 //if the Normalized vector is 0, the object is probably nearly overlapping the target. 
                 //Therefore, we move it directly there.
                 transform.position = new Vector3(target.x, target.y, target.z);
             }
 
             else
             {
                 //because we're moving backwards along the direction between the two, 
                //we get the negative of the vector
                HorizontalMove = -MoveTowardsVector.x;
                VerticalMove = -MoveTowardsVector.y;
             }
             

However, I'm trying to avoid directly manipulating the transform outside of the "Move" function, so if there's a better method, I'd love to hear it.

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 Dave-Carlile · Apr 01, 2016 at 07:01 PM

One thing you might try is to clamp the position to the target position. Online 27 and 37 where you check if it has reached the target...

 if (transform.position.y >= end_transform.y)
 {
     ... your stuff to change direction...
     var position = transform.position;
     transform.position = new Vector3(p.x, end_transform.y, p.z);  // set y to the target y
 }

Not sure if that will make your other math work out properly or not.

Another way of moving a platform back and forth is to use a linear interpolation function (Lerp) such as Vector3.Lerp. It takes as input the end points of your line, and a value t that varies between 0 an d 1. When you pass in 0 for t it will return the first point. When you pass in 1 for t it will return the second point. And when you pass in a value in between 0 and 1 it will return a point between the first and second end points. I like to think of t as a percentage - e.g. 0.5 will return the point 50% between the two input points. 0.25 would return the point 25% of the way between the input points.

So your platform position becomes...

 transform.position = Vector3.Lerp(start_point, end_point, t);

Now all you have to do is figure out how to change t over time. One way to do that is very similar to what you're already doing with your vectors. Add Time.deltaTime * direction to t each frame where direction is +1 or -1. When t > 1 set direction to -1, when t < 0 set direction to +1.

Another option that is a bit more straightforward, but a bit more blackbox, is to use Mathf.PingPong. This function takes any value that changes over time and "ping pongs" it between 0 and a value of your choosing. So...

 float t = Mathf.PingPong(Time.time, 1);
 transform.position = Vector3.Lerp(start_point, end_point, t);

This will move t between 0 and 1, then back toward 0, then back toward 1, and so on. Plugging that into your Lerp function will automatically move the transform back and forth between your two points.

Now you can do some interesting things with t using Easing Functions. Mathf has one called SmoothStep. This will cause t to change a little more slowly as it approaches 0 or 1, so your platform will ease into those positions instead of an instant stop and change direction. You can Google Easing Functions and find a lot more interesting things with those.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Vector3 sets to different coordinates than specified 1 Answer

Moving by rotation of objects 0 Answers

Get the Vector position of the player object when clicking another object C# 1 Answer

Moving a Platform 1 Answer

Why using = isn´t fast enough for changing Vector3 values from the transform? 2 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