Search Unity

Csharp change orientation Opend door

Discussion in 'Scripting' started by ianis100, Aug 30, 2018.

  1. ianis100

    ianis100

    Joined:
    Jun 18, 2018
    Posts:
    8
    I start programming I managed to copy a tutorial for the opening of a sliding door.
    My problem is that if I move the animation on the field and the axis is different the door always opens in the same direction. In the successful test it opens in Z .... and I can not open it if it has to be done in X. I tried to invert the values in the lines x and z but nothing moved ....
    I would like to receive some tips to finish my study ... Thank you for your help



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class TriggerPorteCoulissante : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public float maxOpenValue;
    9.     public Transform door;
    10.     public bool opening = false;
    11.     public bool closing = false;
    12.  
    13.  
    14.  
    15.     private float currentValue = 0;
    16.     // Use this for initialization
    17.     void Start()
    18.     {
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (opening) OpenDoor();
    26.         if (closing) CloseDoor();
    27.     }
    28.  
    29.     void OnTriggerEnter(Collider obj)
    30.     {
    31.         if (obj.transform.name == "LocalPlayer")
    32.         {
    33.             opening = true;
    34.             closing = false;
    35.         }
    36.     }
    37.     void OnTriggerExit(Collider obj)
    38.     {
    39.         if (obj.transform.name == "LocalPlayer")
    40.         {
    41.             opening = false;
    42.             closing = true;
    43.         }
    44.  
    45.     }
    46.  
    47.  
    48.     void OpenDoor()
    49.     {
    50.         float movement = speed * Time.deltaTime;
    51.         currentValue += movement;
    52.  
    53.         if (currentValue <= maxOpenValue)
    54.         {
    55.             door.position = new Vector3
    56.                 (
    57.                 door.position.x,
    58.                 door.position.y,
    59.                 door.position.z + movement
    60.  
    61.                 );
    62.         }
    63.         else
    64.         {
    65.             opening = false;
    66.         }
    67.     }
    68.  
    69.     void CloseDoor()
    70.     {
    71.         float movement = speed * Time.deltaTime;
    72.         currentValue -= movement;
    73.  
    74.         if (currentValue >= 0)
    75.         {
    76.             door.position = new Vector3(
    77.                 door.position.x,
    78.                 door.position.y,
    79.                 door.position.z - movement
    80.  
    81.  
    82.                 );
    83.         }
    84.         else
    85.         {
    86.             closing = false;
    87.         }
    88.     }
    89.  
    90.  
    91.  
    92. }
     
    Last edited: Aug 30, 2018
  2. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    If you can post in English I might be able to help you. ;)
    It'd also be extremely helpful if you used the code tags. They make it much easier to read the code you posted.
     
  3. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    The code tag can be found on the bar above where you post. It says code and has a little picture of a document with <> on it. I can't fault you too much, I just realized there was an inline code tool beside the code block.

    Can you show me one of your scripts where it's not opening as well?
     
    Last edited: Aug 30, 2018
  4. ianis100

    ianis100

    Joined:
    Jun 18, 2018
    Posts:
    8
    Thanks Avo I found
     
  5. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    Sorry I couldn't help. The script you posted appears to work just fine for me.
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Just to mention, if you are animating properties, then those values are absolute, not relative. For example, if you animate sliding a position along the X-axis then that is exactly what will happen to the object who's property is being animated.
     
  7. ianis100

    ianis100

    Joined:
    Jun 18, 2018
    Posts:
    8
    if I change the lines 10 and 12 and 30 and 32 on this page , in this way I saw that it was not working

    1. void OpenDoor()
    2. {
    3. float movement = speed * Time.deltaTime;
    4. currentValue += movement;

    5. if (currentValue <= maxOpenValue)
    6. {
    7. door.position = new Vector3
    8. (
    9. door.position.x + movement
    10. door.position.y,
    11. door.position.z,

    12. );
    13. }
    14. else
    15. {
    16. opening = false;
    17. }
    18. }

    19. void CloseDoor()
    20. {
    21. float movement = speed * Time.deltaTime;
    22. currentValue -= movement;

    23. if (currentValue >= 0)
    24. {
    25. door.position = new Vector3(
    26. door.position.x - movement
    27. door.position.y,
    28. door.position.z,


    29. );
    30. }
    31. else
    32. {
    33. closing = false;
    34. }
    35. }
    I will try to progress in CSharp
     
  8. ianis100

    ianis100

    Joined:
    Jun 18, 2018
    Posts:
    8
    yes Avo this script works very well ... very fine. Only when I move the door to another place and the orientation has changed, and the animation remains in the same axis as before. The script does not record that the door has changed direction. This is where I do not know how to do it!
     
  9. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    Oh, now I understand your problem.

    change all of your
    door.position.whatever
    to
    door.localPosition.whatever

    When you use position it always moves it relative to the world, if you want to move it relative to itself you need to use localPosition.
    https://docs.unity3d.com/ScriptReference/Transform-localPosition.html

    I hope that's what you're looking for!

    It's difficult to read when it's not in code tags but I think you wiped out a "," in your Vector3's.
     
  10. ianis100

    ianis100

    Joined:
    Jun 18, 2018
    Posts:
    8
    Thanks Avo
     
    Avo likes this.