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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Reggul · Jul 31, 2017 at 04:30 PM · joystick

Joystick Character movement.

Hello! I am trying to make my character move with joystick and want to make him face direction he moving. But i faced some problems and don't know how to solve them. I decided to add eulerangles code and character faces right direction but now movement is not working right.

When i move joystick down, character goes up, when i move joystick up character also goes up.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Movech : MonoBehaviour {

 public float moveSpeed;
 public VirtualJoystick joystick;
 // Use this for initialization
 void Start () {
     moveSpeed = 15f;
 }
 
 // Update is called once per frame
 void Update () {
 transform.eulerAngles = new Vector3 (transform.eulerAngles.x, Mathf.Atan2(joystick.Horizontal 
           (),joystick.Vertical ())*Mathf.Rad2Deg, transform.eulerAngles.z);
 transform.Translate (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, 0f, 
            moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
 transform.Translate (moveSpeed * joystick.Horizontal () * Time.deltaTime, 0f, moveSpeed * 
             joystick.Vertical () * Time.deltaTime);
 }

}

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
3

Answer by FlaSh-G · Jul 31, 2017 at 10:09 PM

I can promise you that using trigonometry is pretty much never needed in Unity. Ever. There's always a function that allows you to create a cleaner and easier solution, usually in the Quaternion struct or the Vector3 struct.

As far as I can see, you're going for 3D here. To immediately rotate your character to a specific direction, the simplest way is to just set Transform.forward:

 var input = new Vector3(joystick.Horizontal, 0, joystick.Vertical);
 if(input != Vector3.zero)
 {
   transform.forward = input;
 }

Alternatively, you can create a Quaternion representing this rotation using Quaternion.LookRotation:

 Quaternion targetRotation;
 
 void Update()
 {
   var input = new Vector3(joystick.Horizontal, 0, joystick.Vertical);
   if(input != Vector3.zero)
   {
     targetRotation = Quaternion.LookRotation(input);
   }
   // Rotate towards targetRotation here
 }

And then, to have the character steadily rotate towards the set target rotation (as the code comment suggests), use Quaternion.RotateTowards:

 transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, speed * Time.deltaTime);

This answer might be a bit more than expected already, but basically it's just to make clear: You don't need trigonometric functions, just Quaternion and Vector3 functions ;)

Comment
Add comment · Show 4 · 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 Reggul · Aug 01, 2017 at 07:59 AM 0
Share

Thank you for you help.

I tried first variant and it became better. Second variant did not work for me. But i still have same problem that when i drag joystick down my character goes up, same thing for dragging joystick up(he also goes up). Character rotation i perfect, he faces the right direction, but movement is wrong. When i turn off the rotation, he moves right. I think the problem is with movement code. Thank you again, i am going to try fix this :)

avatar image ssdigi Reggul · Aug 17, 2017 at 05:43 PM 0
Share

There is a invert option in the input settings this might help with the player going in the wrong direction.

avatar image FlaSh-G · Aug 01, 2017 at 09:51 AM 1
Share

Ah my bad, I kinda ignored that part of your question. The good thing is: It's easy to solve.

Have a look at Transform.Translate, more specifically: The second parameter.

Space relativeTo = Space.Self

This means that the movement performed by Translate is relative to the object's direction. So "(0, 0, 1)" means "forward relatively to the object", not "forward along the global z axis". So when your character looks towards (0, 0, 1) and goes towards (0, 0, 1), it's fine. But when it looks towards (0, 0, -1), also going towards (0, 0, -1) means the opposite direction, because "backwards" now points in the other direction for the character.

Long story short - Change your Translate call to this:

 var direction = new Vector3(joystick.Horizontal(), 0f, joystick.Vertical());
 transform.Translate(direction * moveSpeed  * Time.deltaTime, Space.World);

After all this, however, you will sooner or later find that Transform.Translate is a bad choice if your object is supposed to interact with the world around it. Translate ignores colliders around it and if a rigidbody is attached to the object, it's constantly just trying to fix Translate having moved your object into others.

You can go with Translate for now, but keep in $$anonymous$$d that when you run into collision problems, this might be the reason.

avatar image Reggul · Aug 01, 2017 at 01:29 PM 0
Share

Thank you very much again!

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

69 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

Related Questions

How to make first person game with joysticks? 0 Answers

After rotate with joystick, rotate resetting,joystick rotate correction 0 Answers

How to multi touch on mobile game 1 Answer

move the object where camera look 0 Answers

Move JoyStick Together With Camera 1 Answer


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