• 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 mostlyhuman · Jul 03, 2017 at 03:17 AM · offset

How to do a real time changeable offset?

How can I add an offset to an object so that it can be adjusted while the game is running by changing the offset values in the inspector?

I tried this: object.transform.position = object.transform.position + offsetVector3;

but its causing the object to animate and fly off the screen presumably because its being called every frame in Update, so how can I have it update in real time but also stay put?

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
Best Answer

Answer by BaronScreenKiller · Jul 03, 2017 at 04:11 AM

So the issue is that your adding the offset to the position every frame like you said. Picture this in 1 dimension.

frame 1. position = 0, offset = 1 so new position = 1 frame 2. position = 1, offset = 1 so new position = 2 frame 3. position = 2, offset = 1 so new position = 3

It will keep adding the offset every frame.

I would fix it be storing the original position at the start and adding the offset to that. frame 1. original position = position position = original position + offset frame 2. position = original position + offset frame 2. position = original position + offset

that way it's stable.

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 mostlyhuman · Jul 03, 2017 at 04:22 AM 0
Share

Okay gotchya, here are a couple things, let me explain a little more in detail. Its a script for a character to pickup an item when they walk over it. The item isnt known until the collision happens, once the collision with the proper object happens it parents that object to the character. Its a cartoony game so im just having the item float around the character while it walks and wanted to use the offset so i could have each item have their own offset as to where the item will float around the character (above, below, in front etc).

What I tried from your suggestion was adding in the oncollisionenter that originalPos = currentItemPos, i put this after it was parented, but when i ran the code this just made the item stand there and wobble independently of the character and not follow the character. heres what I have so far and thank you so much for your help i sincerely appreciate it!

 public class Inventory : $$anonymous$$onoBehaviour {
 
     GameObject currentItem = null;
     IsPickupable isPickupable;
     Vector3 originalItemPos;
 
     void Update()
     {
         if (currentItem != null)
         {
             currentItem.transform.position = originalItemPos + isPickupable.GetCarryItemOffset();
             //currentItem.transform.rotation = Quaternion.Euler(isPickupable.GetCarryRotation());
         }
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.GetComponent("IsPickupable") != null && currentItem == null)
         {
             currentItem = collision.gameObject;
             currentItem.gameObject.GetComponent<Rigidbody>().is$$anonymous$$inematic = true;
             currentItem.gameObject.GetComponent<Collider>().enabled = false;
             isPickupable = currentItem.gameObject.GetComponent<IsPickupable>();
             currentItem.gameObject.transform.parent = transform.Find("$$anonymous$$aster/Character1_Reference/Character1_Hips/Character1_Spine/Character1_Spine1/Character1_Spine2/");
             originalItemPos = currentItem.transform.position;
         }
     }
 }
 
avatar image BaronScreenKiller mostlyhuman · Jul 03, 2017 at 04:29 AM 1
Share

right, so the point that you want to offset from (the player position) changes every frame right? so could you do something like:

Object Position = Player Position + offset

The extra Original Position variable is just to stop that feedback loop. if you can dynamically reference the position you want to offset from thats better.

The other option is to use the local position (transform.localPosition) which is the position relative to the parent.

transform.localPosition = offset;

is the same as

transform.position = transform.parent.position + offset;

avatar image mostlyhuman BaronScreenKiller · Jul 03, 2017 at 04:31 AM 0
Share

Thank you so much! i feel silly overlooking something so simple :) Object Position = Player Position + offset worked perfectly.

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

66 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I offset the scaling center for a cube 1 Answer

gun sway, gun swing, gun offset 0 Answers

Look to the side-Scripting Problem 2 Answers

SetIKPosition offset 1 Answer

How to get Terrain to respect shader offset 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