- Home /
Joystick Movement problems
I am trying to make a mario-3D-world style game but am having problems with how Joystick input works in unity. The problem is that when aiming diagonally, the character moves quicker than not diagonally. When Drawing a ray from the character and the characters position + the joystick input, the ray is longer diagonally. I've tried to normalize it and then multiply that with a speed but that doesn't seem to do anything. Is there a way to counteract this?
Code:
var rb : Rigidbody;
var X_Input : float;
var Z_Input : float;
var Speed : float;
var camDir : Vector3;
var newVel : Vector3;
function Start () {
//simplifies getcomponent to only rb
rb = GetComponent("Rigidbody");
Speed = 20;
}
function Update ()
{
//Find Camera Direction
camDir = Camera.main.transform.forward;
//Set Camera Direction.y to 0 so movement not affected on y axis
camDir.y = 0;
camDir.Normalize();
//find left and right of camera by using CameraDirection
var camRight = new Vector3(camDir.z,0,-camDir.x);
var playerUp = Vector3.up;
//Joystick Input
X_Input = Input.GetAxis("L_Joystick_Horizontal");
Z_Input = -Input.GetAxis("L_Joystick_Vertical");
//Set Old Movement to normal
newVel = Vector3.zero;
//Set Based on inputs
newVel = new Vector3(camRight.x*X_Input,0,camDir.z*Z_Input);
//Now only stores the DIRECTION of input(can control speed manually later)
Vector3.Normalize(newVel);
//Vector3.ClampMagnitude(newVel, 1);
//Debug
Debug.Log();
Debug.DrawRay(transform.position,newVel,Color.green);
}
function FixedUpdate()
{
//Adds inputdirection*speed force to player
rb.AddForce(newVel*Speed);
}
Your answer
Follow this Question
Related Questions
Joystick unusual behavior on axis with almost the same script as W A S D 0 Answers
Touch buttons for step movememt 3 Answers
Android based ARCore example ObjectManipulation using Joystick Controller 0 Answers
How to remove the Vertical axis on Joystick? 1 Answer
Hello Im making a Game for mobile and want two floating joysticks one on either side of the screen. 0 Answers