Search Unity

Question tilting player to left and right( by moving mouse X) and after a tilt to L or R the player automatic

Discussion in 'Scripting' started by Elvise, Dec 12, 2020.

  1. Elvise

    Elvise

    Joined:
    May 26, 2017
    Posts:
    6
    Hello everyone, I try to reach the movement mechanism of the join clash game
    I wrote a code but it's not the thing that I try to achieve. it would be great if you help me or guild me. thank you in advance



    Code (CSharp):
    1. using UnityEngine;
    2. public class test : MonoBehaviour
    3. {
    4.     //********************************
    5.     public bool MoveByTouch;
    6.     private Vector3 _mouseStartPos,PlayerStartPos;
    7.     [Range(0f,100f)] public float maxAcceleration;
    8.     private Vector3 move,direction;
    9.     public Transform target; // the player will look at this object, which is in front of it
    10.    
    11.     void Start()
    12.     {
    13.         maxAcceleration = 10f;
    14.     }
    15.    
    16.     void Update()
    17.     {
    18.         if (Input.GetMouseButtonDown(0))
    19.         {
    20.             Plane plane = new Plane(Vector3.up, 0f);
    21.        
    22.             float Distance;
    23.        
    24.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    25.        
    26.             if (plane.Raycast(ray, out Distance))
    27.             {
    28.                 _mouseStartPos = ray.GetPoint(Distance);
    29.                 PlayerStartPos = transform.position;
    30.             }
    31.            
    32.             MoveByTouch = true;
    33.        
    34.         }
    35.         else if (Input.GetMouseButtonUp(0))
    36.         {
    37.             MoveByTouch = false;
    38.         }
    39.        
    40.         if (MoveByTouch)
    41.         {
    42.             Plane plane = new Plane(Vector3.up, 0f);
    43.             float Distance;
    44.        
    45.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    46.            
    47.        
    48.             if (plane.Raycast(ray, out Distance))
    49.             {
    50.                 var newray = ray.GetPoint(Distance);
    51.                 move = newray - _mouseStartPos;
    52.                 var controller = PlayerStartPos + move;
    53.                
    54.                 controller.x = Mathf.Clamp(controller.x, -8f, 8f);
    55.                 var TargetNewPos = target.position;
    56.                
    57.                 TargetNewPos.x = Mathf.MoveTowards(TargetNewPos.x,controller.x , 80f * Time.deltaTime);
    58.                 TargetNewPos.z = Mathf.MoveTowards(TargetNewPos.z, 1000f, 10f * Time.deltaTime);
    59.        
    60.                 target.position = TargetNewPos;
    61.        
    62.                 var PlayerNewPos = transform.position;
    63.        
    64.                 PlayerNewPos.x = Mathf.MoveTowards(PlayerNewPos.x, controller.x, maxAcceleration * Time.deltaTime);
    65.                 PlayerNewPos.z = Mathf.MoveTowards(PlayerNewPos.z, 1000f,  10f * Time.deltaTime);
    66.                 transform.position = PlayerNewPos;
    67.        
    68.                 for (int i = 0; i < transform.childCount; i++)
    69.                 {
    70.                  
    71.                     Vector3 relativePos = target.position - transform.GetChild(i).position;
    72.                     Quaternion rotation = Quaternion.LookRotation(relativePos, direction);
    73.            
    74.                     transform.GetChild(i).rotation = rotation;
    75.                 }
    76.        
    77.             }
    78.         }
    79.        
    80.         else
    81.         {
    82.             MakePlayerlookforward();
    83.         }
    84.     }
    85.     void MakePlayerlookforward()
    86.     {
    87.         for (int i = 0; i < transform.childCount; i++)
    88.          transform.GetChild(i).rotation = Quaternion.Slerp(transform.GetChild(i).rotation,Quaternion.identity, 10f * Time.deltaTime);
    89.     }
    90. }
    91.  
    92.  
     
  2. Elvise

    Elvise

    Joined:
    May 26, 2017
    Posts:
    6
    about the code, there is a target that the player always looks at it but not chase. I don't know how to explain it clear please watch the video and you'll find out in general, if the left mouse is down and the player translates to left or right it should tilt a little bit and automatically after a moments get back to a normal rotation that is quaternion.identity