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 trs9556 · May 27, 2013 at 10:28 PM · vector3lerplocalslerpglobal

How can I Vector3 Slerp or Lerp locally?

Hello, I'm using:

 if(forwardMoveSpeed > 0){
     this.transform.localPosition = Vector3.Lerp(this.transform.position , new Vector3(this.transform.position.x  + 1f,this.transform.position.y, this.transform.position.z), forwardMoveSpeed * Time.deltaTime);
     forwardMoveSpeed -= 0.05f;
 }

to move my character for a small amount of time. But this moves it in the global x position. As soon as my character rotates it doesn't look right, obviously.

How can I move my character in it's local x position.

Thanks.

I did some searches and found a bunch on how to rotate something locally/globally but nothing about just moving using Vector3.

EDIT:

So let me explain a tad bit more so you can get a better understanding.

I have a character(GameObject). He runs a path and has a specific amount of life. When he dies I want him to fall "forward" while doing his dying animation.

He faces the Z axis, therefore "forward" is his LOCAL Z axis. The path he travels has 90 degree turns. So he faces different directions relative to the global axis. Sometimes his LOCAL z axis is facing the GLOBAL x axis, sometimes the GLOBAL y Axis.

With that being said all I want to do is move his overall position's LOCAL z axis by like 2 units while he's doing his animation. (so if his position is 0,0,0 then when he dies I want his LOCAL position to be 0,0,2. I can't just change his position because his LOCAL Z axis might be the GLOBALS X axis).

How do I do that?

Eveything I try moves him 2 units in a GLOBAL axis.

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

3 Replies

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

Answer by trs9556 · May 28, 2013 at 12:14 AM

Ok so I couldn't find a solution so I just made my own personal converter. This code is pretty specific to my model but if your "forward" is your local Z axis then it might work for you.

Thanks to everyone who tried to help, maybe I didn't explain it good enough.

             if(forwardMoveSpeed > 0){
                 /*@@@@LEGEND
                  *In a perfect world, if all rotations were EXACT then this is what way the character is facing 
                  * 
                  * 
                  * If it's y rotation is 90 degrees: He is facing X direction and forward is +x
                  * 
                  * If it's y rotation is 0 degrees: He is facing Z direction and forward is +Z
                  * 
                  * If it's y rotation is 180 degrees: He is facing Z direction and forward is -z
                  * 
                  * If it's y rotation is 270 degrees: He is facing X direciont and forward is -x
                  * 
                  */
                 
                 
                 currentPosition = this.transform.position;            
                 float moveAmount = 2f;
                 
     
                 if(oneShot){
                     float currentRotitation = this.transform.rotation.eulerAngles.y;
                     //ALL VALUES ARE A RANGE/ABOUT BECAUSE WE CAN'T GAURENTEE THE CHARACTER'S ROTATION WILL BE EXACTLY 0,90,180, OR 270
                     //if rotoation is greater then 20 then it CANT be "0" rotation
                     if(currentRotitation > 20){
                         //if rotation is greater then 110 then it CANT be "90" rotation
                         if(currentRotitation > 110){
                             //if rotation is greater then 200 then it CANT be "180" rotation
                             if(currentRotitation > 200){
                                 //if we are greater then 200 we have to be at "270" degrees
                                 //forward is negative x
                                 nextPosition = new Vector3(currentPosition.x - moveAmount, currentPosition.y, currentPosition.z);    
                             }
                             //if it's less then 200 then it has to be "180" degrees
                             else{
                                 //forward is negative z
                                 nextPosition = new Vector3(currentPosition.x, currentPosition.y, currentPosition.z - moveAmount);    
                             }
                         }
                         //if it's less then 110 it has to be "90" rotation
                         else{
                             //forward is positive x
                             nextPosition = new Vector3(currentPosition.x + moveAmount, currentPosition.y, currentPosition.z);
                         }
                         
                         
                     }
                     //if it's less then 20 it must be "0" rotation
                     else{
                         //forward is positive Z
                         nextPosition = new Vector3(currentPosition.x, currentPosition.y, currentPosition.z + moveAmount);
                     }
                     //this is so we only figure out our "end" location once.
                     oneShot = false;    
                 }
                     
             
                 
                 this.transform.position = Vector3.Slerp(currentPosition , nextPosition, forwardMoveSpeed * Time.deltaTime);
                 forwardMoveSpeed -= 0.05f;
             }
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 robertbu · May 28, 2013 at 12:29 AM 0
Share

If you are moving in the forward direction of your object, and if you are facing the direction you want to go, then you can boil your code down to this:

 if(forward$$anonymous$$oveSpeed > 0){
     currentPosition = this.transform.position;       
     float moveAmount = 2f;
     
     if(oneShot){
         nextPosition = transform.forward * moveAmount;
         oneShot = false;   
     }
     
     this.transform.position = Vector3.Lerp(currentPosition , nextPosition, forward$$anonymous$$oveSpeed * Time.deltaTime);
     forward$$anonymous$$oveSpeed -= 0.05f;
 }

You want Lerp, not Slerp. Slerp is for spherical movement of vectors.

avatar image robertbu · May 28, 2013 at 12:33 AM 0
Share

Actually you can get rid of your forward$$anonymous$$oveSpeed and use a constant speed (and still get an eased movement):

 if(transform.position != nextPosition){
     currentPosition = this.transform.position;       
     float moveAmount = 2f;
 
     if(oneShot){
        nextPosition = transform.forward * moveAmount;
        oneShot = false;   
     }
 
     this.transform.position = Vector3.Lerp(transform.position , nextPosition, speed * Time.deltaTime);
   
 }
avatar image
1

Answer by hiddenspring81 · May 27, 2013 at 11:11 PM

if you want to move the character in local-space, shouldn't you be using Lerp on the transform.localposition and not transform.position? Try this instead

 if(forwardMoveSpeed > 0){
     this.transform.localPosition = Vector3.Lerp(this.transform.localposition , new Vector3(this.transform.localposition.x  + 1f,this.transform.localposition.y, this.transform.localposition.z), forwardMoveSpeed * Time.deltaTime);
     forwardMoveSpeed -= 0.05f;
 }
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 trs9556 · May 27, 2013 at 11:23 PM 0
Share

I tried something similar while waiting for a response, and it did nothing. So I went to the docks and it makes sense why it doesn't work.

"Position of the transform relative to the parent transform." $$anonymous$$y script is on the parent so it just returns the same as transform.Position. Thanks though.

avatar image mentalgear · May 31, 2020 at 03:54 PM 0
Share

This worked for me, thanks !

avatar image
0

Answer by robertbu · May 27, 2013 at 11:16 PM

I'm not sure exactly what you to happen here. Typically you would have something like this:

 var v3LocalDest : Vector3;
 var speed = 2.0;
 
 function Start() {
   v3LocalDest = transform.localPosition;
   v3LocalDest.x += 1.0;
 }
 
 function Update() {
     transform.localPosition = Vector3.Lerp(transform.localPosition, v3LocalDest, Time.deltaTime * speed);
 }

Note if you have a global position and want to set v3LocalDest, you can do:

 var v3LocalDest = transform.InverseTransformPoint(v3GlobalPoint);
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 trs9556 · May 27, 2013 at 11:33 PM 0
Share

I've tried a few different ways using InverseTransformPoint and using TransformPoint, it all gives me weird cords. Same with TransformDirection

avatar image robertbu · May 27, 2013 at 11:38 PM 0
Share

Based on your comment above, if this is on a parent object, replace all 'localPosition' with 'position' in this sample code.

avatar image trs9556 · May 27, 2013 at 11:47 PM 0
Share

Yes you are correct, I can use both localPosition or position sense they will return the same thing.

With that being said it does exactly what I already have accomplished. $$anonymous$$oves the GameObject in the global position. I'm looking for local, thanks though.

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

15 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

Related Questions

How to use Vector3.Lerp without slow-down 3 Answers

Moving the player toward a direction 1 Answer

How to Gradually Increase Speed with Lerp or Slerp? 1 Answer

[QuaternionSlerp] Lookback script 1 Answer

Vector3.lerp doesn't work when level is loaded 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