Search Unity

Spawning and object based on rotation of player

Discussion in 'Scripting' started by thundercat71, May 23, 2019.

  1. thundercat71

    thundercat71

    Joined:
    Sep 13, 2018
    Posts:
    10
    OK first off a little more than just rotation.
    I'm using a raycast from my players head at a 45 degrees angle down and using the hitinfo returned to decide if the object can spawn based on what is there and also what angle to spawn at.

    Now most is working bar the raycast at 45 degrees as the player rotates the 45 degrees changes., Maths not being my strong point Im more than confused by what is going on. Any help greatly appreciated.

    Here is a video showing the problem:


    And here be the code:
    Code (CSharp):
    1.     private void MoveCurrentObjectToPlayer()
    2.     {
    3.         playerPos = player.transform.position + Vector3.up * 1.8f;
    4.         playerDir = player.transform.forward;
    5.         playerDir = Quaternion.Euler(-45, 0, 0) * playerDir;
    6.         Debug.DrawRay(playerPos, playerDir, Color.yellow);
    7.      
    8.         Ray ray = new Ray(playerPos, playerDir);
    9.         RaycastHit hitInfo;
    10.         if (Physics.Raycast(ray, out hitInfo))
    11.         {
    12.             currentPlaceableObject.transform.position = hitInfo.point;
    13.             currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
    14.         }
    15.     }
     
    Last edited: May 24, 2019
  2. dontdiedevelop

    dontdiedevelop

    Joined:
    Sep 18, 2018
    Posts:
    68
    you just need to set "else" situation to Physics.Raycast :D
     
  3. thundercat71

    thundercat71

    Joined:
    Sep 13, 2018
    Posts:
    10
    I cant see how the else would fix it? Its not a spawn problem as such but the direction of the raycast isn't staying at 45 degrees downwards.
     
    Last edited: May 24, 2019