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 TheSaviour · Apr 26, 2015 at 01:53 AM · c#gameobjectcollidertransform.translate

Why is transform.Translate not working?

So I have written this code:

 void OnTriggerEnter (Collider other)
 {
     if(other.gameObject.CompareTag ("PickUp"))
     {
         Destroy(other.gameObject);
         Vector3 roll = new Vector3(500.0f,0.0f,0.0f);
         object2.transform.Translate(roll * Time.deltaTime);
     }
 }

When the player collides with the pick-up object, the pick-up object get destroyed and another gameObject (which I named object2) rolls to a new position. However, object2 is not changing its position the way I want it to. It just shifts to its new position. It does not roll to the new position. How to I make object2 (which is a sphere by the way) roll to its new position?

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

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Apr 26, 2015 at 02:44 AM

OnTriggerEnter only runs once, as the name strongly implies (that is, when the trigger is entered). Since Time.deltaTime is likely to be a small number, Translate is working as it always does, but it won't move very far. If you want the object to move continuously over time, you'll have to program it to do that. Coroutines are a convenient way (and OnTriggerEnter can be a coroutine).

Comment
Add comment · Show 3 · 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 kburkhart84 · Apr 26, 2015 at 03:32 AM 0
Share

Just to add some detail here, if you intend on actual rolling of the sphere, not just movement, you need to put that in as well. You would need to do a rotate as well as the translate.

Another thing to consider, if you think it would fit your game, you could convert it to use the physics engine ins$$anonymous$$d of directly manipulating the transforms yourself. This does add overhead and complicates things in some ways, but if you need physics, it works great, and can make other parts of the game work right too.

avatar image TheSaviour · Apr 26, 2015 at 08:45 AM 0
Share

Ok so I tried adding the script to object2 this time. Here's what I wrote:

 if (GameObject.Find("Cube") == null)
         {
             Vector3 shift = new Vector3 (500.0f,0.0f,0.0f);
             rigidbody.transform.Translate (shift * Time.deltaTime);
         }

This almost solved my problem. object2 does roll this time but it just rolls off the plane and just keeps on rolling on empty space. It does not stop at a new position. I tried lowering the x value on Vector3 but that just slows the rolling speed. How do I fix this?

avatar image kburkhart84 · Apr 26, 2015 at 07:28 PM 0
Share

You have to make it stop at the new position. You will need to program it in somehow. $$anonymous$$aybe you can say if the distance to the new position you desire is a really small amount, then directly place it there, and stop the rolling.

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

Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers

Multiple Cars not working 1 Answer

Search GameObjects while in game 3 Answers

Distribute terrain in zones 3 Answers

I need help with triggers 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