Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ahmetesmer · Apr 20, 2015 at 12:46 PM · 2drotationmovementjoystickcontrols

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Top down 2d movement without being affected by rotation 2 Answers

Joystick 2D Sprite Rotation and Movement 0 Answers

2d Movement Messes up when cube is rotatated 0 Answers

No movement to negative z-axis. 1 Answer

Basic 2D movement C# - Key presses cancel eachother out 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges