Search Unity

Help guys.

Discussion in 'Scripting' started by Jamesafk, May 24, 2015.

  1. Jamesafk

    Jamesafk

    Joined:
    May 22, 2015
    Posts:
    9
    I have a problem with the position of the field, even I have the good code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof(SpriteRenderer))]
    5.  
    6. public class Tiling : MonoBehaviour {
    7.  
    8.     public int offsetX = 2;         // the offset so that we don"t get any weird errors
    9.  
    10.     // these are used for checking if we need to instantiate stuff
    11.     public bool hasARightBuddy = false;
    12.     public bool hasALeftBuddy = false;
    13.  
    14.     public bool reverseScale = false;    // used if the object is not tilable
    15.  
    16.     private float spriteWidth = 0f;     // the width of our element
    17.     private Camera cam;
    18.     private Transform myTransform;
    19.  
    20.  
    21.     void Awake () {
    22.         cam = Camera.main;
    23.         myTransform = transform;
    24.     }
    25.  
    26.     // Use this for initialization
    27.     void Start () {
    28.         SpriteRenderer sRenderer = GetComponent<SpriteRenderer> ();
    29.         spriteWidth = sRenderer.sprite.bounds.size.x;
    30.     }
    31.     // Update is called once per frame
    32.     void Update () {
    33.         //does it still need buddies? If not do nothing
    34.     if (hasALeftBuddy == false || hasARightBuddy == false) {
    35.             //calculate the cameras extend (half the width) of what the camera can see in
    36.         float camHorizontalExtend = cam.orthographicSize * Screen.height;  
    37.        
    38.             // calculate the x position where the camera can see the edge of the sprite (element)
    39.             float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizontalExtend;
    40.             float edgeVisiblePositionLeft = (myTransform.position.x + spriteWidth/2) + camHorizontalExtend;
    41.          
    42.             // checking if we can see the edge of the element and then calling MakeNewBuddy if we can
    43.             if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)      
    44.             {        
    45.                 MakeNewBuddy (1);
    46.                 hasARightBuddy = true;
    47.             }
    48.             else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
    49.              {
    50.                 MakeNewBuddy (-1);
    51.                 hasALeftBuddy = true;
    52.             }
    53.       }
    54.     }
    55.    
    56.         // a function that creates a buddy on the side required
    57.         void MakeNewBuddy (int rightOrLeft) {
    58.             // calculating the new position for our new buddy
    59.             Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.z);
    60.           // instantating our new body and atoring him in a variable
    61.         Transform newBuddy    = Instantiate (myTransform, newPosition, myTransform.rotation)as Transform;
    62.  
    63.         // if not tilable let"s reverse the x size og our object to get rid of ugly seams
    64.         if (reverseScale == true) {
    65.             newBuddy.localScale = new Vector3 (newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
    66.     }
    67.  
    68.         newBuddy.parent = myTransform.parent;
    69.         if (rightOrLeft > -1) {
    70.             newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
    71.          }
    72.         else {
    73.             newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
    74.         }
    75.     }
    76. }
     

    Attached Files:

  2. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    What is the problem?
     
    krougeau likes this.
  3. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    To reitterate idurvesh's question; what is it you're trying to do? What exactly is going wrong? We cannot assist you if you do not provide us with adequate information.
     
    idurvesh likes this.
  4. Jamesafk

    Jamesafk

    Joined:
    May 22, 2015
    Posts:
    9
    the problem is that when the character moves and runs is this higher than normal soil in the game
     

    Attached Files:

    • lel.png
      lel.png
      File size:
      136.5 KB
      Views:
      693
  5. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    OK, so your tiles are showing up higher than you expect? Most of the code you've posted above seems to deal with the left & right positioning (normally the X axis), so we need to determine where the tiles are getting their vertical positioning information (Y axis) and alter that to suit your needs. Since I don't know what is considered "a buddy" in your game, I'm not really sure where to start... The code posted above is for your ground tiles though, correct?
     
  6. Jamesafk

    Jamesafk

    Joined:
    May 22, 2015
    Posts:
    9
    correct Krou,
    If the code had put before floor was called forground
     
  7. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Hmmm... Well, no guarantees, but line #59 looks like it might have something to do with it.
    Code (CSharp):
    1. Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.z);
    On that line, you're setting a new Vector3, but you're only giving it x and z values, so y isn't being set at all... Perhaps try changing it to something like
    Code (CSharp):
    1. Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
    Again, not sure if this will fix it for you, but it may be part of the problem.
     
    Jamesafk likes this.
  8. Jamesafk

    Jamesafk

    Joined:
    May 22, 2015
    Posts:
    9
    Thank you for your help again, that was where the problem was! :cool::)
     

    Attached Files:

    • Thx.png
      Thx.png
      File size:
      119.1 KB
      Views:
      591
    krougeau likes this.
  9. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Awesome, glad it helped & glad you got it worked out :) Have fun!
     
    idurvesh likes this.