My character is teleported the wrong direction despite the code saying to teleport to the ball
I know it doesnt make sense. here is a youtube link demonstrating the issue https://youtu.be/91QUmWBAe9o
here is the code im using
using UnityEngine; using System.Collections;
public class teleporter : MonoBehaviour { public GameObject launchloc; public GameObject teleTarget; private Vector3 launchpos; private Vector3 telepos; // Use this for initialization void Start () { launchpos = launchloc.transform.position; }
// Update is called once per frame
void Update () {
if (GameObject.FindGameObjectWithTag("teleLoc"))
telepos = GameObject.FindGameObjectWithTag("teleLoc").transform.position;
launchpos = launchloc.transform.position;
if (Input.GetKeyDown(KeyCode.Mouse0))
Instantiate(teleTarget, launchpos, Quaternion.identity);
if (Input.GetKeyUp(KeyCode.Mouse0))
transform.Translate(telepos);
}
}
Answer by DavidWatts · Sep 03, 2016 at 10:53 PM
Transform.Translate moves the transform in the direction of the vector but you have a position vector. the direction vector should be telepos - transform.position so change line 9 to transform.Translate( telepos - transform.position );
now i never teleport to the location of the ball
sorry. still no. is there any way for me to teleport directly to a location? I dont $$anonymous$$d using something different than transform.translate. I tried rigidbody.move and i didnt get teleported anywhere. i just dont understand how hard it could be to just set your xyz to that of another objects
Thank you for you help. I cant add onto the comment feed for some reason, but your solutioon worked. Im kinda new to unity (havent used it much). thank you.