Search Unity

Joystick left right script (mobile first person) question !

Discussion in 'Scripting' started by spiretakk, Sep 15, 2020.

  1. spiretakk

    spiretakk

    Joined:
    Aug 18, 2019
    Posts:
    10
    Hello everyone , hope every one is having a good day !
    So i have this premade (template) script of a joystick. Its kinda complicated for me to explain whats going on but ill give it a go ! :) The joystick function is incorrect for me , meaning that on my First person game pulling the joystick up moves the character forward (thats good !). moving it down moves the character backwards (also good). The problem is left and right ! When (as you face the joystick) turn it left or right, instead of the character moving left and keep facing forward (like in every first person game) the character rotates while not moving as well. I have a the script here if anyone of you can take some time to look on whats going on so i can make the change on making it move left or right (and keep looking forward without the character rotating).

    Thank you !!!

    Code (JavaScript):
    1.  
    2.  
    3. ///
    4. var phys;
    5. let aVec = new Vec2(0,0); // Angle vector
    6. let dVec = new Vec2(0,0); // Direction vector
    7. let jRad = 200; // Should be synced with actual UI radius
    8. let _cam;
    9. let _rotSpeed = 1;
    10. let _moveSpeed = 1;
    11. let yAngle = 0;
    12. let deltaY = 0;
    13. let localSpace = true;
    14.  
    15. let speedScale = 0;
    16.  
    17. Quaternion.prototype.toString = function(){return this.x+' '+this.y+' '+this.z+' '+this.w}
    18. let speed = new Vec3(0,0,0);
    19. let forward = new Vec3(0,0,0);
    20.  
    21. function init() {
    22.     _cam = this.scene().camera();
    23.     phys = this.entity().physics();
    24.     speedScale = 0.05;
    25.     if (phys && phys.type() != 'kDynamic') {
    26.         phys = null;
    27.         speedScale = 0.003;
    28.     }
    29.     _moveSpeed = this.attribute('Movement Speed');
    30.     _rotSpeed = this.attribute('Rotation Speed');
    31.     localSpace = this.attribute('Local Space');
    32.  
    33.     // Set initial yAngle
    34.     let currentY = this.entity().rotation().y;
    35.    
    36.     if (currentY > 360){
    37.         yAngle = currentY%360;
    38.     }
    39.     else if (currentY < 0){
    40.         yAngle = 360 - Math.abs(currentY)%360;
    41.     }
    42. }
    43. function update(dt) {
    44.     let cRot = _cam.worldRotation();
    45.                
    46.     if (aVec.length() != 0){
    47.         // Calculate intended y-axis angle
    48.         let yDel = aVec.y / jRad * 3 * _rotSpeed;
    49.         let yFinal = yAngle+yDel;
    50.         deltaY = yDel;
    51.         if (yFinal > 360){
    52.             yFinal = yFinal-360;
    53.         }
    54.         else if (yFinal < 0){
    55.             yFinal = 360+yFinal;
    56.         }
    57.        
    58.         yAngle = yFinal;
    59.         cRot = Quaternion.fromEuler(0, yFinal, 0);
    60.     }
    61.     else {
    62.         deltaY = deltaY/1.1;
    63.        
    64.         if (Math.abs(deltaY) < 0.01){
    65.             deltaY = 0;
    66.         }
    67.        
    68.         cRot = Quaternion.fromEuler(0, yAngle, 0);
    69.     }
    70.    
    71.     speed = speed.scale(_moveSpeed);
    72.  
    73.     if (!phys){
    74.         if (dVec.length() == 0){
    75.             speed = speed.scale(0.9);
    76.            
    77.             if (speed.length() < 0.01){
    78.                 speed = new Vec3(0,0,0);
    79.             }
    80.         }
    81.        
    82.         let ep = this.entity().position().add(speed);
    83.         this.entity().setPosition(ep.x, ep.y, ep.z);
    84.     }
    85.    
    86.     // Transform facing direction
    87.     if(forward.length() != 0){
    88.         if (phys){
    89.             phys.setAngularVelocity(0, deltaY, 0);
    90.         }
    91.         else {
    92.             //let ep = this.entity().position();
    93.             let originQuat = this.entity().rotationQuat();
    94.             //let targetQuat = Quaternion.lookAt(ep, ep.add(forward));
    95.             let nextQuat = Quaternion.slerp(originQuat, cRot, 0.1);
    96.             this.entity().setRotationQuat(nextQuat);          
    97.         }
    98.     }
    99.    
    100.     if (phys){
    101.         // Calculate intended forward/backwards movement
    102.        
    103.        
    104.            
    105.         if (speed.length() != 0){
    106.             let vel = phys.linearVelocity();
    107.             phys.setLinearVelocity(speed.x, vel.y, speed.z);
    108.         }
    109. //        else {
    110. //            phys.setLinearVelocity(vel.x, vel.y, vel.z);
    111. //        }
    112.     }
    113. }
    114.  
    115. function signal(name, value) {
    116.     aVec = new Vec2(value.y, -value.x);
    117.     dVec = new Vec2(value.x, -value.y);
    118.    
    119.     value = new Vec3(0, 0, -value.y);
    120.     //value = new Vec3(value.x, 0, -value.y);
    121.    
    122.     //Camera Mapping
    123.     if(localSpace) {
    124.         //let rot = this.scene().camera().worldRotation();
    125.         let rot = this.entity().rotationQuat();
    126.         let m = Mat4.createRotation( rot );
    127.         value = Mat4.transformVector( m, value);
    128.         value.y = 0;
    129.     }
    130.    
    131.    
    132.     if(value.length()>0){
    133.         forward = value.scale(1);
    134.        
    135.         if (!phys){
    136.             speed = value.scale(speedScale);          
    137.         }
    138.     }
    139.    
    140.    
    141.     if (phys){
    142.         speed = value.scale(speedScale);
    143.     }
    144. }
     
  2. mgrekt

    mgrekt

    Joined:
    Jun 22, 2019
    Posts:
    92
    You should reference the camera to tell which way is forward, left, and right for the character. So that even if the camera is facing a different direction than of that how it was set up it'd move to the left of the camera.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    This is UnityScript... this does not run on Unity anymore.

    The lingua Franca is now C#.
     
    mgrekt likes this.
  4. mgrekt

    mgrekt

    Joined:
    Jun 22, 2019
    Posts:
    92
    oh right xd I didn't even bother reading the script didn't realize it was jav
     
    Kurt-Dekker likes this.
  5. spiretakk

    spiretakk

    Joined:
    Aug 18, 2019
    Posts:
    10
    How am I able to do that? :) thanks for the reply btw!!!
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    100% of the code you posted is NOT going to ever again work in Unity. It is old code and that language has been deprecated and subsequently removed from Unity.

    Instead, break out Youtube and start looking for tutorials for what you want to do. There are tens or hundreds of thousands of different tutorials for Unity.