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 ibrahimmunaser · Nov 29, 2015 at 09:56 AM · c#unity 5movementtransformobject

Move Object to location of Trigger?

Hi, I want to move my object to a trigger.

I want if I click on the trigger, my object will smoothly go to the trigger (NOT INSTANTLY), right now if I click my trigger the object will move slowly and then stop, and wait for me to click again so it can move. I dont want that. I want to click once on the trigger, and the object move smoothly all the way to the location of the trigger.

heres my code:

using UnityEngine; using System.Collections;

public class GreenEnvelope : MonoBehaviour { public Transform target; public float speed; public bool isMove;

 void Start()
 {

     isMove = false;
 }

 void Update() 
 {
         
     if (Input.GetMouseButton (0)) {
         SetTargetPosition();
     }
 }

 void SetTargetPosition()
 {
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if (Physics.Raycast(ray, out hit))
     {
         if(Input.GetMouseButtonDown(0) && hit.collider.name == "GreenTrigger")
         {
         float step = speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards (transform.position, target.position, step);
         isMove=true;
     }
 }

}

}

Comment
Add comment · Show 2
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 savlon · Nov 29, 2015 at 02:54 PM 0
Share

The SetTargetPosition method is only being called when you are holding the mouse button down. You will need to change your logic for it to work. Then in that method, you check again for the mouse button down. Again, it will not continue to execute unless your mouse button is down.

avatar image kalen_08 · Jul 20, 2018 at 09:26 PM 0
Share

give me a sec i'm on it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kalen_08 · Jul 20, 2018 at 10:00 PM

The best way to do this kind of thing is through a CoRoutine (IEnumerator) So basically the way it works is until its a certain distance from the target (which is a very small amount) then it will move towards it and when it reaches there it will stop and set the target to nothing. If say you were to click on a different target... It stops call coroutines and then starts the coroutine again. Ensuring that there aren't two scripts fighting to do the same thing.

     [SerializeField] float speed = 1;
     // generally identifying things by their name sucks!!
     // if you change the name of the object then it no longer works.
     // i think it would be best if you have all of these triggers on a Layer
     // and if you click on something of that layer then what you click becomes
     // becomes the target... but anyways...
     [SerializeField] string triggerName = "Trigger";
 
     GameObject target;
     float stoppingDistance = 0.001f;
 
 
     void Update() 
     {
         if (Input.GetMouseButtonDown (0))
         {
             SetTarget();
         }
 
         MoveToTargetPosition();
     }
 
     void SetTarget()
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit))
         {
             if(hit.collider.name == triggerName)
             {
                 target = hit.collider.gameObject;
 
                 // stop any existing coroutine that is running
                 // this way there won't be two running at the same time.
                 StopAllCoroutines();
                 StartCoroutine(MoveToTargetPosition());
             }
         }
     }
 
 
     IEnumerator MoveToTargetPosition ()
     {
         //while the target exists...
         while (target != null)
         {
             transform.position = Vector3.MoveTowards (
                 transform.position,
                 target.transform.position,
                 speed * Time.deltaTime
             );
 
             // wait a frame
             yield return new WaitForEndOfFrame();
 
             // calculate the distance between the objects
             // if its close enough then stop the process.
             float distanceToTarget = Mathf.Abs(Vector3.Distance(transform.position, target.transform.position));
             if (distanceToTarget <= stoppingDistance) {
                 target = null;
                 yield break;
             }
         }
     }


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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Moving a Game Object in a specific volume/area 1 Answer

How can I modify this rotation code to change how far the object rotates? 0 Answers

How do I move the camera to another object in the scene on mouse down? 1 Answer

Modifying the Transform of a GameObject 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