Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how can I tilt the player to the left and right?

Discussion in 'Scripting' started by Elvise, Oct 17, 2020.

  1. Elvise

    Elvise

    Joined:
    May 26, 2017
    Posts:
    6
    how can I tilt the player to the left and right?, base on mouse X and after tilting get it back to Quaternion.identity automatically
    here is my script, please help me out I stuck in it. I know the mouse down doesn't let the player get back to normal, it would be great if you give a better way to settle it.

    thanks in advance
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class moveplayer : MonoBehaviour
    4. {
    5.     [Range(0f,100f)] public float maxAcceleration;
    6.     private bool MoveByHold;
    7.     private Vector3 _mouseStartPos,PlayerStartPos;
    8.     private float ZAxisOfPlayer;
    9.     private float AlterMove;
    10.     private Vector3 move,NewPosFinishLine;
    11.     private float distance_stage_player;
    12.     private float aletMovex,exm;
    13.  
    14.    void Start()
    15.    {
    16.        maxAcceleration = 4f;
    17.        ZAxisOfPlayer = transform.position.z; // initial z axis
    18.    }
    19.  
    20.    void Update()
    21.    {
    22.        if (Input.GetMouseButtonDown(0))
    23.        {
    24.            Plane plane = new Plane(Vector3.up, 0f);
    25.  
    26.            float Distance;
    27.  
    28.            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    29.  
    30.            if (plane.Raycast(ray, out Distance))
    31.            {
    32.                _mouseStartPos = ray.GetPoint(Distance);
    33.                PlayerStartPos = transform.position;
    34.            }
    35.        
    36.            MoveByHold = true;
    37.        }
    38.        else if (Input.GetMouseButtonUp(0))
    39.        {
    40.            MoveByHold = false;
    41.        }
    42.  
    43.        if (MoveByHold)
    44.        {
    45.            Plane plane = new Plane(Vector3.up, 0f);
    46.            float Distance;
    47.            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    48.  
    49.            if (plane.Raycast(ray, out Distance))
    50.            {
    51.                Vector3 MousePos = ray.GetPoint(Distance);
    52.                move = MousePos - _mouseStartPos;
    53.                var controller = PlayerStartPos + move;
    54.                controller.x = Mathf.Clamp(controller.x, -5f, 5f);
    55.                controller.z = ZAxisOfPlayer;
    56.                transform.position = Vector3.Lerp(transform.position, controller, Time.deltaTime * maxAcceleration);
    57.          
    58.                  // if (move.x < 2.5f)
    59.                  // {
    60.                  //     AlterMove = Mathf.Clamp(move.x * 40f, -50f, 50f);
    61.                  //
    62.                  //     transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0f, AlterMove, 0f),
    63.                  //         Time.deltaTime * 10f);
    64.                  //
    65.                  // }
    66.                  // else
    67.                  // {
    68.                  //     BackToNormal();
    69.                  // }
    70.              
    71.              
    72.          
    73.            }
    74.  
    75.        }
    76.        else
    77.        {
    78.            BackToNormal();
    79.        }
    80.  
    81.  
    82.    }
    83.  
    84.  
    85.    void BackToNormal()
    86.    {
    87.        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.identity, Time.deltaTime * 10f);
    88.    }
    89.  
    90. }
    91.  
     
    Last edited: Oct 19, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Use a floating point value for your tilt angle.

    Have two of them:

    Code (csharp):
    1. float currentTilt;
    2. float desiredTilt;
    When you get input, ONLY set the
    desiredTilt
    to either left, right, or center (choose angle values)

    EVERY FRAME, use
    Mathf.MoveTowards()
    to move
    currentTilt
    towards
    desiredTilt
    , at whatever rate you want.

    Finally, use
    currentTile
    to produce a fresh rotation to drive your tilt:

    Code (csharp):
    1. transform.rotation = Quaternion.Euler( 0, 0, currentTilt);
     
    eses and PraetorBlue like this.
  3. Elvise

    Elvise

    Joined:
    May 26, 2017
    Posts:
    6
    thank you, but i didn't get your point i found this block of code and kina works most of the time!, but could you write an instance script ?
    this is the code that i found :

    Vector2 dir = transform.position - move ;
    float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;
    var newrotate = Quaternion.AngleAxis (angle, Vector3.up);

    transform.rotation = Quaternion.Slerp (transform.rotation, newrotate, 30f * Time.deltaTime);
     
    Last edited: Oct 18, 2020
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Elvise likes this.
  5. Elvise

    Elvise

    Joined:
    May 26, 2017
    Posts:
    6