Search Unity

SOLVED: Create object in front of player

Discussion in 'Scripting' started by GreedyCorporationGames, Jun 15, 2018.

  1. GreedyCorporationGames

    GreedyCorporationGames

    Joined:
    Apr 20, 2017
    Posts:
    24
    I'm trying to create a spell that spawns a portal in front of the player without getting stuck inside any objects.

    Currently I'm using a boxcast the same size as the portal to check for collisions. When a collision is detected I want to spawn the portal slightly before it collides.

    The problem I'm having is off setting the portal position from the players position by the hit distance. I can do it using vector3 but that doesn't account for rotation, so the portal spawns to the left of the player instead of in front. I've tried using transform.forward but can't figure out how to manipulate it.

    Boxcast Script
    Code (CSharp):
    1. Vector3 fwd = playerCamera.TransformDirection(Vector3.forward);
    2.  
    3.             extents = new Vector3(1.5f, 1, 0);
    4.             castAdjust = new Vector3(0, 1.5f, 1f);
    5.  
    6.  
    7.             if (Physics.BoxCast(player.transform.position + castAdjust, extents, fwd, out hit, player.transform.rotation, range))
    8.             {
    9.                 Debug.Log("TEST");
    10.                 gameManagerMaster.CallEventPortalPlacement(hit.point, hit.transform, hit.distance);
    11.             }
    Portal Position Script
    Code (CSharp):
    1. void ActivatePortal(Vector3 hitPosition, Transform hitTransform, float hitDistance)
    2.         {
    3.             portalA.SetActive(true);
    4.             portalB.SetActive(true);
    5.             goCameraA.SetActive(true);
    6.             goCameraB.SetActive(true);
    7.  
    8.             portalAdjust = new Vector3(0, 0, hitDistance);
    9.             portalA.transform.rotation = player.rotation;
    10.             portalA.transform.position = player.position + portalAdjust;
    11.         }
    Any help/suggestions would be greatly appreciated

    Thanks
     
    Last edited: Jun 16, 2018
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  3. GreedyCorporationGames

    GreedyCorporationGames

    Joined:
    Apr 20, 2017
    Posts:
    24
    Not sure how that would help, my problem is position not rotation. I just mentioned rotation because that changed where the vector3 spawned the portal when I was attempting that method.

    Thanks for replying though.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Sorry I read through too fast and got mistake from this "I can do it using vector3 but that doesn't account for rotation"

    If you know the distance, you should be able to place the object with something like this I would think
    Code (csharp):
    1. Vector3 newObjectLocation = (player.transform.forward * distance) + player.transform.position;
     
  5. GreedyCorporationGames

    GreedyCorporationGames

    Joined:
    Apr 20, 2017
    Posts:
    24
    That worked!

    Thank you very much this was a big help.

    P.S Not sure how to mark this thread as solved so I just changed the title.
     
    Joe-Censored likes this.