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 wafer23 · Jun 23, 2017 at 04:22 AM · vector3joystickrotate objectsmooth

my 2d Object dont rotate smoothly with joystick

hi all below is the code i'm using the game object is rotating via joystick but it's not realistic or due to using Quaternion.Slerp please help me to make the abject rotate smoothly

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;

public class androifplanecontroller : MonoBehaviour {

 public float rotatespeed = 1 ;
 public float rotatevalue  ;


 Rigidbody2D rb;

 void Start () {
     rb = GetComponent<Rigidbody2D> ();
 }

 void FixedUpdate ()
 {
     Vector3 lookvect = new Vector3 (CrossPlatformInputManager.GetAxis ("Horizontal"), CrossPlatformInputManager.GetAxis ("Vertical"), 5000);

     if (lookvect.x != 0 && lookvect.y != 0) {
         
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation (lookvect, Vector3.back), rotatespeed* 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
0

Answer by Guy_Yome · Jun 23, 2017 at 04:01 PM

As you requested earlier today, here is the joystick conversion code @RationalCommander

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class JoyStick : MonoBehaviour {
 
     private float dirX = 0F;
     private float dirY = 0F;
     private Vector2 circlePoint = Vector2.zero;
 
     private float circleRadius = 1;
 
     void Update () {
 
         dirX = Input.GetAxis( "JoyStick_X" );
         dirY = -1 * Input.GetAxis( "JoyStick_Y" );
 
         dirX = Mathf.Clamp( dirX, -1, 1 );
         dirY = Mathf.Clamp( dirY, -1, 1 );
 
         // Makes into a single point the joystick values
         Vector2 circlePoint = new Vector2( dirX, dirY );
 
         circlePoint = correctCirclePoint( circlePoint ); // Converts it to a circle
         circlePoint *= circleRadius; // Makes it the radius we really need it at the end. Could easily be added at the end of the function with an argument
 
     }
 
     // Returns the "square" value of the joystick mapped to a circle of same radius
     private Vector2 correctCirclePoint ( Vector2 point ){
 
         // If the point given is at distance 0 from the center, then we don't need to convert it
         if (Vector2.Distance(Vector2.zero, point) > 0) {
 
             float xC = point[0]; // X coordinate of the point
             float yC = point[1]; // Y coordinate of the point
 
             float xC2 = Mathf.Pow( xC, 2 ); // Squared value of the X coordinate
             float yC2 = Mathf.Pow( yC, 2 ); // Squared value of the Y coordinate
 
             float xCercle = xC * ( Mathf.Sqrt( xC2 + yC2 - ( xC2 * yC2 ) ) / Mathf.Sqrt( xC2 + yC2 ) ); // Conversion of X
             float yCercle = yC * ( Mathf.Sqrt( xC2 + yC2 - ( xC2 * yC2 ) ) / Mathf.Sqrt( xC2 + yC2 ) ); // Conversion of Y
 
             return new Vector2( xCercle, yCercle );
 
         }else{
             return new Vector2( 0, 0 );
         }
 
     }
 
 }

alt text


joystick-inputs.png (26.0 kB)
Comment
Add comment · Show 1 · 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 wafer23 · Jun 25, 2017 at 01:45 AM 0
Share

Can u please elaborate more . How can i use this code where should i refer to the player .... etc. Where should i add this code on the player or in the joystick . Thanks in advance .

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

116 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

Related Questions

Rotating 3d person character 0 Answers

Game pad controller (submarine rotation) 0 Answers

rotate object around a point over time 1 Answer

Rotate object to joystick and position direction 0 Answers

Rotate object to joystick direction 2 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