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 /
avatar image
0
Question by adityarzqqq · May 19, 2020 at 11:33 PM · mobilejoystickthird person controller

mobile input joystick

i want to make game with joystick controller , and i have download the example scripts

using UnityEngine; using System.Collections.Generic;

public class SimpleCharacterControl : MonoBehaviour {

 private enum ControlMode
 {
     Tank,
     Direct
 }

 [SerializeField] private float m_moveSpeed = 2;
 [SerializeField] private float m_turnSpeed = 200;
 [SerializeField] private float m_jumpForce = 4;
 [SerializeField] private Animator m_animator;
 [SerializeField] private Rigidbody m_rigidBody;

 [SerializeField] private ControlMode m_controlMode = ControlMode.Direct;

 public FixedJoystick leftjoystick;
 public FixedButton button;
 public FixedTouchField Touchfield;
 protected float CameraAngelY;
 protected float CameraAngelSpeed = 0.1f;
 protected float CameraPosY;
 protected float CameraPosSpeed = 0.1f;

 private float m_currentV = 0;
 private float m_currentH = 0;

 private readonly float m_interpolation = 10;
 private readonly float m_walkScale = 0.33f;
 private readonly float m_backwardsWalkScale = 0.16f;
 private readonly float m_backwardRunScale = 0.66f;

 private bool m_wasGrounded;
 private Vector3 m_currentDirection = Vector3.zero;

 private float m_jumpTimeStamp = 0;
 private float m_minJumpInterval = 0.25f;

 private bool m_isGrounded;
 private List<Collider> m_collisions = new List<Collider>();

 private void OnCollisionEnter(Collision collision)
 {
     ContactPoint[] contactPoints = collision.contacts;
     for(int i = 0; i < contactPoints.Length; i++)
     {
         if (Vector3.Dot(contactPoints[i].normal, Vector3.up) > 0.5f)
         {
             if (!m_collisions.Contains(collision.collider)) {
                 m_collisions.Add(collision.collider);
             }
             m_isGrounded = true;
         }
     }
 }

 private void OnCollisionStay(Collision collision)
 {
     ContactPoint[] contactPoints = collision.contacts;
     bool validSurfaceNormal = false;
     for (int i = 0; i < contactPoints.Length; i++)
     {
         if (Vector3.Dot(contactPoints[i].normal, Vector3.up) > 0.5f)
         {
             validSurfaceNormal = true; break;
         }
     }

     if(validSurfaceNormal)
     {
         m_isGrounded = true;
         if (!m_collisions.Contains(collision.collider))
         {
             m_collisions.Add(collision.collider);
         }
     } else
     {
         if (m_collisions.Contains(collision.collider))
         {
             m_collisions.Remove(collision.collider);
         }
         if (m_collisions.Count == 0) { m_isGrounded = false; }
     }
 }

 private void OnCollisionExit(Collision collision)
 {
     if(m_collisions.Contains(collision.collider))
     {
         m_collisions.Remove(collision.collider);
     }
     if (m_collisions.Count == 0) { m_isGrounded = false; }
 }

 void Update () {
     m_animator.SetBool("Grounded", m_isGrounded);

     switch(m_controlMode)
     {
         case ControlMode.Direct:
             DirectUpdate();
             break;

         case ControlMode.Tank:
             TankUpdate();
             break;

         default:
             Debug.LogError("Unsupported state");
             break;
     }

     m_wasGrounded = m_isGrounded;

     var input = new Vector3(leftjoystick.InputVector.x, 0, leftjoystick.InputVector.y);
     var vel = Quaternion.AngleAxis(CameraAngelY + 100, Vector3.up) * input * 5f;
     // m_rigidBody.velocity = new Vector3(vel.x, m_rigidBody.velocity.y, vel.z);
     CameraAngelY += Touchfield.TouchDist.x * CameraAngelSpeed;
     Camera.main.transform.position = transform.position + Quaternion.AngleAxis(CameraAngelY, Vector3.up) * new Vector3(0, 3, 4);
     Camera.main.transform.rotation = Quaternion.LookRotation(transform.position + Vector3.up * 2f - Camera.main.transform.position, Vector3.up);
 }

 private void TankUpdate()
 {
      float v = Input.GetAxis("Vertical");
      float h = Input.GetAxis("Horizontal");

     bool walk = Input.GetKey(KeyCode.LeftShift);

     if (v < 0) {
         if (walk) { v *= m_backwardsWalkScale; }
         else { v *= m_backwardRunScale; }
     } else if(walk)
     {
         v *= m_walkScale;
     }

     m_currentV = Mathf.Lerp(m_currentV, v, Time.deltaTime * m_interpolation);
     m_currentH = Mathf.Lerp(m_currentH, h, Time.deltaTime * m_interpolation);

     transform.position += transform.forward * m_currentV * m_moveSpeed * Time.deltaTime;
     transform.Rotate(0, m_currentH * m_turnSpeed * Time.deltaTime, 0);

     m_animator.SetFloat("MoveSpeed", m_currentV);

     JumpingAndLanding();
 }

 private void DirectUpdate()
 {
     float v = Input.GetAxis("Vertical");
     float h = Input.GetAxis("Horizontal");

     Transform camera = Camera.main.transform;

     if (Input.GetKey(KeyCode.LeftShift))
     {
         v *= m_walkScale;
         h *= m_walkScale;
     }

     m_currentV = Mathf.Lerp(m_currentV, v, Time.deltaTime * m_interpolation);
     m_currentH = Mathf.Lerp(m_currentH, h, Time.deltaTime * m_interpolation);

     Vector3 direction = camera.forward * m_currentV + camera.right * m_currentH;

     float directionLength = direction.magnitude;
     direction.y = 0;
     direction = direction.normalized * directionLength;

     if(direction != Vector3.zero)
     {
         m_currentDirection = Vector3.Slerp(m_currentDirection, direction, Time.deltaTime * m_interpolation);

         transform.rotation = Quaternion.LookRotation(m_currentDirection);
         transform.position += m_currentDirection * m_moveSpeed * Time.deltaTime;

         m_animator.SetFloat("MoveSpeed", direction.magnitude);
     }

     JumpingAndLanding();
 }

 private void JumpingAndLanding()
 {
     bool jumpCooldownOver = (Time.time - m_jumpTimeStamp) >= m_minJumpInterval;

     if (jumpCooldownOver && m_isGrounded && Input.GetKey(KeyCode.Space))
     {
         m_jumpTimeStamp = Time.time;
         m_rigidBody.AddForce(Vector3.up * m_jumpForce, ForceMode.Impulse);
     }

     if (!m_wasGrounded && m_isGrounded)
     {
         m_animator.SetTrigger("Land");
     }

     if (!m_isGrounded && m_wasGrounded)
     {
         m_animator.SetTrigger("Jump");
     }
 }

}

please how can i change the controller from wasd to joystick, i have tired for this...

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by boss2070301 · May 20, 2020 at 12:32 AM

This should be it

  emphasized textprivate void DirectUpdate()
  {
      float v = leftjoystick.Vertical;
      float h = leftjoystick.Horizontal;


Comment
Add comment · Share
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

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

187 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 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

Why isn't my joystick code working? 1 Answer

Hello Im making a Game for mobile and want two floating joysticks one on either side of the screen. 0 Answers

2D shooting with mobile joystick 1 Answer

Make gameObject face position of Joystick [Mobile] 4 Answers

Unity 5 Character Controller Mobile Input 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