Question by
leodutoit38 · Aug 17, 2018 at 11:31 AM ·
c#
Need help with joystick turning
In my game I am using a joystick to determine where the player moves to, the issue is; I can't figure out how to make him turn in the direction that the joystick faces. At the moment he just looks in one direction and moves backwards, upwards, downwards and forwards if you move the joystick, but I'd like him to turn left if you move the joystick left and right if you turn it right. It be greatly appreciated if anyone could help me out.
My code:
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveForce = 5, boostMultiplier = 2;
Rigidbody2D myBody;
// Use this for initialization
void Start () {
myBody = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate() {
Vector2 moveVec = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical")) * moveForce;
bool isSprinting = CrossPlatformInputManager.GetButton("Jump");
Debug.Log(isSprinting ? boostMultiplier : 1);
myBody.AddForce(moveVec * (isSprinting ? boostMultiplier : 1));
}
}
Note: I used the CrossPlatformInputManager default joystick.
Comment
Hi @leodutoit38 - Did you by any chance happen to google for it? At least if I do a search for "Unity make character face joystick direction" I get a good assortment of different solutions / answers...