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 Confused · Apr 05, 2011 at 11:13 PM · gameobjecttriggermove

Trigger moves another object

What I basically want to do is move an object when I collide with a trigger. In my scene I have a trigger that when it is collided with, you will see an object in the distance move diagonally upwards.

Here is what I have so far:


var box : GameObject; var Speed : float;

function Update () {

 var translation : float = Time.deltaTime * Speed;
 box.transform.Translate (-translation, translation, 0);

}

function OnTriggerEnter (other : Collider) {

 Debug.Log("trigger");

}


My only issue is that I can't move the box upwards upon collision with the trigger if box.transform.Translate isn't in function Update() along with the line of code above it.

and I can't stick OnTriggerEnter in function Update.

I want to be able to place this trigger separately from this moving object, but it's proving to be trickier than I originally thought..

Comment
Add comment · Show 1
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 Justin Warner · Apr 05, 2011 at 11:21 PM 0
Share

When posting code next time, please highlight it, and push the 10101 button up top =). Thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Ben Holmes · Apr 06, 2011 at 12:03 AM

looks like you need two seperate objects in the scene. I would create two seperate scripts for this kind of behaviour. One which is attached to the trigger and uses the OnTriggerEnter method, and another which is attached to the box and uses the Update function.

The trigger script should get a reference to the other script in it's Start() function (there are a number of ways to do this). and when the trigger is entered you can simply set a bool on the box script. In the box script's Update() function you only want to translate if the bool member is true. Here's an example:

class BoxScript : MonoBehaviour { public bool moving = false;

void Update() { if(moving) { var translation : float = Time.deltaTime * Speed; box.transform.Translate (-translation, translation, 0); } } }

class TriggerScript : MonoBehaviour { BoxScript otherScript; void Start() { BoxScript = GameObject.Find("Box").GetComponent<BoxScript>(); }

void OnTriggerEnter() { otherScript.moving = true; } }

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 Confused · Apr 06, 2011 at 04:59 PM 0
Share

the scripts aren't working for some reason.. Originally I was trying to do all this with one script, but I might just go with two to make it simpler.

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

No one has followed this question yet.

Related Questions

Object moving OnTriggerEnter, please help?! 2 Answers

Moving object with a trigger? 0 Answers

How would I go about moving a group of objects as one using iTween? 1 Answer

Moving GameObject to various position ? 1 Answer

Use Vector Lerp or other to move back with arc 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