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. Dismiss Notice

Character Sitting on Rotated Chair (Border) in a 3D Space

Discussion in 'Scripting' started by vProjects, Mar 3, 2021.

  1. vProjects

    vProjects

    Joined:
    Jun 17, 2019
    Posts:
    2
    Hello together,:)

    within our top-down RPG game we select a chair via mouse click and let the character move to the chair. Then the character should sit down on the border in the direction that the chair is looking at. Actually the character sits in the middle of a chair within the looking direction of the chair. To identify the border position of the chair as Vector3, we add the chairs game object width to the center of the chairs position. This only works if the chair has no rotation in the world space of the game. The problem is that the rotation makes it difficult to identify the correct position. The following code tries to adress this issue:

    Vector3 centerChair = go.transform.position;
    float depth = go.GetComponent<Renderer> ().bounds.size.z;

    Vector3 borderChair=centerChair.z += depth / 2;// this is the border position

    borderChair = Quaternion.Euler(0, centerChair.transform.rotation.y, 0)*borderChair;

    player.transform.position = borderChair.position;

    Do you have an idea how to solve our problem?

    Please help us! :(:(:(
     

    Attached Files:

  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Get a local position on the chair and then convert it into world space. I don't know where the pivot of your chair model is, so I'm assuming that it's at the top since that's implied by your code.

    Code (CSharp):
    1. Bounds bounds = go.GetComponent<Renderer>().bounds;
    2.  
    3. Vector3 localSitPoint = new Vector3( 0, 0, bounds.extents.z ); // extents is size / 2
    4.  
    5. Vector3 worldSitPoint = go.transform.TransformPoint( localSitPoint );
    6.  
    7. player.transform.position = worldSitPoint;
    You will most likely want to specify an explicit sit point on chairs and store it with the object rather than calculating it, as this method would only work for stool-like chairs. Once you have a chair with arms the renderer bounds won't be aligned with the seat anymore.
     
    Last edited: Mar 3, 2021
  3. vProjects

    vProjects

    Joined:
    Jun 17, 2019
    Posts:
    2
    Hello Madgvox,

    thanks for your fast response! You helped me a lot! I have now the lower border z position of a chair. How can i now set the top (Y) position while considering the calculated Z boarder to a character? I tried the following Code:

    Vector3 localSitPoint = new Vector3( 0, bounds.size.y, bounds.extents.z );

    The problem is, that thereby the character spawns within a big distance above the object and not directly at the right corner that is marked int the attached image of the main post. Madgvox do you know how to do that, or somebody else? Furthermore it seems that bounds.size.y not returns the correct height of an chair after scaling y orientation.

    It seems that the fixed sit point is the best solution! Do you know how character with different size can sit on a chair by using the same point? Or would you create for each character size a different sitting point?

    Greetings
     
    Last edited: Mar 4, 2021