Search Unity

Questions about(this.transform.position)and(cube.transfor.position) in my script

Discussion in 'Getting Started' started by CHINA-SandWind, Feb 6, 2019.

  1. CHINA-SandWind

    CHINA-SandWind

    Joined:
    Jan 26, 2019
    Posts:
    2
    Here is my code ,It can control the <cube> find my mouse on <Unity Screen>.
    c# binding with the <Main camera>.And set the camera position is (0.0.0),cube position is (0.0.18)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ABC : MonoBehaviour
    6. {
    7.     public GameObject cube;
    8.     public int pixelWidth;
    9.     public int pixelHeight;
    10.     public Vector3 mousePosition;
    11.     public Vector3 worldPosition;
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         pixelWidth = this.GetComponent<Camera>().pixelWidth;
    23.  
    24.         pixelHeight = this.GetComponent<Camera>().pixelHeight;
    25.  
    26.         mousePosition = new Vector3(Input.mousePosition.x,Input.mousePosition.y,cube.transform.position.z);
    27.  
    28.         //mousePosition.z = this.GetComponent<Camera>().farClipPlane;//
    29.  
    30.         worldPosition = this.GetComponent<Camera>().ScreenToWorldPoint(mousePosition);
    31.  
    32.         //cube.GetComponent<Transform>().position = worldPosition;  
    33.         cube.transform.position = worldPosition;
    34.  
    35.     }
    36. }

    UP code is correct. the cube can find my mouse in screen.
    But the DOWN code is wrong .The cube can't find my mouse and it go to de worldposition(0.0.0),and didn't move.
    the difference is the 26 lines only change( cube.transform.position.z )to( this.transform.position.z)
    Code (CSharp):
    1. public class ABC : MonoBehaviour
    2. {
    3.     public GameObject cube;
    4.     public int pixelWidth;
    5.     public int pixelHeight;
    6.     public Vector3 mousePosition;
    7.     public Vector3 worldPosition;
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         pixelWidth = this.GetComponent<Camera>().pixelWidth;
    19.  
    20.         pixelHeight = this.GetComponent<Camera>().pixelHeight;
    21.  
    22.         mousePosition = new Vector3(Input.mousePosition.x,Input.mousePosition.y,this.transform.position.z);
    23.  
    24.         //mousePosition.z = this.GetComponent<Camera>().farClipPlane;
    25.  
    26.         worldPosition = this.GetComponent<Camera>().ScreenToWorldPoint(mousePosition);
    27.  
    28.         //cube.GetComponent<Transform>().position = worldPosition;  
    29.         cube.transform.position = worldPosition;
    30.  
    31.     }
    32. }

    I want to konw <this.transform.position.z>. is who's position.z?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It's the position.z of the GameObject to which that script is attached.
     
    CHINA-SandWind likes this.
  3. CHINA-SandWind

    CHINA-SandWind

    Joined:
    Jan 26, 2019
    Posts:
    2
    thanks for you reply. I make de script on the main camera then position.Z is my camera world position.z~ I konw that
    ;)
     
    JoeStrout likes this.