I am trying to make a simple teleport with this code:
public var endPoint : Transform;
function OnTriggerEnter (collidingObject:Collider) {
if (collidingObject.gameObject.name=="localPlayer") {
var pos : Vector3 = endPoint.transform.position;
var rot : Quaternion = endPoint.transform.rotation;
collidingObject.gameObject.transform.position = pos;
collidingObject.gameObject.transform.rotation = rot;
}
}
When I enter a trigger to which this script is attached my local player object (which is an object with mouse look, fps controller and fps walker) should change its position and rotation to the values obtained from the end point. However, only position is changed and rotation remains the same on the screen. It is strange for me because for example I am able to assign rotation in the opposite way (from the local player to the end point) without any problem.
Yes I tried. Both:
Debug.Log(GameObject.Find(“localPlayer”).transform.rotation);
Debug.Log(rot);
This code works:
public var endPoint : Transform;
function OnTriggerEnter (collidingObject:Collider) {
if (collidingObject.gameObject.name=="localPlayer") {
var pos : Vector3 = endPoint.transform.position;
var rot : Quaternion = endPoint.transform.rotation;
collidingObject.gameObject.transform.position = pos;
var mouseLookScript : MouseLook = GameObject.Find("localPlayer").GetComponent("MouseLook");
mouseLookScript.originalRotation = rot;
collidingObject.gameObject.transform.rotation = rot;
}
}
It is necessary to change originalRotation which is set in mouse look script and used as a reference. This value is not public so I had to make it public to make this code work.
I think the problem stemmed from the object being controlled specifically by the MouseLook. This would not be that much of a deal if the object was controlled independently and the mouselook updated in the LateUpdate() function.
function OnTriggerExit (other : Collider){ //use enter or exit what ever you need.
if (other.name == "my gameObject")
other.transform.position = new Vector3(300,100,0); // your vector3.
camera.main.transform.position = new Vector3 (300,100,0); // the same vector3 you put in the line above.
}