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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Vector3 giving me 0 instead of value

Discussion in 'Editor & General Support' started by Arno1975, Sep 21, 2018.

  1. Arno1975

    Arno1975

    Joined:
    Aug 14, 2013
    Posts:
    43
    my programming skills are not yet that good and i try to debug with logs..but i am getting a 0 value when i shouldn't (i think)

    this is the script i am using.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StructurePlacement : MonoBehaviour {
    6.  
    7.  
    8.     private Transform currentStructure;
    9.     private PlaceableStructure placeableStructure;
    10.     private Camera cam;
    11.     private bool hasPlaced;
    12.     //added
    13.     private Grid grid;
    14.    // private Vector3 finalPosition;
    15.     private Vector3 tempPosition;
    16.  
    17.     public LayerMask structureMask;
    18.  
    19.     // Use this for initialization
    20.     private void Awake()
    21.     {
    22.         //get grid from other script
    23.         grid = FindObjectOfType<Grid>();
    24.         cam = Camera.main;
    25.     }
    26.  
    27.     void Start() {
    28.        
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.  
    35.         Vector3 m = Input.mousePosition;
    36.         m = new Vector3(m.x,m.y,transform.position.y);
    37.         Vector3 p = cam.ScreenToWorldPoint(m);
    38.         Debug.Log("Mouse P" + p.x + " and" + p.z);
    39.         PreviewCubeNear(p);
    40.        
    41.         Debug.Log("Mouse tempposition" + tempPosition.x + " and" + tempPosition.z);
    42.  
    43.  
    44.  
    45.         if (currentStructure != null && !hasPlaced)
    46.         {
    47.            
    48.             currentStructure.position = new Vector3(tempPosition.x, 0, tempPosition.z);
    49.             //currentStructure.position = new Vector3(p.x, 0, p.z);
    50.  
    51.  
    52.  
    53.  
    54.             if (Input.GetMouseButtonDown(0))
    55.             {
    56.                 if (IsLegalPosition())
    57.                 {
    58.                     hasPlaced = true;
    59.                 }
    60.             }
    61.         }
    62.         else
    63.         {
    64.             if (Input.GetMouseButtonDown(0))
    65.             {
    66.                 RaycastHit hit = new RaycastHit();
    67.                 Ray ray = new Ray(new Vector3(p.x, 8, p.z), Vector3.down);
    68.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity, structureMask))
    69.                 {
    70.                     Debug.Log(hit.collider.name);
    71.                     //destroy the object that is clicked
    72.                     Destroy(hit.collider.gameObject);
    73.                 }
    74.             }
    75.         }
    76.     }
    77.  
    78.     //don't place inside other structure
    79.     bool IsLegalPosition()
    80.     {
    81.         if(placeableStructure.colliders.Count>0)
    82.         {
    83.             return false;
    84.         }
    85.         return true;
    86.     }
    87.  
    88.     public void SetItem(GameObject b)
    89.     {
    90.         hasPlaced = false;
    91.    
    92.         currentStructure = ((GameObject)Instantiate(b)).transform;
    93.    
    94.         placeableStructure = currentStructure.GetComponent<PlaceableStructure>();
    95.     }
    96.  
    97.     private void PreviewCubeNear(Vector3 clickPoint)
    98.     {
    99.         //Get the grid position from the script grid
    100.         var tempPosition = grid.GetNearestPointOnGrid(clickPoint);
    101.    
    102.     }
    103.  
    104.     }
    in the Grid script the GetNearestPointOnGrid is:
    Code (CSharp):
    1. public Vector3 GetNearestPointOnGrid(Vector3 position)
    2.     {
    3.         Debug.Log("grid near" + position);
    4.         position -= transform.position;
    5.         Debug.Log("position" + position);
    6.         int xCount = Mathf.RoundToInt(position.x / sizeGable);
    7.         int yCount = Mathf.RoundToInt(position.y / sizeSide);
    8.         int zCount = Mathf.RoundToInt(position.z / sizeSide);
    9.  
    10.         Vector3 result = new Vector3(
    11.             (float)xCount * sizeGable,
    12.             (float)yCount * sizeSide,
    13.             (float)zCount * sizeSide);
    14.  
    15.         result += transform.position;
    16.         Debug.Log("result" + result);
    17.  
    18.         return result;
    19.     }
    the result in the grid script gives me an debug log for example (21.0, 3.0, 7.0)
    this result is feeded into the other script in the variable tempPosition if i am correct..but that Debug gives me 0..
    that should be the same value...or am i missing something

    so i the normal situation without the grid script..the gameobject can be placed everywhere..now i want the object to stick to the grid in worldspace.. the camera can orbit and pan so i need worldcoordinates and not the camcoordinates, that is why i use the grid on a seperate game object.
     
  2. Arno1975

    Arno1975

    Joined:
    Aug 14, 2013
    Posts:
    43
    found the problem i am declaring the variable tempposition not setting it.


    1. Code (CSharp):
      1. private void PreviewCubeNear(Vector3 clickPoint)
      2.     {
      3.         //Get the grid position from the script grid
      4.         var tempPosition = grid.GetNearestPointOnGrid(clickPoint);
      5.  
      6.     }
      the var tempPostion doesn't need the var... only tempPosition = ......