
UNITY TRANSFORM CODE
I have enabled IK pass on my base layer, and have placed my code in the "idle run jump" script, attached to the top level of the game object for the playable character.

I have the Mecanim Example project from the asset store and I'm in the animator controller scene. I have the mecanim example and unity 5.0.1f1. Ok so to expand on the question, I'll explain exactly what I'm trying to do. I have also tried: Debug.Log() īut still, all I get is the position of the top level parent. My code to read the position is as follows: Debug.Log(object.position) All of the objects (Level1 through Level4) are not explicitly moved, however they inherit their positions from the rotation of their parent. The value returned to me is the position of the prefab placed in the scene. The object for which I am attempting to find the position is nested inside the skeleton of the character. I do not believe this may be relevant, but what I am doing is an IK rig to move a character's limb. When I try to find the transform.position of Level4 by way of Debug.Log to the console, I get the position of Level1, and so when I change the position of Level4 inside Level3, the data sent to the console doesn't change, still shows the position of Level1. My script is attached to the Level1 object. You can also swap the input back to GetKeyDown if you like, the only reason I used it is so you get arrow keys as well as wasd input.So in my scene hierarchy, I have an object laid out like so The answer is quite generic so it can be tweaked to what you need. set the coroutine to null so we can jump againĪs I was not exactly sure how your implementation was done, I had a few assumptions about lanes and other general setups. Transform.position = new Vector3(goalXPos,, ) set our position directly in case of floating point errors Transform.position = new Vector3(Mathf.Lerp(currentXPos, goalXPos, jumpTimer / TimeForJump),, ) continue the lerp for the time we have left timer for how long it has passed since we started the jump Private IEnumerator Jump(float direction)įloat currentXPos = įloat goalXPos = currentXPos + (direction * JumpOffset) PlayerJumping = StartCoroutine(Jump(-1)) it is also an easy way to determine direction when adding or subtracting for a goal locationįloat horzInput = Input.GetAxis("Horizontal") instead of 'a' and 'd' you can use GetAxis to allow for arrow key and wasd support no need to detect input if we are already jumping I am assuming your setup for lanes is -1 | 0 | 1 I will also be using time not speed to control the jump of the Lerp, you can easily change it to speed if you like. I will keep the input detection inside of Update, and will allow the player to jump from side to side from the middle, but not off the track nor jump while jumping already. I am not entirely sure of your lane setup but will assume you have 3 lanes similar to the other infinite run track games. Coroutines are very powerful but please read into the documentation before overusing them to get a firm understanding of what they do. Think of Courtines as a process that is completed over multiple frames, jumping back to where it left off in the previous frame. Instead of placing your logic inside of Update, I would consider moving the actual movement to a special function called a Coroutine. Lerps are meant to be completed over time, not immediately. One other issue is you are using GetKeyDown, which only occurs the one frame the user clicks the key.

It will immediately set your current position to your new position, then break out of your while loop. You are subtracting a value of 2.5 from your current then continually checking if your current position x is greater than the new, which is always true. The reason it is immediately moving to the next point is due to your if conditional.
UNITY TRANSFORM UPDATE
You are using a while loop instead of Update meaning the program will stick inside of that loop until the process has finished.


However, the implementation and use case does not quite fit what you are attempting to do.Īs you are only changing a single axis of the movement, I would not use Vector3.Lerp, instead use Mathf.Lerp as you are only changing the x-axis of your position. You are right in thinking to use a Vector3.Lerp to gradually interpolate between a start and end value.
