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

Question Third persone movement underwater strange behaviour

Discussion in 'Scripting' started by Rakowu, Feb 14, 2021.

  1. Rakowu

    Rakowu

    Joined:
    Nov 15, 2017
    Posts:
    16
    Hey everyone.
    I'm trying to create a third person movement but with tha abiliy to swim up and down. (Basically a third person but underwater). It works more or less fine. The character rotates toward the camera is facin when rotating with the mouse and mioving forward. The weird stuff happens when i'm trying to go up or down (pressing space or left shift). Can anyone try to explain me what the problem is?

    Any help is apreciated :)


    upload_2021-2-14_12-38-24.png

    The set up is a 3d Capsule and this script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour{
    6.  
    7.     Rigidbody rb;
    8.     public Transform cam;
    9.     public float speed = 6f;
    10.     public float turnSmoothTime = 0.1f;
    11.     float turnSmoothVelocity;
    12.     CharacterController chC;
    13.  
    14.     float targetAngleX;
    15.     float angleX;
    16.  
    17.  
    18.     void Start(){
    19.         rb = GetComponent<Rigidbody>();
    20.         chC = GetComponent<CharacterController>();
    21.     }
    22.  
    23.     void Update(){
    24.         float horizontal = Input.GetAxisRaw("Horizontal");
    25.         float vertical = Input.GetAxisRaw("Vertical");
    26.         float up = 0f;
    27.      
    28.  
    29.         if (Input.GetKey("space")){
    30.             up = -1;
    31.         }else if( Input.GetKey("left shift")){
    32.             up = 1;
    33.         }else{
    34.             up = 0;
    35.         }
    36.  
    37.         Vector3 direction = new Vector3(horizontal, up, vertical).normalized;
    38.  
    39.         if(direction.magnitude >= 0.1f){
    40.             float targetAngleY = Mathf.Atan2(direction.x,direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    41.             float angleY = Mathf.SmoothDampAngle(transform.eulerAngles.y,targetAngleY,ref turnSmoothVelocity,turnSmoothTime);
    42.  
    43.             if(direction.y > 0 || direction.y < 0){
    44.                 targetAngleX = Mathf.Atan2(direction.y,direction.z * vertical) * Mathf.Rad2Deg;
    45.                 angleX = Mathf.SmoothDampAngle(transform.eulerAngles.x,targetAngleX,ref turnSmoothVelocity,turnSmoothTime);
    46.             }else{
    47.                 if(angleX > 0.1f || angleX < -0.1f){
    48.                     targetAngleX = 0;
    49.                     angleX = Mathf.SmoothDampAngle(transform.eulerAngles.x,targetAngleX,ref turnSmoothVelocity,turnSmoothTime);
    50.                 }
    51.             }
    52.             transform.rotation = Quaternion.Euler(angleX,angleY,0f);
    53.  
    54.             Vector3 moveDir = Quaternion.Euler(targetAngleX,targetAngleY,0f) * Vector3.forward;
    55.             rb.AddForce(moveDir.normalized * speed * Time.deltaTime);
    56.         }
    57.     }
    58. }
    59.  
     
    Last edited: Feb 16, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Nobody is going to be able to explain the problem until you can explain it to us.

    When I look above, it looks good to me.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand errors in general:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    Rakowu likes this.
  3. Rakowu

    Rakowu

    Joined:
    Nov 15, 2017
    Posts:
    16
    The thing ist that i realy try to understand (and more after your answer) but i have no clue. I only know that the strange behaviour start when moving down (pressing shift). After that sometime the character rotates well, sometimes it rotates facing upward. Sometimes up and down facing is inverted. What ever it is, it has to do with my
    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(angleX,angleY,0f);
    I guess that my vector direction is wrong. (Vector are my kryptonite)
     
  4. Rakowu

    Rakowu

    Joined:
    Nov 15, 2017
    Posts:
    16
    Btw not sure what you mean with "code tags" i'm using code tags -> [ code ]
     
  5. Rakowu

    Rakowu

    Joined:
    Nov 15, 2017
    Posts:
    16
    Ok i solved it more or less.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MovementTutorial : MonoBehaviour{
    6.    
    7.     Rigidbody rb;
    8.     public Transform cam;
    9.     public float speed = 6f;
    10.     public float turnSmoothTime = 0.1f;
    11.     float turnSmoothVelocity = 2f;
    12.     CharacterController chC;
    13.  
    14.     float targetAngleX;
    15.     float angleX;
    16.  
    17.  
    18.     void Start(){
    19.         rb = GetComponent<Rigidbody>();
    20.         chC = GetComponent<CharacterController>();
    21.     }
    22.  
    23.     void Update(){
    24.         float horizontal = Input.GetAxisRaw("Horizontal");
    25.         float vertical = Input.GetAxisRaw("Vertical");
    26.         float up = 0f;
    27.        
    28.  
    29.         if (Input.GetKey("space")){
    30.             up = -0.5f;
    31.         }else if( Input.GetKey("left shift")){
    32.             up = 0.5f;
    33.         }else{
    34.             up = 0;
    35.         }
    36.         Cursor.lockState = CursorLockMode.Confined;
    37.  
    38.         Vector3 direction = new Vector3(horizontal, up, vertical).normalized;
    39.  
    40.         if(direction.magnitude >= 0.1f){
    41.             float targetAngleY = Mathf.Atan2(direction.x,direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    42.             float angleY = Mathf.SmoothDampAngle(transform.eulerAngles.y,targetAngleY,ref turnSmoothVelocity,turnSmoothTime);
    43.  
    44.             /* if(direction.y > 0 || direction.y < 0){
    45.                 targetAngleX = Mathf.Atan2(direction.y,direction.z * vertical) * Mathf.Rad2Deg;
    46.                 angleX = Mathf.SmoothDampAngle(transform.eulerAngles.x,targetAngleX,ref turnSmoothVelocity,turnSmoothTime);
    47.             }else{ */
    48.             targetAngleX = Mathf.Atan2(direction.y,direction.z * vertical) * Mathf.Rad2Deg;
    49.             angleX = Mathf.SmoothDampAngle(transform.eulerAngles.x,targetAngleX,ref turnSmoothVelocity,turnSmoothTime);
    50.        
    51.             if(angleX > 0.1f || angleX < -0.1f){
    52.                 /* targetAngleX = 0; */
    53.                 angleX = Mathf.SmoothDampAngle(transform.eulerAngles.x,targetAngleX,ref turnSmoothVelocity,turnSmoothTime);
    54.             }
    55.             /* } */
    56.             transform.rotation = Quaternion.Euler(angleX,angleY,0f);
    57.  
    58.             Vector3 moveDir = Quaternion.Euler(targetAngleX,targetAngleY,0f) * Vector3.forward;
    59.             rb.AddForce(moveDir.normalized * speed * Time.deltaTime);
    60.         }
    61.     }
    62. }
    63.  
    But one question. Do you know why the cinemachine behaviour is different on fullscreen?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Perhaps because aspect ratio is different?

    You can go in the upper left corner of the Game tab in Unity and make precisely the resolution you use for fullscreen and see if it behaves the same way in the editor.
     
  7. Rakowu

    Rakowu

    Joined:
    Nov 15, 2017
    Posts:
    16
    Hey Kurt, thanks for your reply :) I found a solution.