Search Unity

Issue with correctly positioning player to sit on chair

Discussion in 'Scripting' started by vivianindatub, Jan 16, 2022.

  1. vivianindatub

    vivianindatub

    Joined:
    Apr 23, 2021
    Posts:
    71
    I'm having issues with correctly positioning player to sit on chair. For some reason the player rotates to face towards the sky when sitting on this orange chair. I'm using character controller for the player.
    The orange chair has a box collider and rigidbody+ gravity attached (for player to be able to drag mouse and move it for decoration)
    Attached is the script for the sitting aciton.

    Code (CSharp):
    1. public class SitOn : MonoBehaviour //attach on objects like chairs etc
    2. {
    3.     public GameObject player;
    4.     private Animator anim;
    5.     bool isWalkingTowards = false;
    6.     bool sittingOn = false;
    7.  
    8.     private void OnMouseDown()
    9.     {
    10.         if (!sittingOn)
    11.         {
    12.             anim.SetTrigger("isWalking");
    13.             isWalkingTowards = true;
    14.         }
    15.        
    16.     }
    17.  
    18.  
    19.  
    20.     void Start()
    21.     {
    22.         anim = player.GetComponent<Animator>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if (isWalkingTowards)
    29.         {
    30.             Vector3 targetDir;
    31.             targetDir = new Vector3(transform.position.x - player.transform.position.x, 0f, transform.position.z - player.transform.position.z);
    32.  
    33.             Quaternion rot = Quaternion.LookRotation(targetDir);
    34.             player.transform.rotation = Quaternion.Slerp(player.transform.rotation, rot, 0.05f);
    35.             player.transform.Translate(Vector3.forward * 0.01f);
    36.  
    37.             if(Vector3.Distance(player.transform.position, this.transform.position) < 1.0)
    38.             {
    39.                 anim.SetTrigger("isSitting");
    40.  
    41.                 //turn player around to align forward vector with object's vector aka they're facing same direction
    42.  
    43.                 player.transform.rotation = this.transform.rotation; //immediate snaps rotation
    44.                 isWalkingTowards = false;
    45.                 sittingOn = true;
    46.             }
    47.         }
    48.     }
    49. }
    50.  
     

    Attached Files:

    Last edited: Jan 16, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
  3. vivianindatub

    vivianindatub

    Joined:
    Apr 23, 2021
    Posts:
    71
    Sorry about that, I will fix the code tags. I made sure the player and the chair all have axis correctly oriented. The green axis pointing upward, blue pointing forward, red pointing sideward. It still doesn't work properly...
     

    Attached Files:

    • a.jpg
      a.jpg
      File size:
      66.4 KB
      Views:
      219
    • aa.jpg
      aa.jpg
      File size:
      133.6 KB
      Views:
      213
  4. vivianindatub

    vivianindatub

    Joined:
    Apr 23, 2021
    Posts:
    71

    Here is the script for the player movement. It works fine when walking/running.
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. //using UnityEngine.UIElements;
    6. using UnityEngine.UI;
    7.  
    8.  
    9. public class PlayerMovement : MonoBehaviour{
    10.     public static PlayerMovement Player;
    11.  
    12.     [SerializeField]
    13.     public float walkSpeed;
    14.     [SerializeField]
    15.     public float runSpeed;
    16.     [SerializeField]
    17.     private float _gravity = 9.81f;
    18.     public float rotationSpeed;
    19.  
    20.     private CharacterController controller;
    21.     private Animator animator; //for later animation use
    22.                                // Start is called before the first frame update
    23.  
    24.     private bool arrowsKeyPressed; //if any of the arrows keys are pressed
    25.    
    26.  
    27.  
    28.     enum MotionState {
    29.         Idle,
    30.         Walking,
    31.         Running
    32.     }
    33.  
    34.  
    35.  
    36.     private MotionState mState = MotionState.Idle;
    37.     private float lastUnpressed;
    38.     private float pressSpan = 1;
    39.     private bool hold;
    40.  
    41.     private void Awake(){
    42.         Player = this;
    43.     }
    44.  
    45.     void Start()
    46.     {
    47.         controller = GetComponent<CharacterController>();
    48.         animator = GetComponent<Animator>();
    49.     }
    50.  
    51.     // Update is called once per frame
    52.     void Update()
    53.     {
    54.         ArrowsKeyPressed();
    55.  
    56.         playerMoves();
    57.         PlayerData.Instance.Update();
    58.         handleAnimation();
    59.        
    60.  
    61.     }
    62.  
    63.  
    64.     void transitState(MotionState state) {
    65.         if (mState == state)
    66.             return;
    67.  
    68.         mState = state;
    69.         Debug.Log($"State transite to {mState}");
    70.         switch (state)
    71.         {
    72.             case MotionState.Idle:
    73.                 animator.SetBool("isRunning", false);
    74.                 animator.SetBool("isWalking", false);
    75.                 break;
    76.             case MotionState.Walking:
    77.                 animator.SetBool("isWalking", true);
    78.                 animator.SetBool("isRunning", false);
    79.  
    80.                 //walkSpeed = 2f;
    81.                 break;
    82.             case MotionState.Running:
    83.                 animator.SetBool("isWalking", true);
    84.                 animator.SetBool("isRunning", true);
    85.                 //walkSpeed = 6f;
    86.                 break;
    87.         }
    88.     }
    89.  
    90.  
    91.     void ArrowsKeyPressed()
    92.     {
    93.        
    94.  
    95.  
    96.         if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
    97.         {
    98.             arrowsKeyPressed = true;
    99.         }
    100.  
    101.  
    102.         else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
    103.         {
    104.             arrowsKeyPressed = true;          
    105.         }
    106.         if (Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.DownArrow) || Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))
    107.         {
    108.             arrowsKeyPressed = false;
    109.         }
    110.     }
    111.     private void playerMoves()
    112.     {
    113.  
    114.        
    115.  
    116.         float horizontalInput = Input.GetAxis("Horizontal");
    117.         float verticalInput = Input.GetAxis("Vertical");
    118.         Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
    119.         direction.Normalize();
    120.  
    121.         direction.y -= _gravity;
    122.  
    123.         //multiple by Time.deltaTime so it moves once/second. In update it moves once every frame and frame/second can be very high
    124.  
    125.         //controller.Move(direction * Time.deltaTime * walkSpeed); //multiple by Time.deltaTime so it moves once/second. In update it moves once every frame and frame/second can be very high
    126.         if (direction != Vector3.zero)
    127.         {
    128.             Quaternion toRotation = Quaternion.LookRotation(direction, Vector3.up);
    129.             transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
    130.         }
    131.  
    132.  
    133.         //walk
    134.         if (arrowsKeyPressed && !Input.GetKey(KeyCode.RightShift))
    135.         {
    136.  
    137.            
    138.          
    139.          transitState(MotionState.Walking);
    140.          controller.Move(direction * Time.deltaTime * walkSpeed); //multiple by Time.deltaTime so it moves once/second. In update it moves once every frame and frame/second can be very high
    141.  
    142.         }
    143.         else if (arrowsKeyPressed&& Input.GetKey(KeyCode.RightShift))
    144.         {
    145.  
    146.             transitState(MotionState.Running);
    147.             controller.Move(direction * Time.deltaTime * runSpeed); //multiple by Time.deltaTime so it moves once/second. In update it moves once every frame and frame/second can be very high
    148.  
    149.         }
    150.        
    151.  
    152.         else
    153.         {
    154.             transitState(MotionState.Idle);
    155.         }
    156.  
    157.     }
    158.  
    159.  
    160.  
    161.     void handleAnimation()
    162.     {
    163.         bool isWalking = animator.GetBool("isWalking");
    164.         bool isRunning = animator.GetBool("isRunning");
    165.        
    166.  
    167.     }
    168.  
    169.     public void MoveTo(Vector3 targetPoint) //simply move to a point
    170.     {
    171.         Vector3 moveVector = targetPoint - Player.transform.position;
    172.         controller.Move(moveVector);
    173.  
    174.     }
    175.  
    176.  
    177.    
    178.  
    179.  
    180.  
    181.  
    182.  
    183.  
    184.  
    185.  
    186. }
    187.  
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I think you want your line 126 to be more selective, such as:

    Code (csharp):
    1. if (direction.magnitude > 0.05f)
     
  6. vivianindatub

    vivianindatub

    Joined:
    Apr 23, 2021
    Posts:
    71
    Still same. The rotation on x axis of player rotates to -90 when performing the sitting. I checked the code anywhere and still don't understand what could be triggering that. The animation is fine though when I just create a cube as the chair.
    Update: I also tried bake axis conversion on model import settings. Still same
     

    Attached Files:

    • aaa.jpg
      aaa.jpg
      File size:
      37.9 KB
      Views:
      217
    • aaaa.jpg
      aaaa.jpg
      File size:
      104.4 KB
      Views:
      211
    Last edited: Jan 16, 2022
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Try turning off the animation next. Then rebuild it with a couple of cubes so you can tell direction, etc. Something is doing it and you have to find it. Rip tear shred! Use source control so you can instantly revert when you do find the issue.