- Home /
transform.position not working in OnControllerColliderHit
Hi. I have code with detection player collider with another blocks. In previous project and version of unity this me working. I using for detection this method: void OnControllerColliderHit(ControllerColliderHit collision) I have also Debug.Log for debuging, if the collider detection working. But if i insert into this method these code, nothing happaned: transform.position = new Vector3 (0, 0, 0); I am tryed another method like another coordinates, GameObject.transform.position atc. In previous project working this correctly but now it not working. For detection with another object i using these: if (collision.gameObject.name == "Dropp") Another interested thing is, when i place piece of the code with the transform.position..... in the another part of the code, it works perfect. Any case when this is not working is in my method with detection. Do i anything wrong? If i do anything wrong, why it is worked in previous version and another project?
Thanks all. Marks
Can you show us more parts of your code? Sounds like you made a mistake somewhere else in your code where you don't expect it.
$$anonymous$$y code:
void OnControllerColliderHit(ControllerColliderHit collision){
Debug.Log("a");
if(collision.gamobject.name = "Dropp"){
Debug.Log("b");
Debug.Log(transform.position);
transform.position = new Vector3(0,0,0);
Debug.Log(transform.position);
}
}
When i collision with any object, it shows me a. If i collision with object named Dropp, it show me b and player location and location after transport. Interestingly, the location after transport is the same as the finish location, but the player is not teleported.
Answer by Marks98 · Feb 16, 2018 at 06:41 PM
After all what i tryed i reported it as bug to unity, because it not working only in my code. The unity helpers writed me back. The problem is, when i tryed change the position player, the player has own update method with teleport him at the same coordinates. Only what is need to do is disable it. If i understant it correctly. Here is example of my worked code which working thanks to unity helpers. Thanks !
void OnControllerColliderHit(ControllerColliderHit collision){
if(collision.gameObject.name == "Dropp"){
transform.position = new Vector3(0,0,0);
GetComponent<CharacterController>().enabled = false;
}
}
private void LateUpdate(){
if (GetComponent<CharacterController> ().enabled == false) {
GetComponent<CharacterController> ().enabled = true;
}
}
Answer by bliu886 · Feb 06, 2018 at 03:44 PM
It looks like this code on a script attached to a block (or whatever object the player is colliding with). In this case, you should do
collision.gameObject.transform.position = new Vector3(0,0,0);
instead of
transform.position = new Vector3(0,0,0);
to change the location of the player instead of the location of the object the player is colliding with.
If you mean change from these:
transform.position = new Vector3(0,0,0);
to these:
collision.gameObject.transform.position = new Vector3(0,0,0);
I tried it and this code teleport the platform called Dropp. The player is in the same coordinates. I don't know why, but when i debuging player coordinates show me good data, but nothing happened like teleport.
$$anonymous$$ake sure that the scripts are attached to the right gameObjects. Is there other code that could change player's position which might be changing the position back?
I am using characters assets including by unity. $$anonymous$$y script i have placed on FPSController. When i place my script on the camera, nothing happenings, because camera cannot collide with any object. I don't know what i do wrong.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152013im_/https://answers.unity.com/themes/thub/images/avi.jpg)