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 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, which 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(this);
         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 this 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

I tried changing the following:

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

to:

transform.Translate(transform.position - target.rigidbody.position * Time.deltaTime);

Not only does it still act funny (in terms of going all over), but now it eventually spits out the following console error:

transform.position assign attempt for 'Emerald' is not valid. Input position is { -Infinity, -787959360953950142464.000000, 1426979897156153901056.000000 }. UnityEngine.Transform:Translate(Vector3) GemPickup:Update() (at Assets/Props/Scripts/GemPickup.js:24)

Why is it passing -infinity as a value?

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Strange OnCollisionEnter behavior, returning wrong collision object? 0 Answers

Disabling Kinematic on hit 2 Answers

Checking Collision on instantiated object 0 Answers

Ragdollize kinematic rigidbodies 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