Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

how place an object below ground, then every animation end, increment y position

Discussion in 'Scripting' started by digiman72, Nov 14, 2018.

  1. digiman72

    digiman72

    Joined:
    Oct 6, 2018
    Posts:
    31
    hello there, I am trying to simulate a building being built, I want to start it below the ground, then an animation fires an event function to increment the y value, so the building rises up...

    I am able to place the object, although I hard coded the height, since the building height are all the same.

    this is what I got so far in code, but currently it just pushes the building even farther below, and brings it back, up but still below the gound....what am I doig wrong ??

    Code (csharp):
    1.  
    2. [code=CSharp] float animFrame = 0;
    3.     public void buildHouse() //fired from an event in animation "build"
    4.     {
    5.         animFrame++;
    6.  
    7.         dir2.y += animFrame; // make the building rise..?? but now working
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         if(isCLicked==true)       //before right click have building follow mouse hit pos
    13.         {
    14.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    15.             RaycastHit hit;
    16.  
    17.             if (Physics.Raycast(ray, out hit))
    18.             {
    19.                dir = hit.point;
    20.                 myworldScpt.buildDir = dir;
    21.                 dir.y = 0;
    22.                 currentPlaceableObject.transform.position=dir;
    23.              
    24.             }
    25.         }
    26.        
    27.  
    28.         if (Input.GetMouseButtonDown(1)) //when right click, set position of building below ground
    29.         {
    30.             if(isCLicked==true)
    31.             {
    32.  
    33.                 myworldScpt.worldPopulation += residents;
    34.                 myworldScpt.popAmount.text = myworldScpt.worldPopulation.ToString();
    35.              
    36.              
    37.                 currentPlaceableObject.transform.rotation = Quaternion.identity;
    38.                 dir2 = new Vector3(dir.x, dir.y - 6.5f, dir.z);
    39.                 currentPlaceableObject.transform.position = dir2;
    40.    
    41.             isCLicked = false;
    42.              
    43.                
    44.             }
    45.  
    46.         }
    [/CODE]

    thanks for any help ?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Code (CSharp):
    1. dir2 = new Vector3(dir.x, dir.y - 6.5f, dir.z);
    You reduce altitude by subtracting.
    Probably you want add height.
     
  3. digiman72

    digiman72

    Joined:
    Oct 6, 2018
    Posts:
    31
    that part was to set it below the ground..., the code is all commented..but thanks
     
  4. digiman72

    digiman72

    Joined:
    Oct 6, 2018
    Posts:
    31
    hmm anyone ? everything I try, it doesn't work, using animator or this way, please....?

    here it is using Animator, it always returns null for some reason when the player starts building

    When I look at inspector and I click on the house1 in editor, it clearly says "House1";
    Code (CSharp):
    1.  
    2.  
    3. public void setClick() //////// fired by UI Image button
    4.     {
    5.         i++;
    6.         isCLicked = true;
    7.      
    8.          currentPlaceableObject = Instantiate(placeableObjectPrefabs[0]);
    9.         //currentPlaceableObject.GetComponentInChildren<Animator>().SetFloat("speed",1);
    10.         currentPlaceableObject.name = "House" + i;
    11.      
    12.     }
    13.     float speed = 5;
    14.     // Update is called once per frame
    15.     float animFrame = 0;
    16.     public void buildHouse()      ///  the build animation fired an event at the end called HouseBeingBuilt, to the player
    17.     {                                                     ////    whith another function called buildhouse sent back to button script..
    18.         if(currentPlaceableObject!=null)
    19.         {
    20.             myAnim = GameObject.Find("House" + i).GetComponentInChildren<Animator>(); /// returns null reference
    21.  
    22.  
    23.             myAnim.Play("HouseUp", 0, (1 / 60) * animFrame); ///nothing happens
    24.         }
    25.      
    26.  
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         if(isCLicked==true)
    32.         {
    33.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    34.             RaycastHit hit;
    35.  
    36.             if (Physics.Raycast(ray, out hit))
    37.             {
    38.                dir = hit.point;
    39.                 myworldScpt.buildDir = dir;
    40.                 dir.y = 0;
    41.                 GameObject.Find("House" + i).transform.position=dir;
    42.              
    43.             }
    44.         }
    45.        
    46.  
    47.         if (Input.GetMouseButtonDown(1))   ///// this works
    48.         {
    49.             if(isCLicked==true)
    50.             {
    51.  
    52.                 myworldScpt.worldPopulation += residents;
    53.                 myworldScpt.popAmount.text = myworldScpt.worldPopulation.ToString();
    54.                 currentPlaceableObject.transform.rotation = Quaternion.identity;
    55.                 dir2 = new Vector3(dir.x, dir.y, dir.z);
    56.                 currentPlaceableObject.transform.position = dir2;
    57.                 currentPlaceableObject.GetComponentInChildren<Animator>().Play("HouseDown"); ////works
    58.                 currentPlaceableObject = null; /////// the temp object holding the gameobject inside the GameObject Array[]
    59.                 isCLicked = false;
    60.                
    61.              
    62.             }
    63.  
    64.         }
    65.        
    66.        
    67.      
    68.  
    69.     }
    70.  
     
  5. digiman72

    digiman72

    Joined:
    Oct 6, 2018
    Posts:
    31
    well I realized that my animation would of never worked, I believed I could play the animation frame by frame using the current method, but its not the case, I still don't know why I could not move my house in the y axis, I checked to make sure there was no positions set in the animation as well, but anyway, I managed to just do it simple way of firing the instatiated object animation from the player when he arrived at destination instead, and set both animations to same speed, I would of preferred a frame by frame approach, that moves the house y position every frame, until it reaches the proper position above ground, but for now I guess this will have to do...if someone still wants to share that method, It would be appreciated, cheers!!!
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    What I can suggest, if you use
    Code (CSharp):
    1. Debug.Log ( "Some text: " + someValue ) ;
    Put it in few places, and observe, if your code is triggered as expected.