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 arturoza · Jul 29, 2016 at 05:47 PM · movementpositiontranslatewhile

Click to Move Object till it reaches a certain place

Hi, So I have a Gameobject that I want to move slowly when I click it until it reaches a certain place, so far I got this:

 using UnityEngine;
 using System.Collections;
 
 public class Moving_Planks : MonoBehaviour {
 
 
     public Transform targetpos;
     public float Speed = 3f;
     // Use this for initialization
     void Start () {
 
     }
 
     // Update is called once per frame
     void Update () 
     {
         
     }
     public void MovePlanksToPlace ()
     {
         while( this.transform.position != targetpos.transform.position)
         {
             float step = Speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards (transform.position, targetpos.position, step);
         }
     }
 }
 

But when I click it, it just moves in one step, I'd like to be slowly until it reaches the place.

I might be approaching this on the wrong side, so I'd be grateful if you could guide me to the correct path

thank you

Comment
Add comment · Show 3
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 arturoza · Jul 29, 2016 at 06:14 PM 0
Share

The code works If I use it on Update() but clicking it messes the whole code up

avatar image arturoza · Jul 29, 2016 at 08:31 PM 0
Share

I just tried, It just doesn't work with any mouseDown, $$anonymous$$ouseEnter or whatever at all

avatar image Firedan1176 arturoza · Jul 30, 2016 at 07:30 PM 0
Share

Please let me know if my answer helped you. If it did, I'd appreciate an accepted answer to close the question, and also boost my self esteem :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Firedan1176 · Jul 29, 2016 at 11:26 PM

I'm sort of confused still on what you're wanting. These are the scenarios I'm picturing:

  1. The object jumps 'increments' each time you click

  2. The object slides 'increments' each time you click

  3. The object slides to the target the first (and only) time you click

For the first option, you could do:

 public Transform targetPos;
 public int increments;
 float incrementDistance;
 
 void Start() {
     incrementDistance = Vector3.Distance(transform.position, targetPos.position) / increments;
 }
 
 void Update() {
     if(Input.GetMouseButtonDown(0)) JumpIncrement();
 }
 
 void JumpIncrement() {
     transform.position += (transform.position - targetPos.position) * incrementDistance;
 }


For the 2nd scenario you could try something like:

 public Transform targetPos;
 public int increments;
 public float speed;
 float incrementDistance;
 
 void Start() {
     incrementDistance = Vector3.Distance(transform.position, targetPos.position) / increments;
 }
 
 void Update() {
     if(Input.GetMouseButtonDown(0)) JumpIncrement();
 }
 
 void JumpIncrement() {
     transform.position = Vector3.Lerp(transform.position, transform.position + (transform.position - targetPos.position) * incrementDistance, speed * Time.deltaTime);
 }


And the 3rd:

 public Transform targetPos;
 public float speed;
 
 void Update() {
     if(Input.GetMouseButtonDown(0)) GoToTarget();
 }
 
 void GoToTarget() {
     transform.position = Vector3.Lerp(transform.position, targetPos.position, speed * Time.deltaTime);
 }


Sorry, I get confused easily. Do any of these help you?

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

59 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

translate 2 Answers

Match position of two object without making one a child of the other 4 Answers

Object transported to point instead of moving towards it? 1 Answer

Stop moving at the target position by using Vector3.forward 0 Answers

How can i make my enemy jump with a delay 1 Answer


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