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 RandomUser123 · Aug 04, 2014 at 03:42 PM · joystickballtorqueroll

Problem with joystick movement

I'm using the standard asset mobile joystick.js script for my controls, but I'm having trouble with my character movement. I'm just trying to move, left, right, up and down using AddRelativeTorque and it works to an extent but not quite the way I want.

I have a ball in the scene and rolls around ok at first, but it seems as though the rotation is throwing the controls off as when the ball is stationary it works fine but when I start moving it, the controls don't behave accordingly.

I'm probably doing this wrong but can anyone help me out here, I would just like to be able to roll my ball in the x and z axis. Here is my code:

 #pragma strict
 
 // This script must be attached to a GameObject that has a CharacterController
 @script RequireComponent( CharacterController )
 
 var moveJoystick : joystick;
 
 var speed : float = 10;                   // Ground speed
 
 
 private var thisTransform : Transform;
 private var character : CharacterController;
 
 function Start()
 {
     // Cache component lookup at startup instead of doing this every frame 
     thisTransform = GetComponent( Transform );
     character = GetComponent( CharacterController );   
 }
 
 function Update()
 {
     //transform.eulerAngles.y = 0;
     
     if(moveJoystick.position.x >= 0.5)
     {
         rigidbody.AddTorque(Vector3.up * 10);
     }
     
     else if(moveJoystick.position.x <= -0.5)
     {
         rigidbody.AddTorque(Vector3.down * 10);
     }
     
     else if(moveJoystick.position.y >= 0.5)
     {
         rigidbody.AddTorque(Vector3.left * 10);
     }
     
     else if(moveJoystick.position.y <= -0.5)
     {
         rigidbody.AddTorque(Vector3.right * 10);
     }
 
 }

Comment
Add comment · Show 2
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 RandomUser123 · Aug 04, 2014 at 06:33 PM 0
Share

Id really appreciate any input :/

avatar image drex150 · Aug 04, 2014 at 07:34 PM 0
Share

You're going to want to add torque relative to the rotation of your camera or an empty game object ins$$anonymous$$d of the ball itself. The ball is spinning and so it's rotation is changing which is screwing up the torque. If you make an empty game object and make it the parent of the ball you can simply use the transform.forward of the parent to rotate the ball.

So with the ball as a child of an empty do this:

 Var ball : GameObject; //set the ball to this in the inspector. 
 
 Function Update()
 {
      if(moveJoystick.position >= 0.5)
      {
           ball.rigidbody.AddTorque(Vector3.Forward) *10;
      }
 
 }



Now remove the script from the ball itself and attach it to the empty game object.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by RandomUser123 · Aug 04, 2014 at 11:58 PM

Cheers for the input drex150 but i managed to fix it, i had my Vectors all wrong, i should have just done them one by one instead of assuming what the values should have been. I was using Vector3.up and Vector3.down instead of Vector3.back and Vector3.forward

Here's my working script, everything up to the if statements is the same:

      if(moveJoystick.position.x >= 0.5)
      {
           rigidbody.AddTorque(Vector3.back * 10) ;
      }
  
 
     
     else if(moveJoystick.position.x <= -0.5)
     {
         rigidbody.AddTorque(Vector3.forward * 10);
     }
     
     
     else if(moveJoystick.position.y >= 0.5)
     {
         rigidbody.AddTorque(Vector3.right * 10);
     }
     
     else if(moveJoystick.position.y <= -0.5)
     {
         rigidbody.AddTorque(Vector3.left * 10);
     }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Another Ball Rolling Game Question 1 Answer

ROLL a ball towards the player 1 Answer

make a wedge moveable 0 Answers

first time coding, getting "NullReferenceException: Object reference not set to an instance of an object" error 1 Answer

Ball Doesn't Roll Down Hill - Physics Problem 3 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