Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

enemy teleport

Discussion in 'Scripting' started by AdinaS, Mar 8, 2015.

  1. AdinaS

    AdinaS

    Joined:
    Jan 19, 2015
    Posts:
    24
    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.
    Code (JavaScript):
    1. #pragma strict
    2. var target : Transform;
    3. var moveSpeed = 3.1;
    4. var rotationSpeed = 3;
    5. var myTransform : Transform;
    6. function Awake(){ myTransform = transform;
    7. }
    8. function Start(){
    9.   target = GameObject.FindWithTag("Player").transform;
    10. }
    11. function Update () {
    12. transform.position.y = 3;
    13. transform.rotation.x = 0;
    14. myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    15. transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, transform.eulerAngles.z );
    16. }
    17. function OnTriggerEnter(col : Collider){
    18. if(col.gameObject.tag == "Player"){
    19.              Debug.Log ("game over");
    20.          }
    21.          }
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    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.
     
  3. AdinaS

    AdinaS

    Joined:
    Jan 19, 2015
    Posts:
    24
    Thanks, what should i do to make it works?
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    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.

    Code (CSharp):
    1. #pragma strict
    2.  
    3. var target : Transform;
    4. var moveSpeed = 3.1;
    5. var rotationSpeed = 3;
    6. var teleportTime = 45;
    7.  
    8. function Start() {
    9.     target = GameObject.FindWithTag("Player").transform;
    10.  
    11.     transform.position.y = 3;
    12.     transform.rotation.x = 0;
    13.  
    14.     InvokeRepeating("Teleport", teleportTime, teleportTime);
    15. }
    16.  
    17. function Update() {
    18.     transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotationSpeed * Time.deltaTime);
    19.     transform.position += transform.forward * moveSpeed * Time.deltaTime;
    20.     transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, transform.eulerAngles.z);
    21. }
    22.  
    23. function OnTriggerEnter(col : Collider)
    24. {
    25.     if (col.gameObject.tag == "Player") {
    26.         Debug.Log("game over");
    27.     }
    28. }
    29.  
    30. function Teleport() {
    31.     transform.position += Random.insideUnitSphere * 10;
    32. }
     
  5. AdinaS

    AdinaS

    Joined:
    Jan 19, 2015
    Posts:
    24
    Thank you! :D
     
  6. PolymathicIndustries

    PolymathicIndustries

    Joined:
    Feb 17, 2020
    Posts:
    3
    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!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,487
    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://forum.unity.com/threads/using-code-tags-properly.143875/