Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 fathantara · Jan 31, 2017 at 08:30 PM · following

hey guys i just wanna make simple script that an object will follow point using point's position of x and z, but when i try to play it its gonna to wrong direction not to the point.

void Start () {

     float xpoint = point.transform.position.x;
     float zpoint = point.transform.position.z;
     float xme = this.transform.position.x;
     float zme = this.transform.position.z;
     Debug.Log(" point x: " + xpoint + " point z: " + zpoint + " me x: " + xme + " me z: " + zme);

 }
 
 // Update is called once per frame
 void Update () {
     if (xme <= xpoint)
         vectorx = 1 * speed;
     if (xme >= xpoint)
         vectorx = -1 * speed;
     if (zme <= zpoint)
         vectorz = 1 * speed;
     if (zme >= zpoint)
         vectorz = -1 * speed;


 }

 

 void FixedUpdate()
 {
     if (xme != xpoint)
         Debug.Log("hey pewds");
         transform.position += new Vector3(vectorx * Time.deltaTime, 0f, 0f);
     if (zme != zpoint)
         transform.position += new Vector3(0f, 0f, vectorz * Time.deltaTime);
 }

}

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 destructivArts · Jan 31, 2017 at 11:48 PM

It would be faster, and more reliable to do something like this:

 public Vector3 TargetPoint;
 public float Speed;
 public float TargetDistance;
 
 private Vector3 m_MoveDirection;
 
 void Start () {
     m_MoveDirection = TargetPoint - transform.position;
     m_MoveDirection.Normalize();
 }
 
 void Update () {
     float CurrentDistance = Vector3.Distance (TargetPoint, transform.position);

     // If moving full speed for one frame would have the object overshoot, just move to the target
     if (CurrentDistance < Speed * Time.deltaTime) {
         transform.position = TargetPosition;

     // If we are further than our minimum distance to the target, move full speed towards it
     } else if (CurrentDistance > TargetDistance) {
         transform.Translate (m_MoveDirection * Speed * Time.deltaTime);
     }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS Animations for head and arms 0 Answers

enemy shakes when it gets to a certin position 0 Answers

How to make an enemy chase you and catch you when close (like slendytubbies) 0 Answers

How to limit rotation to target 2 Answers

Following AI - Similar to Snake Game 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