Search Unity

Cinemachine - CharacterController ?

Discussion in 'Cinemachine' started by DizzyDwarf12, Jun 3, 2019.

  1. DizzyDwarf12

    DizzyDwarf12

    Joined:
    Mar 24, 2019
    Posts:
    9
    Hey guys,
    Im trying myself on CM and imported the FreeLookCamera.I also wrote the basic script to move around with a Charactercontroller.

    My question now is:
    How do i set it up so my character goes forward into the direction im looking? I made 1 Script from scratch b4 for that but it didnt go perfectly and idk what to do and also cant find it anymore.

    PS: Otherwise i absolutely love the tool!

    Thank you so much for your help allready!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    In CinemachineExamples you'll find a script called PlayerMove.cs that does what you're looking for.
     
  3. DizzyDwarf12

    DizzyDwarf12

    Joined:
    Mar 24, 2019
    Posts:
    9
    Thanks, actually pretty obious how it works now that i see it. But i have another question: How do i approach the issue of a smooth turn instead of the snapping that happens when i turn the camera and go forward?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    It's up to you to move the player appropriately in response to user input. PlayerMove just snaps it, you can lerp the direction more gradually.

    Try googling unity third person controller. Lots of examples and tutorials out there.
     
  5. DizzyDwarf12

    DizzyDwarf12

    Joined:
    Mar 24, 2019
    Posts:
    9
    Lerp are you sure? I got better results with slerp so far since its made specifically or rotations
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    yeah, slerp. That's what I meant. Sorry.
     
  7. DizzyDwarf12

    DizzyDwarf12

    Joined:
    Mar 24, 2019
    Posts:
    9
    I modifyed it and it seems to be working but somethings still off it feels like. Is there a better way to do it?

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Events;
    4.  
    5. public class PlayerMove : MonoBehaviour
    6. {
    7.     public float speed = 5;
    8.     public bool worldDirection;
    9.     public bool rotatePlayer = true;
    10.  
    11.     public Action spaceAction;
    12.     public Action enterAction;
    13.  
    14.     public float rotationSpeed;
    15.     public GameObject player;
    16.  
    17.     private Vector3 movedirection;
    18.  
    19.     void Update()
    20.     {
    21.  
    22.         movedirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
    23.         Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    24.         if (input.magnitude > 0)
    25.         {
    26.             Vector3 fwd = worldDirection
    27.                 ? Vector3.forward : transform.position - Camera.main.transform.position;
    28.             fwd.y = 0;
    29.             fwd = fwd.normalized;
    30.             if (fwd.magnitude > 0.001f)
    31.             {
    32.                 Quaternion inputFrame = Quaternion.LookRotation(fwd, Vector3.up);
    33.                 input = inputFrame * input;
    34.                 if (input.magnitude > 0.001f)
    35.                 {
    36.                     transform.position += input * (speed * Time.deltaTime);
    37.                     if (rotatePlayer)
    38.                         transform.rotation = Quaternion.LookRotation(input.normalized, Vector3.up); //rotation smoothness
    39.                         Quaternion newRotation = Quaternion.LookRotation(new Vector3(movedirection.x, 0f, movedirection.z));
    40.                         transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, rotationSpeed * Time.deltaTime);
    41.                 }
    42.             }
    43.         }
    44.         if (Input.GetKeyDown(KeyCode.Space) && spaceAction != null)
    45.             spaceAction();
    46.         if (Input.GetKeyDown(KeyCode.Return) && enterAction != null)
    47.             enterAction();
    48.     }
    49. }
    50.  
    Edit: i noticed abug that when i move left or right my player spinns like crazy now .. damn
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    what he said