Search Unity

Player transform needs to update

Discussion in 'Scripting' started by San_Holo, Nov 21, 2014.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Hi, I'm trying to update my player transform to hold it to my min/max screen boundaries, no errors and in the inspector the transform position gets updated or the variable rather, but not the players transform and so the player continues to move on the x axis regardless.. the code is below, can you spot what I'm missing? it's gotta e something simple Thanks dudes

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.         public float speed = 1.0f;
    8.         private float minX = -9.0f;
    9.         private float maxX = 9.0f;
    10.         private SceneManager_GameScene _scManager = null;
    11.         public Vector3 playerPos;
    12.    
    13.         void Start ()
    14.         {
    15.                 _scManager = SceneManager_GameScene.instance;
    16.                 playerPos = transform.position;
    17.         }
    18.  
    19.         void Update ()
    20.         {
    21.                 if (_scManager != null && _scManager.levelState != LevelState.Playing)
    22.                         return;
    23.  
    24.                 transform.position += new Vector3 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"), 0.0f) * speed * Time.deltaTime;
    25.  
    26.                 if (transform.position.x <= minX || transform.position.x >= maxX) {
    27.                         float xPos = Mathf.Clamp (transform.position.x, minX + 0.1f, maxX);
    28.                         playerPos = new Vector3 (xPos, playerPos.y, 0);
    29.                 }
    30.         }
    31. }
     
  2. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Code (csharp):
    1. playerPos = new Vector3 (xPos, playerPos.y, 0);
    should be just the current transform:

    Code (csharp):
    1. transform.position = new Vector3 (xPos, playerPos.y, 0);
    sorted!
     
  3. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    OK, here's a thing, I can't check my z transform to stop my player, the code below has no errors, it works on the x axis and my 2D shooter needs to check the z axis to create a top & bottom player boundary, but what ever I try here I just can't seem to put a check in place and do anything, nothing happens, I can see my players z transform going up & down in the inspector and I thought I could just re-jig the code for the working x-axis and I'm there, but I can't lock it down, I placed a debug log in there just to see, I've changed values, my z just go's from -9 to plus 9 and from top to bottom and nope, I dunno... perhaps it's my code, see below...

    is there another way to check my vertical axis? or at least do something with my z, please advise

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.         public float speed = 1.0f;
    7.         private float minX = -9.0f;
    8.         private float maxX = 9.0f;
    9.         private float minZ = -9.0f;
    10.         private float maxZ = 9.0f;
    11.         private SceneManager_GameScene _scManager = null;
    12.    
    13.         void Start ()
    14.         {
    15.                 _scManager = SceneManager_GameScene.instance;
    16.         }
    17.  
    18.         void Update ()
    19.         {
    20.                 if (_scManager != null && _scManager.levelState != LevelState.Playing)
    21.                         return;
    22.  
    23.                 transform.position += new Vector3 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"), 0.0f) * speed * Time.deltaTime;
    24.  
    25.                 if (transform.position.x <= minX || transform.position.x >= maxX) {
    26.                         float xPos = Mathf.Clamp (transform.position.x, minX + 0.1f, maxX);
    27.                         transform.position = new Vector3 (xPos, transform.position.y, transform.position.z);
    28.                 }
    29.                 if (transform.position.z <= minZ || transform.position.z >= maxZ) {
    30.                         float zPos = Mathf.Clamp (transform.position.z, minZ + 0.1f, maxZ);
    31.                         transform.position = new Vector3 (transform.position.x, transform.position.y, zPos);
    32.                 }
    33.         }
    34. }
     
  4. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    the value of my z axis keeps changing, it must be somthing in a scroll scrip, not sure, either way my z decreases slowly by 1 everytime on its own and it's gotta be that, I can see my x axis is locked and steady and so I'm able to do somthing, but because I'm looking top down with the z from top to bottom and I'm scrolling through the world then it's not the best way to lock my player, I should look for another way or try and lock this down.
     
  5. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Code (csharp):
    1. localPosition
    :) did the trick..

    I was testing using the code below and I now have a stable set of numbers to work with using that 'localposition' because I was slowly moving through space on another axis... yada yada anyhew you know

    Code (csharp):
    1. if (transform.localPosition.z <= minZ || transform.localPosition.z >= maxZ) {
    2.                         print (transform.localPosition.z);
    3.                         float zPos = Mathf.Clamp (transform.localPosition.z, minZ + 0.1f, maxZ);
    4.                         transform.localPosition = new Vector3 (transform.localPosition.x, transform.localPosition.y, zPos);
    5.                 }
    6.                 print (transform.localPosition.z);