What I want to do is when the contrller hit an object to move (snap the controller) to the X position of the object it hit
I trty to use the controller.transfor.position.x = hit.trnasform.position.x but is does not work.
any suggestions. thank you very much for the help in advance
D
What does your code look like? I would assume you want to put that in OnControllerColliderHit. Is it compiling? The code you wrote up there is full of typos.
my code look likr this
function OnControllerColliderHit(hit : ControllerColliderHit){
var collisionObj = hit.collider.gameObject.name ;
if (collisionObj == "upper_floor"){
hitTopFloor = true;
}
if (collisionObj == "ladder" ||collisionObj == "cageB" )
{
//HideElevatorCage(true, false);
//Move controller to the center of the ladder
Debug.Log("controller " + hit.controller.transform.name);
Debug.Log("Ladder " + hit.transform.position.x);
hit.controller.transform.position.x = hit.transform.position.x;
}
Just a little thing, I am pretty sure on the last line the “hit.controller.transform” could just be “transform”.
If there is some oddity or bug changing that might fix it. What happens? do you get the debug logs?, is it getting past the if?
I am sorry for the lack of information.
actually when I run this code there is no error and nothing happends either. The controller wont move to the wanted location it just stays there.
So no debug messages? That means that it isn’t getting past “if (collisionObj == “ladder” ||collisionObj == “cageB” )”
Check your object names.
ohh I am sorry
yesss I get the values back for the x position. it is going inside the If statement but the contro is not been afected.
Did you make the change Yoggy suggested in making the last line?:
transform.position.x = hit.transform.position.x;
I believe how it was first posted the variable was was just being assigned to itself.
hit.controller.transform.position is the character controller and the hit.transform.position is the object that the controller hit
How right you are. When I try to use the script you posted it does not work for me either. I can get Unity to beachball pretty easily though :0. Using a debug line after the position assignment, it looks like it does change the controller transform’s position. It be setting the position back before the next frame. Perhaps it is something to do with the character controller.
A possible work around is to set a flag when colliding. Then, in the LateUpdate() apply the position change.
var setNewPosition = true;
function OnControllerColliderHit(hit : ControllerColliderHit){
var collisionObj = hit.collider.gameObject.name ;
if (collisionObj == "upper_floor"){
hitTopFloor = true;
}
if (collisionObj == "ladder" ||collisionObj == "cageB" )
{
//HideElevatorCage(true, false);
//Move controller to the center of the ladder
Debug.Log("controller " + hit.controller.transform.position +"\tLadder " + hit.transform.position);
//hit.controller.transform.position.x = hit.transform.position.x;
//Debug.Log("After: controller " + hit.controller.transform.position +"\tLadder " + hit.transform.position);
setNewPostion = true;
}
function LateUpdate()
{
if (setNewPosition)
{
hit.controller.transform.position.x = hit.transform.position.x;
//transform.position.y = 5.0;
setNewPosition = false;
}
}
Thanks Ifrog I will try this. I’ll post later the results. I looks like a good Idea