- Home /
How Can I Rotate My 2D Top-Down Character Rotation With Mobile Joystick?
In my project; my character LookAt to mouse position. But I want to change it with Horizontal - Vertical axes Joystick.
Here is the code;
 using UnityEngine;
 using System.Collections;
 [ExecuteInEditMode]
 public class MovementController : MonoBehaviour {
 
     //Private fields
     CharacterController cc; //Reference to the character controller component
     Transform legMesh; //The object that has the lower body
     
     CombatController cControl; //Combat control reference
     BodyController bControl; //Body control reference
     [HideInInspector]
     public Transform rotationMesh; //Which object to rotate to face the mouse
     [Header("Floats")]
     public float moveSpeed = 2f; //Movement speed 
     public float cameraSpeed; //Camera speed
     [Header("Misc")]
     public Camera rotationalCamera; //Which camera does move when moving
     public Rigidbody rigidCollider; //The collider for physics
     public bool canControl; //Can the player be controlled
     public GameObject inTrigger; //In which trigger is the player
     [Header("Camera Shake")]
     public float shake  = 0f; //Camera shake ammount
     public float shakeAmount = 0.7f; //Camera shake time
     public float decreaseFactor = 1.0f; //how much will the shake decrease
 
 
     void Start()
     {
         //Get all the references
         GameController.playerLife = this.gameObject.GetComponent<LifeController>();
         cc = GetComponent<CharacterController>();
         rotationMesh = transform.GetChild(0).GetChild(0);
         legMesh = transform.GetChild(0).GetChild(1);
         Camera.main.transparencySortMode = TransparencySortMode.Orthographic;
         cControl = GetComponent<CombatController>();
         bControl = transform.GetChild(0).gameObject.GetComponent<BodyController>();
     }
     public void PickupControl()
     {
         //If the player has anything but the fists, throw that weapon
         if(cControl.currentWeapon != 0)
         {
             cControl.ThrowWeapon();
         }
         //If player is in a trigger, pickup a weapon
         if (inTrigger != null)
         {
             if (Vector3.Distance(transform.position, inTrigger.transform.position) <= 1.5f)
             {
                 GameObject realTrigger = inTrigger;
                 cControl.currentAmmo = inTrigger.GetComponent<PickupPhysics>().ammo;
                 cControl.ChangeWeapon(inTrigger.GetComponent<PickupPhysics>().weaponID);
                 if (inTrigger = realTrigger) inTrigger = null;
                 GameController.RemoveWeapon(realTrigger);
                 Destroy(realTrigger);
             }
         }
     }
     //When player enters a trigger, mark it as inside
     void OnTriggerEnter(Collider col)
     {
         inTrigger = col.GetComponent<Collider>().gameObject;
     }
     void Update()
     {
         //Make sure the parent object always is named "PLAYER"
         transform.parent.gameObject.name = "PLAYER";
         if (canControl)
         {
             //CombatControl
             if (Input.GetMouseButtonDown(0)) cControl.Shoot();
             if (Input.GetMouseButtonDown(1)) PickupControl();
             //Look at location
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
                 rotationMesh.LookAt(new Vector3(hit.point.x, transform.position.y, hit.point.z));
             //Actual Movement
             float vertical = Input.GetAxisRaw("Vertical") * moveSpeed;
             if (vertical < -0.1f) rotationalCamera.transform.localRotation = Quaternion.Slerp(rotationalCamera.transform.localRotation, Quaternion.Euler(rotationalCamera.transform.localEulerAngles.x, 3f, rotationalCamera.transform.localEulerAngles.z), Time.deltaTime * cameraSpeed);
             if (vertical > 0.1f) rotationalCamera.transform.localRotation = Quaternion.Slerp(rotationalCamera.transform.localRotation, Quaternion.Euler(rotationalCamera.transform.localEulerAngles.x, -3f, rotationalCamera.transform.localEulerAngles.z), Time.deltaTime * cameraSpeed);
             float horizontal = Input.GetAxisRaw("Horizontal") * moveSpeed;
             if (horizontal > 0.1f) rotationalCamera.transform.localRotation = Quaternion.Slerp(rotationalCamera.transform.localRotation, Quaternion.Euler(rotationalCamera.transform.localEulerAngles.x, -3f, rotationalCamera.transform.localEulerAngles.z), Time.deltaTime * cameraSpeed);
             if (horizontal < -0.1f) rotationalCamera.transform.localRotation = Quaternion.Slerp(rotationalCamera.transform.localRotation, Quaternion.Euler(rotationalCamera.transform.localEulerAngles.x, 3f, rotationalCamera.transform.localEulerAngles.z), Time.deltaTime * cameraSpeed);
             Vector3 speed = new Vector3(horizontal, 0f, vertical);
             speed = transform.rotation * speed;
             speed.Normalize();
             cc.Move(speed * Time.deltaTime * moveSpeed);
             transform.position = new Vector3(transform.position.x, 0.58f, transform.position.z);
             //Apply rotation and animation
             if (speed != Vector3.zero)
             {
                 Quaternion rotation = legMesh.rotation;
                 bControl.isMoving = true;
                 rotation.SetLookRotation(speed);
                 legMesh.rotation = Quaternion.Euler(90f, 90f + rotation.eulerAngles.y, rotation.eulerAngles.z);
             }
             else bControl.isMoving = false;
         }
         //Apply Camera Shake
         if (shake > 0f)
         {
             Camera.main.transform.localPosition = Random.insideUnitSphere * shakeAmount;
             Camera.main.transform.localPosition = new Vector3(Camera.main.transform.localPosition.x, 10f, Camera.main.transform.localPosition.z);
             shake -= Time.deltaTime * decreaseFactor;
         }
         else shake = 0.0f;
     }
 }
 
In my opinion change this section;
 //Look at location
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
                 rotationMesh.LookAt(new Vector3(hit.point.x, transform.position.y, hit.point.z));
But when i change " rotationMesh.LookAt(new Vector3(hit.point.x, transform.position.y, hit.point.z)); " with this ; " rotationMesh.LookAt(new Vector3(CFInput.GetAxis ("Horizontal"), 0f, CFInput.GetAxis ("Vertical"))); " But this time my character look at directly in-scene joystick gameObject.
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                