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);
}
}
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 );
}
}
}
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
Follow this Question
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