I have my main player and I want him so that when he touches a certain object, he appears at another one. I tried using this script to no avail:
using UnityEngine;
using System.Collections;
public class Fun : MonoBehaviour {
float speed = 5.0f;
public Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "Player"){
transform.LookAt(target);
transform.position += transform.forward * speed * Time.deltaTime;
}
}
}
Help will be appreciated!
The Script you have there should (assuming proper setup) cause your trigger object to face the target location and move toward it for one frame. If you want to teleport your Player to the other location, instead of adding a small forward vector to the trigger object, just directly set the Player’s position to the target’s. Like so:
col.transform.position = target.position;
Well, this code doesn’t seem to do anything remotely similar to what you want - at most the object will move a few milimeters in direction to target when the player touches it.
From your text, it seems that you actually want to teleport the player when it touches this object - is this right? If so, you should make this object a trigger and use OnTriggerEnter: when the player touches the trigger, the code instantly moves him to the target object’s position, copying the target rotation as well:
using UnityEngine;
using System.Collections;
public class Fun : MonoBehaviour {
public Transform target; // drag the destination empty object here
void OnTriggerEnter(Collider other) {
if (other.tag == "Player"){ // if the player touched the trigger...
other.transform.position = target.position; // move it to the target position...
other.transform.rotation = target.rotation; // and assume the target rotation too
}
}
}
Memige is exactly right with the teleportation script. As for moving… I was thinking maybe just have a static boolean variable called “moving” or something like that preset to false. Then on the Update call, apply the movement portion within an if statement. Also, within that statement have a nested if stating that if the player’s location is as close to the target as you’d like, turn off the moving. Something like:
if (moving==true)
{
//Move Script Goes Here
if (Distance between Player and Target <= 3) // If the player is within a 3 unit radius
{
moving=false;
}
}
Hope this helps.