• 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 dbubbis · 2 days ago · charactercontrollercollision detectioncharacter controller

If-statement not working on certain gameObjects,Transform.Position not working anymore on gameObject except in one if-statement.

For some reason, whenever I use an if-statement on gameObjects besides one of them, the entire if statement except for when I try to change the player's position works. Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Threading;
 public class detectcollision : MonoBehaviour
 {
     public GameObject tplocation1;
     public GameObject player;
     public GameObject entranceloc;
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
         //This if statement does Debug.Log the message and it also changes the player's position.
         if (hit.gameObject.name == "trigger")
         {
             Debug.Log("triggered");
             gameObject.transform.position = tplocation1.transform.position;
             Thread.Sleep(7);
             Application.Quit();
         }
         //This if statement does not change the player's position, but Debug.Logs the teleporting message.
         if (hit.gameObject.name == "entrance")
         {
             gameObject.transform.position = new Vector3(-3184.53f, 20.35f, -171.585f);
             Debug.Log("teleporting...");
         }
 
     }
 }


,This problem is very unusual, and I really need this to be fixed soon. Here is my code where I am trying to change the player's position:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Threading;
 public class detectcollision : MonoBehaviour
 {
     public GameObject tplocation1;
     public GameObject player;
     public GameObject entranceloc;
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
         //This if statement does Debug.Log the message and it also changes the player's position.
         if (hit.gameObject.name == "trigger")
         {
             Debug.Log("triggered");
             gameObject.transform.position = tplocation1.transform.position;
             Thread.Sleep(7);
             Application.Quit();
         }
         //This if statement does not change the player's position, but Debug.Logs the teleporting message.
         if (hit.gameObject.name == "entrance")
         {
             gameObject.transform.position = new Vector3(-3184.53f, 20.35f, -171.585f);
             Debug.Log("teleporting...");
         }
 
     }
 }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Deuce2008 · 2 days ago

If you are using a character controller, it won't work. character controller don't respond well to having there transforms changed. To fix this is to first disable the character controller, teleport the player, then enable the character controller.

Example:

          void TeleportPlayer(Vector3 position)
          {
              //Get the charecterController attached to this gameobject
              CharacterController controller = GetComponent<CharacterController>();
      
              //Disables the controller
              controller.enabled = false;
      
              //Teleports this object to the position specified when this function is called
              transform.position = position;
      
              //Enables the controller
              controller.enabled = true;
          }
Comment
Add comment · Show 2 · Share
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 dbubbis · 2 days ago 0
Share

Could you provide an example?

avatar image Deuce2008 dbubbis · yesterday 0
Share

Could you mark as correct if this is the right answer.

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

156 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

Related Questions

Why does OnCollisionEnter not work between a character controller and a rigidbody? 4 Answers

Player acts weirdly when I release movement input 0 Answers

How to get a character to stay on a moving platform? 3 Answers

Opinion on Character Controllers? 1 Answer

I can't do that my character jumps while running 0 Answers


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