• 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
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

People who like this

0 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

  • Sort: 
avatar image

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

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

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

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

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

Push the player forward C# 0 Answers

TriggerExit not working as intended/Transforming GameObject Problems 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