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