Search Unity

Adding a curvature to the Movement of A Player when moving horizontally

Discussion in 'Scripting' started by horotv, Oct 10, 2018.

  1. horotv

    horotv

    Joined:
    Oct 10, 2018
    Posts:
    1
    Hey. I'm a pretty big noob when it comes to unity.
    I'm self taught when it comes to programming, I finally got myself to work on a game, or better said a character controller.

    I wanted to mimic the controlls of a game called Nier:Automata, atleast the basic feel of moving around and how the camera behaves. So far I've been able to move the character in relation to the camera.

    I calculate the angle this way:
    Code (CSharp):
    1. void Update () {
    2.  
    3.         Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    4.         input.Normalize();
    5.  
    6.         if (input != Vector2.zero)
    7.         {
    8.             float targetRotation = Mathf.Atan2(input.x, input.y) * Mathf.Rad2Deg + cameraBase.transform.eulerAngles.y;
    9.             targetRotation = Mathf.SmoothDampAngle(transform.rotation.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
    10.  
    11.             transform.rotation = Quaternion.Euler(0f, targetRotation, 0f);
    12.         }
    13.      
    14.         bool running = Input.GetKey(KeyCode.LeftShift);
    15.         float speed = ((running) ? runSpeed : walkSpeed) * input.magnitude;
    16.         rigidbody.MovePosition(transform.position + transform.forward * speed * Time.deltaTime);
    17.      
    18.     }
    As you can see I just move the character by turning him in the apropriate direction and then adding a forward force.

    This however has one major difference looking at my inspiration Nier:Automata - When you move horizontally in my "game" the character just moves on the horizontal axis. However in Nier:Automata, the Character runs in a big circle.

    My camera is focused on the character by just this code

    transform.position = target.position - offset * currentZoom;


    I can rotate the camera around the character with this code
    Code (CSharp):
    1.  
    2.         mouseX = Input.GetAxis("Mouse X");
    3.         mouseY = Input.GetAxis("Mouse Y");
    4.  
    5.         inputX = Input.GetAxis("Joystick Horizontal");
    6.         inputY = Input.GetAxis("Joystick Vertical");
    7.  
    8.         rotationY += (mouseX + inputX) * inputSensitivity * Time.deltaTime;
    9.         rotationX += (mouseY + inputY) * inputSensitivity * Time.deltaTime;
    10.  
    11.         rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
    12.  
    13.         Quaternion localRotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
    14.         transform.rotation = Quaternion.Lerp(transform.rotation, localRotation, smoothRotation);
    So do you have any idea on how I can rotate the character like in nier:automata in my script?
     
    Last edited: Oct 10, 2018
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Took a glimpse, you need to gradually change your rotation I think.
    Code (CSharp):
    1. //new variable
    2. public float rotaPerSec;//in degrees
    3.  
    4. //within your rotation "if" statement
    5. //transform.rotation = Quaternion.Quler(...)//replaced with:
    6. transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotaPerSec * Time.deltaTime);