Search Unity

Rotate and transform in local space relative to the camera.

Discussion in 'Scripting' started by yellowelephant, Aug 8, 2014.

  1. yellowelephant

    yellowelephant

    Joined:
    Aug 7, 2014
    Posts:
    7
    I've got a problem with a controller i'm trying to code.
    The idea is, that the object faces and moves in the direction the joystick is pointing.
    It works as long as there is no camera movement/rotation because the whole movement of the controller is oriented at world space. I have no idea how to change that and need help.

    This is my code so far.

    Code (JavaScript):
    1. if (left_horizontal != 0.0 || left_vertical != 0.0) {
    2. var angle = Mathf.Atan2(-left_vertical, left_horizontal) * Mathf.Rad2Deg;
    3. transform.rotation = (Quaternion.Lerp (heroTransform.rotation, Quaternion.AngleAxis(90.0 - angle, Vector3.up), Time.time * (force * moveSpeed)));
    4. controller.Move(transform.forward * force * moveSpeed);
    5. }