- Home /
Unity Movement
Hello, just getting back into Unity and enjoying the new Input system. Messing around trying to make a 3D platformer. However, when I move the analog stick forward it always changes the Y axis on rotation to 0. I want to know if maybe Cinemachine has something built in or do I have to write a script that would change where forward is depending on where the camera is facing. I don't know if I explained that well enough but thank you. Any help would be appreciated. I commented the movement script below.
public class characterMovement : MonoBehaviour { Animator animator;
int isWalkingHash;
int isRunningHash;
PlayerInput input;
Vector2 currentMovement;
bool movementPressed;
bool runPressed;
Vector2 cameraMovement;
private void Awake()
{
input = new PlayerInput();
input.CharacterControls.Movement.performed += ctx =>
{
currentMovement = ctx.ReadValue<Vector2>();
movementPressed = currentMovement.x != 0 || currentMovement.y != 0;
};
input.CharacterControls.Camera.performed += ctx => Debug.Log(ctx.ReadValueAsObject());
input.CharacterControls.Run.performed += ctx => runPressed = ctx.ReadValueAsButton();
}
void Start()
{
animator = GetComponent<Animator>();
isWalkingHash = Animator.StringToHash("isWalking");
isRunningHash = Animator.StringToHash("isRunning");
}
// Update is called once per frame
void Update()
{
handleMovement();
handleRotation();
}
void handleRotation()
{
Vector3 currentPosition = transform.position;
Vector3 newPosition = new Vector3(currentMovement.x, 0, currentMovement.y);
Vector3 positionToLookAt = currentPosition + newPosition;
transform.LookAt(positionToLookAt);
}
void handleMovement()
{
bool isRunning = animator.GetBool(isRunningHash);
bool isWalking = animator.GetBool(isWalkingHash);
if(movementPressed && !isWalking)
{
animator.SetBool(isWalkingHash, true);
}
if (!movementPressed && isWalking)
{
animator.SetBool(isWalkingHash, false);
}
if((movementPressed && runPressed )&& !isRunning)
{
animator.SetBool(isRunningHash, true);
}
if((!movementPressed || !runPressed) && isRunning){
animator.SetBool(isRunningHash, false);
}
}
void handleCameraMovement()
{
}
void OnEnable()
{
input.CharacterControls.Enable();
}
void OnDisable()
{
input.CharacterControls.Disable();
}
}
Answer by EvStg · Jun 03, 2021 at 08:44 AM
Hi,
Are you using any virtual camera with cinemachine ? The problem could be here.
Also, I'm not sure that you set up correctly the camera in the script, maybe try to use the Find() function in the Start or Awake function to use the camera.
Hope this can help you !