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 $$anonymous$$ · Jun 11, 2018 at 05:59 PM · scripting problemmovement

I can't get a joystick to move my player

So I've tried everything I could find, and nothing works. Can some please tell me how I would make my player move with this code and what I have to add. The player needs to be able to move in every direction like in Last day on earth survival and doesn't have to jump. I'm still learning C# so sorry if I'm missing something simple. I followed the N3K EN joystick tutorial and it's working but I don't know how to make my player move.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class JoyStick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler {
 
     private Image joystickBG;
     private Image joystickS;
 
     public Vector2 pos;
 
     public static Vector3 inputVector;
 
 
     private void Start()
     {
         joystickBG = GetComponent<Image>();
         joystickS = transform.GetChild(0).GetComponent<Image>();
     }
 
     public virtual void OnDrag(PointerEventData ped)
     {
         if (RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBG.rectTransform
                                                                    , ped.position
                                                                    , ped.pressEventCamera
                                                                    , out pos)) ;
         {
             pos.x = (pos.x / joystickBG.rectTransform.sizeDelta.x);
             pos.y = (pos.y / joystickBG.rectTransform.sizeDelta.y);
 
             Debug.Log(pos);
 
             inputVector = new Vector3(pos.x * 2, 0, pos.y * 2);
             inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
 
             joystickS.rectTransform.anchoredPosition =
                 new Vector3(inputVector.x * (joystickBG.rectTransform.sizeDelta.x / 3)
                 , inputVector.z * (joystickBG.rectTransform.sizeDelta.y / 3));
         }
     }
     public virtual void OnPointerUp(PointerEventData ped)
     {
         inputVector = Vector3.zero;
         joystickS.rectTransform.anchoredPosition = Vector3.zero;
     }
     public virtual void OnPointerDown(PointerEventData ped)
     {
         OnDrag(ped);
     }
 }
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
Best Answer

Answer by AdamOliver · Jun 11, 2018 at 07:01 PM

Unless I'm missing something obvious this script will only make the joystick image move, there's nothing in there to control a player. Around line 13 add public Transform player;, then at the bottom of your OnDrag method, you can use player.translate(inputVector) to move your player's transform by the input vector. Then in the inspector don't forget to drag your player into the "player" field on this script.

Comment
Add comment · Show 2 · 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
avatar image $$anonymous$$ · Jun 12, 2018 at 06:53 AM 0
Share

Thank you, it worked! but how do I make it set a bool to activate animation and how can I make it go a little bit slower? If I want it to play the animation do I do this

     public void Update()
     {
         if (inputVector.magnitude != 0)
         {
             player.Translate(inputVector);
             Animator.SetBool("Walk", true);
         }
         if (inputVector.magnitude = 0)
         {
             Animator.SetBool("walk", false);
 
         }
     }


avatar image AdamOliver · Jun 14, 2018 at 08:37 PM 0
Share

I'm afraid I haven't worked with animators so I'm not sure on that one, if you still aren't sure maybe ask it as a new question

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

184 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

Related Questions

Character triple+ Jumping: Please Help! 0 Answers

Slime script? 1 Answer

Having problem with detecting movement 1 Answer

How to make a square mesh and remove a part of it in real time? 0 Answers

AI Movement Direction 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