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 /
  • Help Room /
avatar image
0
Question by talhai321 · Jun 26, 2016 at 06:01 AM · transform.positionmovetowardstransform.translatevectors

How can I translate my game object from a certain coordinate to the negative values of the same coordinate?

I want to create a game similar to Loop Mania on the iPhone App Store. For this, I am calling upon Vector2 and using trigonometric functions which move my object in a circle. However, I want the object to translate to the negative values of the coordinates in a straight line when the left button on a mouse is clicked. For example, if my object is at (2, 3) at a certain point in time, I want it to translate in a straight line to (-2, -3). This is my code so far. I appreciate any and all help.

     private float x, y, a, b;
     private float timeCounter;
     private float speed;
     private float slope;
     private Vector2 targetPosition;
 
     private void Start ()
     {
         x = 2.95f;
         y = 0f;
         timeCounter = 0f;
         speed = 2.5f;
     }
 
     //Updates x and y coordinates of the circle
     private void Update()
     {
         timeCounter += Time.deltaTime * speed;
 
         x = Mathf.Cos(timeCounter) * 2.95f;
         y = Mathf.Sin(timeCounter) * 2.95f;
 
         a = -x;
         b = -y;
 
         transform.position = new Vector2(x, y);
         targetPosition = new Vector2(a, b);
 
         translateAcross();
     }
 
     private void translateAcross()
     {
         if(Input.GetMouseButtonDown(0))
         {
             transform.position = Vector3.MoveTowards(transform.position, targetPosition, timeCounter);
          }
     }
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 Mindmapfreak · Jun 26, 2016 at 11:43 AM

It is probably easier to use Unity's built-in functions:

 private float angularSpeed;
 private float speed;
 private Vector2 targetPosition;
 
 private void Start()
 {
     this.angularSpeed = 100.0f;
     this.speed = 10.0f;
     this.transform.position = new Vector2(0, 2.95f);
     this.targetPosition = this.transform.position;
 }
 
 //Updates x and y coordinates of the circle
 private void Update()
 {
     if (Vector2.Distance(transform.position, targetPosition) < 0.0001f)
     {
         this.transform.RotateAround(Vector2.zero, Vector3.forward, Time.deltaTime * this.angularSpeed);
         this.targetPosition = this.transform.position;
     }
     else
     {
         this.transform.position = Vector2.MoveTowards(this.transform.position, this.targetPosition, Time.deltaTime * this.speed);
     }
 
     if (Input.GetMouseButtonDown(0))
     {
         this.targetPosition = -this.transform.position;
     }
 }

Some additional notes:

  • There is already a built-in variable that does what you are doing with timeCounter, it is Time.time (except when you want to save the time for a specific object)

  • Input.GetMouseButtonDown only gets called once in the frame in which the mouse button is pressed (which was part of your problem)

Comment
Add comment · Show 1 · 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 talhai321 · Jun 26, 2016 at 02:51 PM 0
Share

I looked through the documentation page but didn't catch that. Thank you very much! I would reward you with points if I had any. I just joined recently.

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

Move Gameobject towards/away from position based on the proximity of 2 other objects 0 Answers

Changing transform.position through a reference 1 Answer

how to make the player move in only 2 directions? 0 Answers

transform position don't work with scaled parent 0 Answers

Hi I'm making a game for Android(2d game). I have to move a ball using the swipe(not just vertical and horizontal). The problem is that the ball is moving in the wrong Y axis and I don't understabd why 0 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