• 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 MachCUBED · Apr 03, 2012 at 07:46 PM · collisionrigidbodypickupcollisiondetection

In-game pickup not moving towards player after being collected

Hi guys,

I have some pickups in my game, w$$anonymous$$ch are supposed to move towards the player after being picked up. Here is GemPickup.js:

#pragma strict

var scoreValue : int = 1;

private var target : GameObject;

private var pickedUp = false;

function Start () { target = GameObject.FindGameObjectWithTag("Player"); }

function OnTriggerEnter (other : Collider) { // Make it go towards the penguin and disappear pickedUp = true; Debug.Log("Gem collected!"); }

function Update () {

 if (pickedUp)
 {
     // Move the gem towards the player
     transform.position = transform.position - target.rigidbody.position * Time.deltaTime;
     Debug.Log("Moving gem to player...");
     //Remove from game environment after being collected
     //TODO: Add scoring system
     if (transform.position == target.rigidbody.position)
     {
         Destroy(t$$anonymous$$s);
         Debug.Log("Add score");
         //TODO: take scoreValue and add it to the player's score
     }
 }

}

The OnTriggerEnter() code works just fine, since the the debug log message gets printed. However, although the fact of getting picked up is being detected in Update(), the gems just never seem to meet the player, preferring to move in bizzare directions instead. What gives?

MachCUBED

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
0

Answer by JojoJonas · Apr 03, 2012 at 11:12 PM

It's t$$anonymous$$s line if I'm not mistaken:

 transform.position = transform.position - target.rigidbody.position

You shouldn't be setting transform.position to the difference here; you should use Translate().

For example, if your player was at 10, 0, 10, and your object was at 15, 0, 15, then your computed value (that you're plugging into transform.position) would be 5, 0, 5. That's why your object is bouncing around everywhere.

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 MachCUBED · Apr 06, 2012 at 12:02 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can we do this in Unity 3d? 1 Answer

How do I make OnTriggerEnter() respond to just one collider? 2 Answers

Need help with scripting bug fix. pleease 0 Answers

My Rigid body is passing through my static mesh (non rigid) 1 Answer

Detecting if near the edge of an object ? 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