Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GamingDimensionsGD · Nov 22, 2021 at 03:43 PM · movementteleporting

Trying to teleport back and forth, but it randomly doesn't teleport

This is by for the most confusing bug I've seen. The way the code is supposed to work is that you press a key, it'll spawn a physics sphere to see if anything is in the way. If there isn't, it'll teleport you while playing an animation. It works 80% of the time. I don't know how, I don't know why. If someone knows what's going on, please tell me, I'm very confused.

Here's the movement script(moves player):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
     [Header("Player")]
     public CharacterController player;
     public float speed;
     public float airSpeed;
     public float jumpHeight = 1f;
 
     [Header("Ground & Gravity")]
     public Transform groundCheck;
     public LayerMask ground;
     public Vector3 velocity;
     public float gravityValue = -9.81f;
     public ChangeLevels lvl;
 
     private void Start()
     {
 
     }
 
     void Update()
     {
         if (velocity.y >= 5f) { velocity.y = 5f; }
 
         //Inputs
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
         bool jump = Input.GetButtonDown("Jump");
         bool isGrounded = Physics.CheckSphere(groundCheck.position, 0.1f, ground);
 
         Vector3 move = transform.right * x + transform.forward * z;
 
         if (isGrounded)
         {
             player.Move(move * speed * Time.deltaTime);
             if (velocity.y < 0) { velocity.y = -2f; }
             if (jump) { velocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue); }
         }
         else { player.Move(move * airSpeed * Time.deltaTime); }
 
         velocity.y += -9.84f * Time.deltaTime;
 
         player.Move(velocity * Time.deltaTime);
     }
 
     public void moveHere()
     {
         this.transform.position = lvl.pos;
         lvl.offset *= -1f;
     }
 }
 

Here's the teleporting script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
 public class ChangeLevels : MonoBehaviour
 {
     public GameObject player;
     public Vector3 pos;
     public float offset;
     public TMP_Text text;
     public Animator anim;
     public bool canSw;
     public LayerMask ground;
     public bool isTouching;
     Movement move;
 
     void Start()
     {
         move = GetComponent<Movement>();
     }
 
     void Update()
     {
         pos = new Vector3(player.transform.position.x + offset, player.transform.position.y, player.transform.position.z);
         isTouching = Physics.CheckSphere(pos, .4f, ground);
 
         if (Input.GetKeyDown(KeyCode.R)) { if (canSw) StartCoroutine(switchLevel()); else { inWay("[Wait For Cooldown]"); } }
     }
 
     IEnumerator switchLevel()
     {
         anim.SetTrigger("Switch");
         yield return new WaitForSeconds(0.16f);
         if (!isTouching)
         {
             canSw = false;
             move.moveHere();
             yield return new WaitForSeconds(1f);
             canSw = true;
         }
         else { inWay("[Object In Way]"); }
     }
 
     public void inWay(string newText) { text.text = newText; anim.SetTrigger("Warning"); }
 }
 

Note: It is actually random of when it wants to work or not. I've set up debug.logs all over the place one time. It does hit all the debugs that it needs to, it just won't teleport the player.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elkawee · Nov 22, 2021 at 05:26 PM 0
Share

the way you placed the

  canSw = false;

makes it possible for multiple instances of the Coroutine to be running at the same time. Could that be it?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

187 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to move 2D character from one place to another by "movement" and not "teleporting"? 2 Answers

,Reseting the Position of an Object 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Teleporting my player to the other side of the screen? 1 Answer

Making a bubble level (not a game but work tool) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges