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 Rick74 · Sep 16, 2013 at 10:04 PM · javascripttransformtranslate

Move an object to a specific point?

So I'm trying to get an object to move to a specific point on a click. I had this working in a separate scene with just a cube a very simple version of this code;

 var smooth: int = 0;                                                 
 
 private var targetPosition:  Vector3;
  
 function Update () 
 {
     targetPosition = Vector3 (3.3, -1.49, -4.34);
  
     transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
 }

This works, however...

 // checks for mouse over object
 if ( Physics.Raycast ( ray, hit, 30 ))                                                                                                      
     {
         // checks if we have clicked the button, and the object has a tag "resource"
           if ( hit.collider.gameObject.tag == "resource" && Input.GetMouseButtonDown (0))                                                                           {

             // gets the object _LevelMaster
                var getResources = GameObject.Find("_LevelMaster");
                var targetPosition = Vector3 (0, 0, 0);

             // adds 25 to the resourceCount in _LevelMaster
             getResources.GetComponent(LevelMaster).resourceCount += 25;                                                                            
                getResources.GetComponent(LevelMaster).UpdateHUD ();
                hit.collider.gameObject.transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * resourceSpeed);                                                
             //Instantiate ( explosion, hit.collider.gameObject.transform.position, Quaternion.identity );                                        // plays explosion particle effect    
             //Destroy (hit.collider.gameObject);                                                                                                    // deletes the object we have hit
            }
        }

This does not.

In the second code block, the object snaps instantly to the camera (which is the object housing this script) There is no smooth interpolation over time.deltaTime, and regardless of what coordinates I put in targetPostion, it always snaps to the camera.

Anyone have any ideas?

Comment
Add comment · Show 5
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 Tanshaydar · Sep 16, 2013 at 10:43 PM 0
Share

You might want to take a look at iTween

avatar image Jamora · Sep 16, 2013 at 10:51 PM 0
Share

Have you read this question about lerping?

avatar image Rick74 · Sep 16, 2013 at 11:32 PM 0
Share

thanks Jamora, I hadn't seen that page, reading it now.

avatar image Rick74 · Sep 17, 2013 at 12:03 AM 0
Share

Well reading up on that page breakdown on Lerping,and it's interesting with regards to the various methods of going for a linear Lerp to one that eases in as it reaches it's destination...

But I'm still confused as to why my object ISN'T lerping in the second code block? All the proper parameters are there to tell it to move to a specific spot, but why on earth is it ignoring the code and snapping to the camera, or GameObject?

avatar image Rick74 · Sep 17, 2013 at 12:50 PM 0
Share

While I'm still struggling with this, I've discovered the problem as to why the object "snapped" to the camera.

In the Lerp statement, I specified the starting position as transform.position. This was wrong. transform.position refers to the GameObject that this script is attached too, which would be the $$anonymous$$ain Camera. And the reason why it wasn't moving is because this whole if statement only happens on click down. So on click, the resource would snap to the camera starting position, and because the click stopped, would not move from that position further.

the correct starting position should be the resource position or "hit.collider.gameObject.transform.position".

Now to figure out how to get the Lerp to complete it's movement outside of the If statement.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ArkaneX · Sep 17, 2013 at 03:24 PM

I see you answered half of the question, related to snapping. As to the second part, I suggest calling a coroutine and executing Vector3.Lerp inside, until it reaches the target. So, instead of this line

 hit.collider.gameObject.transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * resourceSpeed);

which probably looks a bit different (after your fix), use this

 MoveResource(hit.collider.gameObject.transform, targetPosition, resourceSpeed);

and add a coroutine code:

 function MoveResource(resourceTransform:Transform, endPosition:Vector3, speed:float)
 {
     var startPosition = resourceTransform.position;
     var t = 0.0;
     while (t < 1.0)
     {
         t += Time.deltaTime * speed;
         resourceTransform.position = Vector3.Lerp(startPosition, endPosition, t);
         yield;
     }
 }
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 Rick74 · Sep 17, 2013 at 08:03 PM 0
Share

Thank you for taking the time to write up an example, ArkaneX! I more or less ended up doing something very similar to what you have here.

Thanks again for helping me out though, very appreciated!

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

How to translate object one movement with script 1 Answer

Player Transformation 2 Answers

Transform.Translate change distance and speed 1 Answer

Help Trying to translate cs to js problem with one block of code 2 Answers

stop object(door) translation 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