enemy teleport

Hello. How can i make my enemy teleport every 45 seconds. I tried with coroutines but it teleports and stay in that place and didn’t move. Sorry for my english. This is my enemy script.

#pragma strict
var target : Transform;
var moveSpeed = 3.1;
var rotationSpeed = 3;
var myTransform : Transform;
function Awake(){ myTransform = transform;
}
function Start(){
  target = GameObject.FindWithTag("Player").transform;
}
function Update () {
transform.position.y = 3;
transform.rotation.x = 0;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, transform.eulerAngles.z );
}
function OnTriggerEnter(col : Collider){
if(col.gameObject.tag == "Player"){
             Debug.Log ("game over");
         }
         }

I guess the problem is that you keep resetting the transform.position at the start of Update(). That will move your object back to the start constantly. Thereby ignoring any movement your teleport imparted.

Thanks, what should i do to make it works?

You could do something like the following, this will move the object and teleport every 45:th second. The movement is exactly like the one you had before.

#pragma strict

var target : Transform;
var moveSpeed = 3.1;
var rotationSpeed = 3;
var teleportTime = 45;

function Start() {
    target = GameObject.FindWithTag("Player").transform;

    transform.position.y = 3;
    transform.rotation.x = 0;

    InvokeRepeating("Teleport", teleportTime, teleportTime);
}

function Update() {
    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotationSpeed * Time.deltaTime);
    transform.position += transform.forward * moveSpeed * Time.deltaTime;
    transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, transform.eulerAngles.z);
}

function OnTriggerEnter(col : Collider)
{
    if (col.gameObject.tag == "Player") {
        Debug.Log("game over");
    }
}

function Teleport() {
    transform.position += Random.insideUnitSphere * 10;
}

Thank you! :smile:

I realize this is a really old thread, but hoping that you might be able to help me convert your above code to C#. I am still learning a lot when it comes to coding, but have managed the below thus far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Teleporting : MonoBehaviour
{
#pragma strict

public Transform target;
public double moveSpeed = 3.1;
public float rotationSpeed = 3;
public float teleportTime = 45;

void Start()
{
target = GameObject.FindWithTag(“Player”).transform;

/transform.position.y = 3;
transform.rotation.x = 0;
/

InvokeRepeating(“Teleport”, teleportTime, teleportTime);
}

void Update()
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotationSpeed * Time.deltaTime);
//transform.position += transform.forward * moveSpeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, transform.eulerAngles.z);
}

/void OnTriggerEnter(col : Collider)
{
if (col.gameObject.tag == “Player”)
{
Debug.Log(“game over”);
}
}
/

void Teleport()
{
transform.position += Random.insideUnitSphere * 10;
}

}

The problem is that the commented out transform.position does not work and neither does the one commented out in Update. I am trying to make the typical slender enemy that teleports around the scene, but trying to upgrade this code from JS. Any help appreciated!

Poly, it’s great that you realize it’s an old thread… but please don’t reply to old threads. You make a mess of the forum for everybody else.

Instead, create your own thread… it’s FREE!

When you do, here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

If you post a snippet of code, please use code tags: https://discussions.unity.com/t/481379