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 willnotfix · Aug 17, 2014 at 08:02 AM · movementgameobjectscripting beginnerontrigger

Move object when player reach trigger

Hello.. I'm noob in scripting. It might be a simple script, but I totally noob :v first here is my 2D game scene alt text

So I want to make my object "Curtain" to move down to the terrain automatically when the "player" reach the "boxtrigger".

So far I add this code to my curtain object and it moved to the targeted terrain but only when every I start the game.

 using UnityEngine;
 using System.Collections;
 
 public class Movement2 : MonoBehaviour {
 
 
 
     public Transform Target;
     public float MoveToSpeed;
     
     private Transform _transform;
     
     private void Start()
     {
         _transform = transform;
     }
     
     private void FixedUpdate()
     {
         _transform.position = Vector3.MoveTowards(_transform.position, Target.position, MoveToSpeed);
     }
 }
 

So how do I make "Boxtrigger" as I trigger to move "Curtain" when my player reach it?

scene.png (3.1 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by dvora · Aug 17, 2014 at 10:30 PM

Try this:

 public var _cutrain : GameObject;
 public var _moveposition : Transform;
 public var speed : float;
 
 function OnTriggerEnter(other : Collider){
 _curtain.transform.position= Vector3.Lerp(_curtain.transform.position,_moveposition.position, Time.deltaTime * speed);

}

Attach this JavaScript to the box collider

Comment
Add comment · Show 2 · 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 willnotfix · Aug 18, 2014 at 05:15 AM 0
Share

Thx for the code.. i will try it

avatar image willnotfix · Aug 19, 2014 at 12:55 AM 0
Share

After I read documentation link from Alessio89 below, I notice that I use 2D box collider in both player and the triggerbox.. so i put :

     public var _cutrain : GameObject;
     public var _moveposition : Transform;
     public var speed : float;
      
     function OnTriggerEnter2D(other : Collider2D){
     _curtain.transform.position= Vector3.Lerp(_curtain.transform.position,_moveposition.position, Time.deltaTime * speed);
 }

and the code works.. thx :)

avatar image
0

Answer by Alessio89 · Aug 17, 2014 at 04:46 PM

You should read about Colliders and how they can function as Triggers. Then what you want to do, is to get the curtain object from the box trigger script. To do this you use GameObject.Find. Once you have the reference for the curtain in your trigger script, you can get the "Movement2" component from the curtain's reference with GetComponent and make the curtain move.

Hope it was clear, I linked to the scripting API so you can see or yourself how these things are achieved. :)

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 willnotfix · Aug 18, 2014 at 05:14 AM 0
Share

Nice advise.. I will observe and try it.. thank u :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why are these object passing through each other? 1 Answer

Moving GameObjects with mouse and check contains 0 Answers

Move object, then destroy it. 1 Answer

scripting for animation 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 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