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 smackledorf · Mar 11, 2021 at 01:22 AM · 3dvector3lerpvector3.lerpvector math

vector3 lerp based on 2 different floats/axes?

I'm trying to move an object around my player in a 2.5d sidescroller game based on analog stick input. The thing is, my player rotates around in the world (with the camera fixed to the side) so it's local horizontal direction (transform.right) is not always the global x direction same for it's local up direction (transform.up). I had this working just fine back when the player's movement direction was fixed on the X axis by setting an offset of the players position and adding the analog on the same axis like so:


 Vector3 endPoint = new Vector3(transform.position.x + inputs.x * floraDistanceMultiplier, transform.position.y + inputs.y * floraDistanceMultiplier, transform.position.z);


but now that the player is "off axis" so to speak, this of course doesn't work.

I am able to get it working one axis at a time:


 Vector3 h = player.transform.position + player.transform.right * floraDistanceMultiplier * inputs.x;
 Vector3 v = player.transform.position + player.transform.up * floraDistanceMultiplier * inputs.y;


But couldn't figure out how to get both vectors influencing the final position. Using localPosition or applying the x and y of the final vector manually didn't work I'm assuming because x and y are always global rather than defining the transform.right as a long some kind of local X axis.

I had the thought to lerp the two - which almost has successful results, in that the end point properly move around the player from left to up only though, not in a full circle around the player, and the diagonal is severely closer than the full extent at up and left.


 Vector3 endPoint = Vector3.Lerp(h, v, inputs.x + inputs.y);


is there a better way to do this overall? Or is there some way to lerp between 2 vectors using 2 floats? (the analog stick x and y) or using a vector? Thanks!

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

Answer by Namey5 · Mar 11, 2021 at 12:02 PM

Let's break down your original working function;

 // This;
 Vector3 endPoint = new Vector3(transform.position.x + inputs.x * floraDistanceMultiplier, transform.position.y + inputs.y * floraDistanceMultiplier, transform.position.z);
 
 // Can be expanded and simplified to this;
 Vector3 endPoint = transform.position + (Vector3.right * inputs.x + Vector3.up * inputs.y) * floraDistanceMultiplier;


As you've already noticed, Vector3.up is in world-space and its player-relative equivalent is player.transform.up .

As such, we can now replace the world-space calls with their local-space equivalents to get the same effect relative to the player;

 Vector3 endPoint = player.transform.position + (player.transform.right * inputs.x + player.transform.up * inputs.y) * floraDistanceMultiplier;
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 smackledorf · Mar 11, 2021 at 05:47 PM 0
Share

Absolutely perfect, thank you! Especially taking the original snippet and showing how to write the equivalent using direction vectors instead of translation really helped. Tried it in game and it works 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.

Follow this Question

Answers Answers and Comments

172 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 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 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 the heck does Mathf.Lerp work? 2 Answers

make my gameobject lerp between points more smoothly 0 Answers

Moving object with vector3 lerp 0 Answers

Vector3.Lerp move weapon up to players face 2 Answers

Vector3.Lerp completing before t = 1? 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