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 /
This question was closed Jan 18, 2018 at 09:50 PM by AZIIZoom for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by AZIIZoom · Jan 16, 2018 at 07:20 PM · gameobjecttriggermoveenterslowly

Moving GameObject slowly OnTriggerEnter

Hello, I’m new in this forum and also in Unity,

and I hope that one of you will help me. I Created a script that I use when an object enters in specific zone (collider), an other object move from its position to another (it depends on the axis) following a certain distance.

The script works well, but I want that the movement occurs slowly. I tried many things that I found in internet but nothing works.

Hope you can help me.

This is my script :

using UnityEngine; using System.Collections;

public class MoveUpObject : MonoBehaviour {

   private Vector3 startPos;
   public float distance = 1.0f;
   public float speed = 1.0f;
   public enum MoveAxe {AxeX, AxeY, AxeZ};
   public MoveAxe moveAxe;
   public GameObject targetGameObject = null;

   void Start () {
         startPos = targetGameObject.transform.position;
   }

 

   void OnTriggerEnter (Collider other) {
         Vector3 endPos = startPos;
         if (moveAxe == MoveAxe.AxeX) {
         endPos.x += distance;
         } else if (moveAxe == MoveAxe.AxeZ) {
         endPos.z += distance;
         } else if (moveAxe == MoveAxe.AxeY) {
         endPos.y += distance;
         }

         float step = speed * Time.deltaTime;
         targetGameObject.transform.position = Vector3.Lerp (endPos, startPos, step);

   }

   void OnTriggerExit (Collider other) {
         targetGameObject.transform.position = startPos ;
   }

   void Update () {

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

  • Sort: 
avatar image
0
Best Answer

Answer by AZIIZoom · Jan 18, 2018 at 09:49 PM

I Finally found a solution, I used another approach,

In the Start () function, I initialized the start position StartPos and the end position EndPos. On the OnTriggerEnter () and OnTriggerExit functions, I just initialized the displacement computation time and added a bool variable that indicates if there is an A object or not in the detection zone. Then, I put the formula that moves the object into the Update () function; and as the case may be: -if object A enters the detection zone, object B moves from its StartPos position to the EndPos position -if object A leaves the detection zone, object B moves from its EndPos position to the StartPos position

public class MoveObject : MonoBehaviour {

 private Vector3 startPos;
 private Vector3 endPos;
 public float distance = 1.0f;
 public float lerpTime = 1.0f;
 private float currentLerpTime = 0;
 public enum MoveAxe {AxeX, AxeY, AxeZ};
 public MoveAxe moveAxe;
 public GameObject targetGameObject = null;
 private bool objectIn;



 void Start () {
     startPos = targetGameObject.transform.position;
     endPos = targetGameObject.transform.position;
     if (moveAxe == MoveAxe.AxeX) {
     endPos.x += distance;
     } else if (moveAxe == MoveAxe.AxeZ) {
     endPos.z += distance;
     } else if (moveAxe == MoveAxe.AxeY) {
     endPos.y += distance;
     }
 }



 void OnTriggerEnter (Collider other) {
     objectIn = true;
     currentLerpTime = 0;
 }



 void OnTriggerExit (Collider other) {
     objectIn = false;
     currentLerpTime = 0;
 }



 void Update () {
     if (objectIn == true) {
     currentLerpTime += Time.deltaTime;
     if (currentLerpTime >= lerpTime) {
     currentLerpTime = lerpTime;
     }



     float perc = currentLerpTime/lerpTime;
     targetGameObject.transform.position = Vector3.Lerp (startPos, endPos, perc);
     } else 
     if (objectIn == false) {
         currentLerpTime += Time.deltaTime;
     if (currentLerpTime >= lerpTime) {
         currentLerpTime = lerpTime;
     }


     float perc = currentLerpTime/lerpTime;
     targetGameObject.transform.position = Vector3.Lerp (endPos, startPos, perc);
     }
 }

}

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

Follow this Question

Answers Answers and Comments

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

Gameobject doesnt move after build 0 Answers

What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 0 Answers

how to test if something enters a specific trigger and set that object to a gameobject variable!?!? 1 Answer

OnTriggerEnter 2 Answers

Two colliders that trigger different things in the same object-hierarchy 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