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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

transform.Translate but with raycast distance

Discussion in 'Scripting' started by RobertOne, Sep 26, 2022.

  1. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    205
    Hi.
    i wanna move a point using transform Translate:

    Code (CSharp):
    1.  
    2. _objectToPlaceRoot.transform.Translate(_camera.transform.right * _input.x, Space.World);
    3. _objectToPlaceRoot.transform.Translate(Vector3.up * _input.y, Space.World);
    4.  
    but also changing its Y position (height) based on a raycast at the same time

    Code (CSharp):
    1.  
    2. if (Physics.Raycast(from, dir, out RaycastHit hit, Mathf.Infinity, _treeLayer))
    3. {    
    4.   hitpoint = hit.point;
    5. }
    6.  
    i thought i can simply do it this way after the transform translate code
    Code (CSharp):
    1.  _objectToPlaceRoot.transform.position = new Vector3(_objectToPlaceRoot.transform.position.x, hitpoint.y, _objectToPlaceRoot.transform.position.z);
    but the _objectToPlaceroot is just somewhere else in the world now.
    how am i supposed to do that the right way?
    thanks
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    2,794
    The issue here is Transform.Translate takes a direction, while Transform.position takes a... position.

    So you need to make both play nice just one one or the other. The easiest at a glance at your code would be to work with positions.

    Generally it's best to build a vector and use it right at the end. So you can first get the horizontal position offset from input (current position + input), the vertical position from your raycast, put those together and set the transform's position to that.
     
    TheDevloper and RobertOne like this.
  3. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    205
    hey, thanks, i tried to do it this way now:

    Code (CSharp):
    1.  
    2. Vector3 right = _camera.transform.right * _input.x;
    3. Vector3 up = _camera.transform.up * _input.y;
    4.              
    5. Vector3 forward = Vector3.zero;
    6. _objectToPlaceRoot.transform.position += right + up + forward;
    7.  
    so that works just like transform translate for moving left/right/up down

    but i dont really know what to set for the forward.
    so thats where the raycast hit something, so that the forward distance should match the raycast distance
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    2,794
    What exactly is 'forward' in this context? I'm not sure what type of movement we're dealing with here.

    Screenshots may help.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    2,794
    Admittedly still trying to understand you a bit. So you can move the box up/down/left/right, but want it to remain in front of the tree based on a raycast from the camera?

    I guess that gets a little complicated. I guess you maintain a position of where the box currently is, and if you register a raycast hit, subtract the position of the hit from the box.
     
unityunity