- Home /
Question by
sullivanshad · Dec 02, 2013 at 03:42 AM ·
c#positionplayer
Strange shifting when reseting player position to origin
I've been working on a script that will reset the position of the player back to the origin(0,0,0), when a certain object is touched. The object resets it's position fine at first, but then drifts several units to one side before stopping, which I don't want. This is the code:
using UnityEngine;
using System.Collections;
public class ControllerScript : MonoBehaviour {
public float speed = 1.0f;
public Vector3 endPos;
public static bool moving = false;
public static int health = 100;
private Vector3 initialPosition;
private Quaternion initialRotation;
void Start () {
endPos = transform.position;
initialPosition= transform.position;
initialRotation=transform.rotation;
}
void Update () {
bool hittingForward=false;
if(Physics.Raycast(transform.position, Vector3.forward, 1)){
hittingForward=true;
}
bool hittingRight=false;
if(Physics.Raycast(transform.position, Vector3.right, 1)){
hittingRight=true;
}
bool hittingLeft=false;
if(Physics.Raycast(transform.position, Vector3.left, 1)){
hittingLeft=true;
}
bool hittingBackwards=false;
if(Physics.Raycast(transform.position, -Vector3.forward, 1)){
hittingBackwards=true;
}
if (moving && (transform.position == endPos)){
moving = false;
}
if(!moving && Input.GetKey(KeyCode.W) && hittingForward==false){
moving = true;
endPos = transform.position + Vector3.forward;
}
if(!moving && Input.GetKey(KeyCode.D) && hittingRight==false){
moving = true;
endPos = transform.position + Vector3.right;
}
if(!moving && Input.GetKey(KeyCode.A) && hittingLeft==false){
moving = true;
endPos = transform.position + Vector3.left;
}
if(!moving && Input.GetKey(KeyCode.S) && hittingBackwards==false){
moving = true;
endPos = transform.position - Vector3.forward;
}
transform.position = Vector3.MoveTowards(transform.position, endPos, Time.deltaTime * speed);
if(health==0){
Destroy(gameObject);
}
}
void OnTriggerEnter(Collider col){
if(col.gameObject.tag=="Finish" && overseer.floors<60){
Debug.Log ("colliding");
GameObject[] name1=GameObject.FindGameObjectsWithTag("room");
GameObject[] name2=GameObject.FindGameObjectsWithTag("environment");
GameObject[] name3=GameObject.FindGameObjectsWithTag("Trap");
GameObject[] name4=GameObject.FindGameObjectsWithTag("Path");
foreach(GameObject room in name1){
Destroy(room);
}
foreach(GameObject environment in name2){
Destroy(environment);
}
foreach(GameObject Trap in name3){
Destroy(Trap);
}
foreach(GameObject Path in name4){
Destroy(Path);
}
Destroy(GameObject.FindWithTag("Finish"));
overseer.floors++;
overseer.nextfloor=true;
transform.position=initialPosition;
transform.rotation=initialRotation;
}
}
}
does anyone know why this would be happening? I don't have any animations on the character yet, so that wouldn't be an issue. thanks.
Comment
Best Answer
Answer by robertbu · Dec 02, 2013 at 03:46 AM
Between lines 84 and 85, insert:
endPos = initialPosition;
Currently you are not resetting endPos, so line 56 is still trying to move your object to endPos.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123218im_/https://answers.unity.com/themes/thub/images/avi.jpg)