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 DeTziko · Nov 19, 2013 at 06:35 PM · objectmovevectortowardsgradually

What am I doing wrong?

I want to move an object from its initial position to another one. This is the code I got:

 Vector2 aimStart = new Vector2(0f,0f);
     Vector2 aimEnd = new Vector2(-0.4f,0.4f);
     public float aimSpeed = 1;
 
     
 
     void Update()
 
     {
         transform.position = Vector2.MoveTowards(aimStart,aimEnd,aimSpeed);
     }

So there are two vectors, one of them (posStart) is the initial position and the other one (posEnd) is the vector my object should move towards to. Anyway, the code doesn't work correctly. The object just jumps to a position that has nothing to do with the vectors posStart and posEnd. It should move gradually towards endPos. I don't care about the 'z' as it reamins always the same. Any idea how to fix it? Remember I'm using C# code! Thanks!

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
1
Best Answer

Answer by robertbu · Nov 19, 2013 at 06:39 PM

For MoveTowards, you want to do it this way:

 transform.position = Vector2.MoveTowards(transform.position,aimEnd,aimSpeed * Time.deltaTime);

At the beginning of your move (in Start() for example), you would assign aimStart to transform.position so that the move starts in the right position. In addition, you need to use Time.deltaTime to keep your code frame rate independent.

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 DeTziko · Nov 19, 2013 at 07:12 PM 0
Share

I did what you said, but the problem remains the same. I think something with endPos might be wrong, but I can't figure out what it is. I don't know if it's useful, but you should know this code is for an Aim Down the Sights script in an FPS. startPos is where the gun is, endPos where it should be when I aim. The gun is attached to the main camera, and the script is attached to the gun

avatar image DeTziko · Nov 19, 2013 at 07:38 PM 0
Share

Forget about it, my bad! Thanks, the code works!

avatar image robertbu · Nov 19, 2013 at 08:26 PM 0
Share

Start with a new scene, add a cube, and attach this script:

     using UnityEngine;
     using System.Collections;
 
     public class Bug25c : $$anonymous$$onoBehaviour {
     
     Vector2 aimStart = new Vector2(0f,0f);
     Vector2 aimEnd = new Vector2(-0.4f,0.4f);
     public float aimSpeed = 1;
    
     void Start () {
         transform.position = aimStart;
     }
  
     void Update() {
        transform.position = Vector2.$$anonymous$$oveTowards(transform.position, aimEnd, aimSpeed * Time.deltaTime);
     }
 }

Your cube will move from position (0,0,0) to position (-04, 0.4,0.0) at the rate of 1 unit per second.

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

17 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

Related Questions

Move Object A towards Object B at a definite speed 1 Answer

Move the player towards the cube to attack. 0 Answers

Moving an Object towards another Moving Object 2 Answers

Limiting Drag and Drop 1 Answer

Aligning an object's rotation direction to a vector 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