Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed May 15 at 09:33 PM by Deuce2008 for the following reason:

Other

avatar image
1
Question by Deuce2008 · May 16 at 09:51 AM · doorsuntiy3d

Variable changing randomly

Hello, my script wont work. I've started working on a door script to make adding doors easier. But the script just wont work, and I cant figure out why.

 //Door
 public GameObject door;

 //The position of the door when open
 public GameObject openDoor;

 //Distance that the door opens
 public float openDistance;

 //The speed the door opens
 public float openSpeed;

 //The speed the door closes
 public float closeSpeed;

 //Player tag
 public string playerTag;

 //The player found by tag
 GameObject player;

 //Where the door starts
 Transform startPos;

 void Start()
 {
     //Finds the player with said tag
     player = GameObject.FindGameObjectWithTag(playerTag);

     //Sets the start position of the door
     startPos = door.transform;
 }

 // Update is called once per frame
 void Update()
 {
     //Calculate the distance
     float distance = Vector3.Distance(player.transform.position, transform.position);

     //Open door
     if (distance <= openDistance)
     {
         //Calculate the movement
         float xPos = Mathf.Lerp(door.transform.position.x, openDoor.transform.position.x, openSpeed);
         float yPos = Mathf.Lerp(door.transform.position.y, openDoor.transform.position.y, openSpeed);
         float zPos = Mathf.Lerp(door.transform.position.z, openDoor.transform.position.z, openSpeed);

         //Set the position of the door
         door.transform.position = new Vector3(xPos, yPos, zPos);

         //Calculate the rotation
         float xRot = Mathf.Lerp(door.transform.rotation.x, openDoor.transform.rotation.x, openSpeed);
         float yRot = Mathf.Lerp(door.transform.rotation.y, openDoor.transform.rotation.y, openSpeed);
         float zRot = Mathf.Lerp(door.transform.rotation.z, openDoor.transform.rotation.z, openSpeed);

         //Set the rotation of the door
         door.transform.rotation = new Quaternion(xRot, yRot, zRot, door.transform.rotation.w);
     }

     //Close door
     if (distance >= openDistance)
     {
         //Calculate the movement
         float xPos = Mathf.Lerp(door.transform.position.x, startPos.position.x, closeSpeed);
         float yPos = Mathf.Lerp(door.transform.position.y, startPos.position.y, closeSpeed);
         float zPos = Mathf.Lerp(door.transform.position.z, startPos.position.z, closeSpeed);

         //Set the position of the door
         door.transform.position = new Vector3(xPos, yPos, zPos);

         //Calculate the rotation
         float xRot = Mathf.Lerp(door.transform.rotation.x, startPos.rotation.x, closeSpeed);
         float yRot = Mathf.Lerp(door.transform.rotation.y, startPos.rotation.y, closeSpeed);
         float zRot = Mathf.Lerp(door.transform.rotation.z, startPos.rotation.z, closeSpeed);

         //Set the rotation of the door
         door.transform.rotation = new Quaternion(xRot, yRot, zRot, door.transform.rotation.w);
     }
 }

Thank you in advance, I'm sure I just missed something stupid...

EDIT,

The startPos variable is changing and I cant figure out why. The door moves, But won't move back. I have an empty game object that just holds all the components; the door object the script and the open door game object. The script was working perfectly, I exited out of unity and came back later and it wouldn't work.

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

  • Sort: 
avatar image
0

Answer by gernomino · May 15 at 12:17 PM

Couple questions/recommendations


Do you know which variable is changing? Would help with debugging.


Is openDoor a separate game object? If it's inside of the gameObject with this script you could maybe have problems because it's rotating this gameObject to the rotation of openDoor. But if it's a child, then rotating`door` will rotate openDoor, and you may end in an ever rotating door.


I made a mistake a couple days ago where I had my lerp time greater than one. I think it should be between 0 and 1. If setting in the inspector you could add change the code to

  //The speed the door opens
 [Range(0, 1)] // might need to be [Range(0f, 1f)]?
  public float openSpeed;
  //The speed the door closes
 [Range(0, 1)]
  public float closeSpeed;
Comment
Add comment · Show 1 · 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 Deuce2008 · May 15 at 02:47 PM 0
Share

I have the open and close speed set to, 0.03125. I'll add this though, thank you.

Follow this Question

Answers Answers and Comments

141 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

Related Questions

Multi Switch Door 4 Answers

Scene Change OnCollision Not Working 2 Answers

Opening door with the same key? 1 Answer

how to calc the user figer path in existed zone 0 Answers

Save and load objects created at runtime 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