- Home /
Ignoring the transform.y position on movement
For example, I have 2 gameobjects. I have a script where gameobject1 follows gameobject 2 using this function:
transform.LookAt(player.position);
controller.SimpleMove(transform.forward * speed);
This works fine, but when I add more gameobjects, that also moves towards gameobject 1, it starts bugging out on the Y position, resulting the gameobject starts to fly above gameobject 1.
Is there some way to make sure every gameobject always stays on the ground (transform.position.y = 0)?
Answer by PouletFrit · May 30, 2014 at 03:04 PM
Since you are using SimpleMove, gravity is applied. So my guess is that your problem come from the lookAt which apply a rotation on the x and/or z axis. But you probably only want to look forward the direction in which your moving (you only want rotation on the y axis).
So you have two possibilities:
1) You only keep the rotation on y axis:
transform.LookAt(player.position);
Vector3 rot = player.eulerAngles;
rot.x = 0;
rot.z = 0;
player.eulerAngles = rot;
2) Or you can use Vector3.RotateTowards, in which you also can "gradually" rotate towards ur target:
float step = speed * Time.deltaTime;
Vector3 from = new Vector3(transform.position.x,0,transform.position.z);
Vector3 to = new Vector3(player.position.x,0,player.position.z);
transform.eulerAngles = Vector3.RotateTowards(from,to,step,0.0f);
That works well for the rotation, but my problem also was the Y position coordinate. The gameobject still floats into the air. This is what I'm using right now:
transform.LookAt(player.position);
Vector3 rot = player.eulerAngles;
rot.x = 0;
rot.z = 0;
player.eulerAngles = rot;
controller.Simple$$anonymous$$ove(transform.forward * speed);
Is it feeling like a character is able to just gradualy walk on top of another one? If yes, it might be that your collider aren't set properly.
Try adjusting their height and you can always set the slope Limit of the character controller to a higher number.
And of course, you can always use what Landern proposed, it is pretty straighforward, but it should definitly work: just set position.y to 0 (or whatever height is ur floor) after each movement, that way your are sure that your character will always have it's y to 0.
Ugh, I appreciate your help a lot but I can't get it to work properly. I tried (just in case) increasing the slope limit of the game objects. I tried setting the height always to 0 with: alwaysGround = gameObject.transform.position.y; Collider height is a bit strange to me though, it means that if I set it higher then my character will always fly.
And yes, the gameobjects are gradualy walking on top of others
have you tryed to simply add
transform.position = new Vector3(transform.position.x, 0, transform.position.z);
right after controller.Simple$$anonymous$$ove?
Answer by Landern · May 30, 2014 at 01:46 PM
If there is a rigidbody attached, either through code or the inspector you can freeze rotation and position.
I'm not sure but when I added the RigidBody and Constrained the X position, the gameobject moves really laggy because it is still calling the Simple$$anonymous$$ove function. Sorry for being new, but how do I fix it?
you shouldn't use a rigidbody with a controller, normally it produce weird result
Okay but how do I fix this problem then without using a rigidbody?:/
$$anonymous$$odify the Simple$$anonymous$$ove method and ensure that the position.y is always 0 when updating the position.