Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Jomelskie · Oct 19, 2016 at 01:05 PM · camerarotationcharactercharacter controllercharacter movement

Help for my character Rotation When using Joystick

Hi. I just want to ask help for my Character Rotation..

My Code works when i'm using keyboard input instead of Joystick..

The desired output is this: If turn the camera using swipe.. the character should go forward to the new rotation of the camera..

when i apply my joystick, it doesn't work and the character just walk all the way to its original rotation not to the new rotation of the camera..

Care to see my code? Hehe Thank you very much. Here it is. VIRTUALJS.CS using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.EventSystems;

 public class virtualJS : MonoBehaviour,  IDragHandler, IPointerUpHandler, IPointerDownHandler{
 
     public Animator anim;
     private Image bgJoy;
     private Image joystick;
     public Vector3 InputDirection { set; get;}
 
     private void Start()
     {
         bgJoy = GetComponent<Image> ();
         joystick = transform.GetChild(0).GetComponent<Image> ();
         InputDirection = Vector3.zero;
         setTransparency (0.5f);
     }
 
     private void setTransparency(float num)
     {
         Color color2 = joystick.color;
         color2.a = num;
         joystick.color = color2;
     }
     public virtual void OnDrag(PointerEventData ped)
     {
         setTransparency (0.9f);
         Vector2 pos = Vector2.zero;
         if (RectTransformUtility.ScreenPointToLocalPointInRectangle (bgJoy.rectTransform, ped.position, ped.pressEventCamera, out pos)) {
             pos.x = (pos.x / bgJoy.rectTransform.sizeDelta.x);
             pos.y = (pos.y / bgJoy.rectTransform.sizeDelta.y);
 
             float x = (bgJoy.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
             float y = (bgJoy.rectTransform.pivot.y == 1) ? pos.y * 2 + 1 : pos.y* 2 - 1;
 
             InputDirection = new Vector3 (x, 0, y);
             if (InputDirection.magnitude == 0) {
                 anim.SetBool ("isWalking",false);
                 anim.SetBool ("isIdle",true);
                 anim.SetBool ("isPunching",false);
                 anim.SetBool ("isRunning",false);
             }
             else if (InputDirection.magnitude > 0.9) {
                 
                 anim.SetBool ("isWalking",false);
                 anim.SetBool ("isIdle",false);
                 anim.SetBool ("isPunching",false);
                 anim.SetBool ("isRunning",true);
             } else {
                 anim.SetBool ("isIdle",false);
                 anim.SetBool ("isPunching",false);
                 anim.SetBool ("isRunning", false);
                 anim.SetBool ("isWalking", true);
             }
             InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;
             Debug.Log (InputDirection);
             joystick .rectTransform.anchoredPosition = 
                 new Vector3 (InputDirection.x * (bgJoy.rectTransform.sizeDelta.x / 3.3333333f)
                     , InputDirection.z * (bgJoy.rectTransform.sizeDelta.y/3.3333333f));
         }
 
     }
     public virtual void OnPointerUp(PointerEventData ped)
     {
         InputDirection = Vector3.zero;
         joystick.rectTransform.anchoredPosition = Vector3.zero;
         anim.SetBool ("isIdle",true);
         anim.SetBool ("isPunching",false);
         anim.SetBool ("isRunning", false);
         anim.SetBool ("isWalking", false);
         setTransparency (0.5f);
     }
     public virtual void OnPointerDown(PointerEventData ped)
     {
         OnDrag (ped);
     }
 
 }

CHARACTERWALKER.CS

 using UnityEngine;
 using System.Collections;
 using Devdog.General;
 using Devdog.General.UI;
 
 
     [RequireComponent(typeof(CharacterController))]
     public class CharacterWalker : MonoBehaviour, IPlayerInputCallbacks
     {
         [InspectorReadOnly]
         public bool isGrounded;
 
         [Header("Config")]
         public float walkSpeed = 2f;
         public float turnSpeed = 2f;
         public float jumpSpeed = 40f;
         public float gravity = 9.81f;
 
         public float rotationSpeed = 1.0f;
 
         public float heightModifier = 1.45f;
 
         [SerializeField]
         [Required]
         private Camera _cam;
         private CharacterController _controller;
         private Animator _anim;
         private Vector3 _moveDirection = Vector3.zero;
 
         public virtualJS joystick;
 
         protected virtual void Start()
         {
             _controller = GetComponent<CharacterController>();
             _anim = GetComponent<Animator> ();
         }
 
         public void SetInputActive(bool active)
         {
             this.enabled = active;
         }
 
         protected void Update()
         {
             if (UIUtility.isFocusedOnInput)
             {
                 return;
             }
             if (joystick.InputDirection.magnitude != 0) {
                 _controller.transform.forward = Vector3.Normalize (joystick.InputDirection);
 
             }
             RotateToCamera(false); // Don't force rotation only if we move
             if (isGrounded)
             {
                 _moveDirection = CalcDirection();
 
                 if (Input.GetButtonDown("Jump"))
                 {
                     _moveDirection.y = jumpSpeed * heightModifier;
                 }
             }
             _moveDirection.y -= gravity * Time.deltaTime;
             var flags = _controller.Move(_moveDirection * Time.deltaTime);
             isGrounded = (flags & CollisionFlags.CollidedBelow) != 0;
 
             float x = 0;
             if (Input.GetMouseButton(1))
             {
                 x = Input.GetAxis("Mouse X") * turnSpeed;
             }
             else if (joystick.InputDirection.y != 0 || joystick.InputDirection.x != 0f)
             {
                 x = Input.GetAxis("Mouse X") * turnSpeed;
             }
 
             transform.Rotate(0, x * rotationSpeed, 0);
         }
 
         public Vector3 CalcDirection()
         {
             //_moveDirection = new Vector3(GetHorizontal(), 0, GetVertical());
             _moveDirection = new Vector3(joystick.InputDirection.x, 0 , joystick.InputDirection.y);
             _moveDirection = _cam.transform.TransformDirection(_moveDirection);
 
             _moveDirection *= walkSpeed;
             _moveDirection.y = 0;
 
             return _moveDirection;
         }
 
         public void RotateToCamera(bool force)
         {
             //Vector3 t = new Vector3(GetHorizontal(), 0, GetVertical());
             Vector3 t = new Vector3(joystick.InputDirection.x, 0, joystick.InputDirection.y);
             if (t != Vector3.zero || force == true)
             {
                 Quaternion nRot = _cam.transform.rotation;
                 nRot.x = 0;
                 nRot.z = 0;
 
                 transform.rotation = Quaternion.Lerp(transform.rotation, nRot, Time.deltaTime * rotationSpeed);
             }
         }
 
         public float GetVertical()
         {
             /* Original */
             /*
             float vert = Input.GetAxis("Vertical");
             if (vert < 0)
             {
                 vert = (vert / 2f); // Twice as slow backwards
             }
 
             return vert;
             */
             return Input.GetAxis("Vertical");
         }
 
         public float GetHorizontal()
         {
             return Input.GetAxis("Horizontal");
         }
     }
 




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

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to have Character controller moving in relation to the camera, and rotating in the direction of movement? 0 Answers

Character object is unstable and I can't figure out why or how to fix it 0 Answers

Rotate on key press to an end 0 Answers

My character only flips left and right. I want him to move and jump onto platforms. Its donkey kong and I want him to catch coins. At the moment hes just swopping directions. I cant find what Im doing wrong!! Please help! 0 Answers

How to check if an object was rotated clockwise or counterclockwise? 0 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