Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 gridGuy · Jul 01, 2019 at 03:15 PM · movementtransform

Can you pass along something like transform.position.x or y? not the value but the VARIABLE?

I want to make a function for movement, that depending on what button is pressed (WASD) that sends passes information to a movement function that then decides which direction the player should move. So if the player presses 'W', then I want to pass along that the transform.position.y value is being affected, whereas if 'D' is pressed, the x value is affected, I'm trying to typecast an Object to hold the information, but I feel like I'm missing something simple.

private void Update() {

     if (Input.GetKey(KeyCode.W)){
         object directionAffected = transform.position.x;
         targetPos = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z);
         StartCoroutine(Movement(targetPos, directionAffected));
         print(targetPos);

} }

 IEnumerator Movement (Vector3 moveTo, object directionAffect)
 {
     print(directionAffect);
     Vector2 theDir;
     theDir = (Vector2)directionAffect;
     while (Vector2.Distance((theDir.x - moveTo.x)) > 1)
     transform.position = Vector3.Lerp(transform.position, moveTo, smoothing * Time.deltaTime);
     yield return null;
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ChrisD0 · Jul 01, 2019 at 06:14 PM

I don't know C# that well so I really don't know if this is possible, but I would have to guess it's not. I have to ask though, why are you even doing this? It looks like you're just using directionAffected to initialise theDir, when you could just pass it as an argument to your coroutine.

Comment
Add comment · 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
0

Answer by Optrix · Jul 02, 2019 at 09:10 AM

That's an unusual request. It MIGHT be possible if you pass a property as a ref (check the ref keyword in the C# documentation), but it's still not an attractive solution.

Most importantly, it also won't work on many things in Unity, because they don't allow you to directly write to a single component - you need to set the entire Vector3 or Quaternion.


Why is this running as a Coroutine? You need to include the 'yield' inside the while loop, or this won't do a thing. And if you're not modifying 'Smoothing', you'll never finish the animation.


If you want to restrict the effect to one axis, you could do something like this (note that this is 'top of my head' code and might not work...)


 IEnumerator Movement (Vector3 moveTo, Transform T, Vector3 restrictAxes, float TimeToDestination = 1)
  {
      float TargetTime = Time.time + TimeToDestination;
      Vector3 InvertAxes = new Vector3(1 - restrictAxes.x,1- restrictAxes.y,1-restrictAxes.z);
      Vector3 TargetLocation = new Vector3(0,0,0);
      Vector3 Origin = Transform.position;
      while (true)
      {
          TargetLocation = Vector3.Scale(InvertAxes,T.position) + Vector3.Scale(restrictAxes,moveTo);
          Original = Vector3.Scale(InvertAxes,Origin) + Vector3.Scale(restrictAxes,moveTo);
          transform.position = Vector3.Lerp(Origin, TargetLocation, Mathf.SmoothStep(0,1,(TargetTime - Time.time) / TimeToDestination));
          yield return null;
      }
  }



The idea of the above function is that you pass the location you want to move to, the transform you want to move, the vector you want to restrict the movement to, and finally the length of the animation.

So if you wanted to move "BigObject" from X=1 to X=5 over 2.5 seconds, but you'd like it to move freely on both the Y and Z axes, you'd call...

 StartCoroutine(Movement(new Vector3(5,0,0),BigObject,new Vector3(1,0,0),2.5)

I'm using 'Math.SmoothStep' to provide smoothing - this makes the animation ease in and out. You don't need this - I just wasn't sure what your 'smoothing' variable did.

There's not a lot of use-cases for this though.

Comment
Add comment · 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

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

185 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 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

Modifying the Transform of a GameObject 1 Answer

My rigged mesh does not react in Unity 0 Answers

How to calc relative transform position & rotation & scale? 0 Answers

Make OnMouseButtonDown do whole task and not just when pressed 1 Answer

How to make object follow touch position?Pls Help 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