Search Unity

Question Newbie at Unity; help me script for movement please

Discussion in 'Scripting' started by hsadiyaa, Mar 28, 2023.

  1. hsadiyaa

    hsadiyaa

    Joined:
    Nov 15, 2022
    Posts:
    3
    Hello, World! (Pun not intentionally intended)

    After countless YouTube videos, forums, manual, and blog readings, I was wondering if I may receive some help with coding for movement cause I cannot figure it out completely (just able to collect some idea on how-to along the way). I have have two objects that I would like to have slide away from each other x amount of distance and then begin to rotate up to ~135• (ex. to help visualize: double doors that are closed and then start to slide/move away from each other and then begin to rotate outwards pivoting on their outer edge say once they are like 10units from each other, then they stop translating at 90• and 15units apart, but continue to rotate outwards while stationary until they reach 135• and then stop; at this point the doors should now be wide open, each outwards at 135• and 15 units apart. Also movement is relative to their outer edge, so 15 units apart from the outer edge when closed but after rotating the outer edge is now the inner/closer edge to each other). Hopefully that made sense

    I want to be able to change the units of translation and the angle in the inspector so I know I’ll need to have public floats for each (I presume). So far I have:

    .
    .
    .
    public Transform objectLocation;
    public float rotationAngle;
    public float rotationSpeed;
    public GameObject pivotObject;
    .
    .
    .
    transform.Translate(new Vector3(1,0,0), objectLocation);
    .
    .
    .
    private Vector3 Vector3(float v1, int v2, int v3)
    .
    throw new NotImplementedException() <—this was inserted as a suggestion by visuals studio but idk what it really does exception something to do with true/false
    .
    .
    .

    transform.Rotate(new Vector3(0,0,-1), rotationSpeed * Time.smoothDeltaTime);
    .
    .


    I won’t lie some of the script ‘I think’ I made-up just based on what I’ve come across (I’m just trial and error at this point but I guess that’s the best teacher)

    I know I’ll need a start/stop and a true/false codon somewhere or somehow but I haven’t figured that part out yet.

    Also would like to be able to have them do the reverse; so rotate inwards and slide back closed.

    Any guidance or help or explanation or script ;) would immensely help as I’ve been trying to figure it out for two days now and time is moving but progress is slow.
    Thank you in advance !
     
    Last edited: Mar 28, 2023
  2. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    You could take a loot at Coroutines for that. The code is not tested and by far not complete, you have to add the rotation and check for movement in the right direction etc. but you have to figure it out from here or you won't be able to use it in the future ;)

    Code (CSharp):
    1. //somewhere in your code
    2. StartCoroutine(OpenDoors());
    3.  
    4.  
    5. IEnumerator OpenDoors()
    6.     {
    7.     Vector3 startPosition=transform.position;
    8.         while (Vector3.Distance(transform.position, startPosition)>1f)
    9.         {
    10.             transform.Translate(new Vector3(1,0,0) * Time.deltaTime);
    11.             yield return null;
    12.         }
    13.     }
    More examples: https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
     
    hsadiyaa likes this.
  3. hsadiyaa

    hsadiyaa

    Joined:
    Nov 15, 2022
    Posts:
    3
    Thank you very much! I will play around with it and hopefully can figure it out from here :)

     
  4. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Good Tutorial on Coroutines: