llkaindy.blogg.se

Top down engine unity
Top down engine unity








top down engine unity

Object.AddComponent().First, attach a Rigidbody2D component to your 2D character, and turn the gravity scale down to 0. Note that this code can be attached to an object either through New Object > 2D > Sprite and then by adding this code as a script, or through the following code programmatically: GameObject object = new GameObject() OriginalY + ((float)Math.Sin(Time.time) * floatStrength), change the range of y positions that are possible. Public float floatStrength = 1 // You can change this in the Unity Editor to Public class FloatBehavior : MonoBehaviour In the end the complete code is as follows: using UnityEngine That code segment does not implement the other changes that I mentioned. OriginalY + (Math.Sinf(Time.time) * floatStrength), ) Finally, we must fix that same problem in the Update() function, and change: transform.position = new Vector2(, Note that this would need to actually be a Vector3 to store transform.position, as that also has a z component, if we were to actually have kept using that. Next, we can remove the Vector2 floatY completely. Math.Sin (Note: not Math.Sinf) yields a double instead of the desired float, so we have to explicitly convert that value. OriginalY + (Math.Sinf(Time.time) * floatStrength)) įinally, the last step is to do some minor refactoring as to remove some errors that have popped up along the way. Alternatively, you could use transform.position directly, doing something like this: Vector2 floatY The final code should look something like this: Vector2 floatY įloatY.y = originalY + (Mathf.Sin(Time.time) * floatStrength) įinally, instead of storing floatY as a Vector, you could store it as a float.

top down engine unity

This.originalY = įloatY.y = (Mathf.Sin(Time.time) * floatStrength) Īfter that, I suggest that instead of directly setting the y value of the floatY to Mathf.Sin(Time.time) * floatStrength, you set it based on the sum of that value and the original transform position. Essentially, you need to store the original y position in a variable, that would look like this: (Don't forget to set the variable in the Start() function.) Vector2 floatY What your problem here is that you are not preserving the original y position.










Top down engine unity