Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Saiura · Jun 14, 2014 at 09:18 AM · movementtransformvector3transform.position

fixed distance for Vector3.moveTowards ?

What I am trying to find out is, if there is a way to create a preset distance for the Vector3.moveTowards statement.

Meaning instead of something like

 transform.position = Vector3.MoveTowards(transform.position, target.position, speed);

If it is possible to do something like

 transform.position = Vector3.MoveTowards(transform.position, 20 in x-direction, speed);

Meaning instead of a target.position, use a fixed distance instead. And if not, if there is a possible work-around for 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
Best Answer

Answer by robusto · Jun 14, 2014 at 10:33 AM

This is what Vector3.MoveTowards essential does:

 Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)
 {
   Vector3 diff = target - current;
   float mag = diff.magnitude;
   if( mag < maxDistanceDelta )
      return current;
   diff.Normalize();
   return (maxDistanceDelta * diff) + current;
 }

If you want it to move only on the x component you need to make the y and z components the same as your position so this function doesn't move it closer to those y and z values since they are already there.

 Vector3 newTargetPos = target.position;
 newTargetPos.y = transform.position.y;
 newTargetPos.z = transform.position.z;
 
 transform.position = Vector3.MoveTowards(transform.position, newTargetPos, speed);

The var speed (maxDistanceDelta) controls how big/small of a step you take in that direction everytime you call MoveTowards.

Comment
Add comment · Show 3 · 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 Saiura · Jun 14, 2014 at 10:36 AM 0
Share

That would work for the y- and z-axis, though how do I set the newTargetPos x-axis? I mean, I want it to move by a fixed value from whereever the current position is. Something like, whereever the object is on the x-axis, make it move further by 20.

avatar image robusto · Jun 14, 2014 at 10:56 AM 0
Share

Global:

 transform.position += Vector3.right * 20f; // or -20f to move left

Local:

 transform.position += transform.right * 20f; // or -20f to move left
avatar image Saiura · Jun 14, 2014 at 11:19 AM 0
Share

thanks, that should get me forward

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Move Obj A towards Obj B BUT keep going in same direction when Obj A has reached Obj B 1 Answer

Trouble converting transform.position to C# 1 Answer

Implement the equation as a code? 1 Answer

Move from one position to another in x seconds 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