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 Hamesh81 · Feb 06, 2015 at 11:03 AM · positionlerpaxislocal

How to Lerp a position along one axis only?

I am trying to lerp the local z axis position of a character to the local z axis position of a trigger being entered. I am using Mathf.Lerp but it is triggering an error:

 void OnTriggerStay(Collider other) {
     other.transform.localPosition.z = Mathf.Lerp(other.transform.localPosition.z, transform.localPosition.z, Time.deltaTime * 1);
 }

How could I make this work, or is there a better way?

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 J_LS · Feb 06, 2015 at 04:12 PM 0
Share

I have had similar problems, and it is always related to parent/child hierarchy. Try to create a gameobjet, place it on the character pivot, and then make the character child of this object. Create an other gameobject, place it on top of the box pivot and then make the box child of this object.

Now try your script with the new "parents" ins$$anonymous$$d of the character and the box, if it works correctly, you have something wrong with your character/box pivot points. Even if it works, I suggest finding the pivot issue and fixing it.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Dhiru · Feb 06, 2015 at 11:28 AM

you can't set Z value directly. You have to use its positions.

     void OnTriggerStay(Collider other) {
         other.transform.localPosition = new Vector3(other.transform.localPosition.x, other.transform.localPosition.y, Mathf.Lerp(other.transform.localPosition.z, transform.localPosition.z, Time.deltaTime * 1));
 
     }
 


Comment
Add comment · Show 2 · 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 Hamesh81 · Feb 06, 2015 at 11:56 AM 1
Share

Ok this also works, but for some reason the character is not being lerped to the center of the trigger box. The triggers pivot is in the center of the box and the pivot of the character is also at its own center. Yet when entering the trigger from the back the character lerps through the box all the way to the front of it. I would like it to lerp to the center of the trigger (center according to the z axis only).

avatar image Hamesh81 · Feb 06, 2015 at 02:43 PM 1
Share

Never $$anonymous$$d I worked it out. I simply changed localposition to position.

avatar image
1

Answer by instruct9r · Feb 06, 2015 at 11:22 AM

It gives error, because on C# you can't access only one axis.

Create a temrorary variable and lerp it, then assign it to the local position.

 void OnTriggerStay(Collider other) 
 {
      Vector3 newPosition = other.transform.localPosition;
      newPosition.z = Mathf.Lerp(other.transform.localPosition.z, transform.localPosition.z, Time.deltaTime * 1);
 
      other.transform.localPosition = newPosition;
  }

Haven't tested it, but i'm pretty much creating new Vector3 and assigning it to the local position (So X and Y) can get from other object. Then i Lerp the Z. Lastly i apply the new vector to the other's local position

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 Hamesh81 · Feb 06, 2015 at 11:56 AM 1
Share

Ok this works, but for some reason the character is not being lerped to the center of the trigger box. The triggers pivot is in the center of the box and the pivot of the character is also at its own center. Yet when entering the trigger from the back the character lerps through the box all the way to the front of it. I would like it to lerp to the center of the trigger (center according to the z axis only).

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

23 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

Related Questions

Move object position on specific axis with Lerp 0 Answers

Move object to position on another object's locally rotated axis 1 Answer

Local Lerp to origin 1 Answer

Move object while rotating another one 1 Answer

Add specific movement on double click 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