- Home /
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;
}
}
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.
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.