changing Y in transform only?

hello iam trying to round the Y position of my player in a 2d game i already tried to do something like

transform.position=new Vector2(transform.position.x,Mathf.RoundToInt(transform.position.y));

but it has heavy issues with the rigidbody velocity movement then can i somehow directly affect y and nothing else?

transform.position.y = Mathf.RoundToInt(transform.position.y);

it says

“error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position’. Consider storing the value in a temporary variable”

That message suggests that this would work:
var p = transform.position;
p.y = Mathf.RoundToInt(transform.position.y);
transform.position = p;

having the same issues with the rigidbody component again

in c# you can’t directly alter one component of a vector2/3, you have to do the original approach, create a new vector changing/carrying over each value.

@Rostam24 , you appear to be using unityscript, that does allow you to directly alter one component.

directly altering the position of a transform (by any method) means you are making changes to the gameobject without the physics engine; hence causing the problems with the rigidbody.

What are you trying to actually achieve?

ive got a tile based 2d map for platforming my character is 1 by 1 tile.
I want to include passages with holes. Speaking of walls with 1 tile holes. The player just bypasses them when jumping or falling beneath them.

I tried to hide a smaller collider wich fits in and deactivating the normal one whenever the player is in the air.
but then he gets stuck at one edge what is fixed by rounding the y position while he is on ground.

that however created the issue with the rbody then

@LeftyRighty , I code in C#, so I’m not entirely sure what you mean. From what I understand, Vector2/3 is a struct, so var p = transform.position is the same as var p = new vector3(transform.position.x, transform.position.y,transform.position.z);
However, I’m not sure, so let me know if I’m mistaken :slight_smile:

@klekle204 , got a screenshot or video?

i try to get screenshots
what do you want to see the rigidbody slowdown or the edge stuck or the situation in generall

My bad, I should’ve made those code tags in Javascript/UnityScript.

I fixed it by myself by simply not updating Y all the time and just when its needed for about 2 frames.
As this is a very specific problem a explanation is not possible/needed