Search Unity

Move Character Controller like transform.translate?

Discussion in 'Scripting' started by RxGian, Apr 3, 2019.

  1. RxGian

    RxGian

    Joined:
    Mar 21, 2018
    Posts:
    70
    I want to make third person shooter with look camera direction (ex: pubg, resident evil 5). But character controller move method is using global Vector. Can I convert it to Local Vector? Here's my code:

    Code (CSharp):
    1. public void Move(Vector3 movement) //movement vector obtained from global direction from camera face, so the player will move forward depends on camera position
    2.     {
    3.         Debug.Log("is this global :?" +movement); //because it is global, move forward is not always (0,0,1)
    4.         if (inFloor) // check if character standing on the floor
    5.         {
    6.             characterController.SimpleMove(movement * Time.deltaTime * moveSpeed); // it's move based global vector
    7.         }
    8.         if (anim != null) // this is checking animation
    9.         {
    10.             LocalDirection= this.transform.InverseTransformDirection(movement); //I want to convert it to local just like translation method
    11.             Debug.Log("is this local ?: "+LocalDirection); //printed vector is no different from global
    12.             anim.SetFloat("Vertical", LocalDirection.z); // this should be value of local vector forward
    13.             anim.SetFloat("Horizontal", LocalDirection.x); //this also same, but horizontal so character play strafe animation
    14.         }
    15.  
    16.         if (movement.magnitude > 0) // i have no idea how to make this work only for turning, but not strafing
    17.         {
    18.             newDirection = Quaternion.LookRotation(movement);
    19.             transform.rotation = Quaternion.Slerp(transform.rotation, newDirection, Time.deltaTime * turnSpeed);
    20.         }
    21.     }
    Maybe I should change everything to transform.translate but is that a good idea? so I don't need character controller components?
     
  2. RxGian

    RxGian

    Joined:
    Mar 21, 2018
    Posts:
    70
    Oh boy, I think I've reached problems that huge developer struggle.
     
  3. awprojectstudio

    awprojectstudio

    Joined:
    Aug 31, 2018
    Posts:
    1
    The character controller will always move on global vectors. But you can convert to local.

    Make a new variable to hold your movement as if it were a local variable, then, when you call move, give it the following:

    Character.Move(transform.right * move.x + transform.up * move .y + transform.forward * move.z);

    This assumes your move vector already has your speed and time.deltaTime factored in and simply converts it to local by multiplying each axis by a local direction. The result is a vector in world space, but one that's defined by your character's local space. This should allow you to treat Move as if it were being passed to transform.translate.

    Also, I wouldn't advise just using translation movements because they don't respect collisions, and the character controller has several useful functions for movement like being able to tell how steep a surface is with onControllerColliderHit, as well as the ever important isGrounded bool.
     
    RxGian likes this.
  4. RxGian

    RxGian

    Joined:
    Mar 21, 2018
    Posts:
    70
    It was few months ago but thanks for headings up! I appreciate it. I can use Space.self and it's good for diagonal 3D space i guess
     
  5. nikolasrojas

    nikolasrojas

    Joined:
    Dec 3, 2019
    Posts:
    1
    hi, how did you used that space self?
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,892
    Subtract your character controller's transform.position, I think.
    Oops. I didn't realize this was an old post.
     
  7. RxGian

    RxGian

    Joined:
    Mar 21, 2018
    Posts:
    70
    Yes, as awprojectstudio said. subtracting transform may useful if want to transform based specific object but not in child.

    You can do: public void Rotate(Vector3 eulers, Space relativeTo = Space.Self);