Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Local y rotation tank with controller

Discussion in 'Scripting' started by TitusMachine_, May 29, 2017.

  1. TitusMachine_

    TitusMachine_

    Joined:
    Apr 24, 2017
    Posts:
    13
    When I move my joystick forward, the tank should point in the same forward direction, the same when I point him to the left,right,....(any direction).
    It does the job. But the more I tilt my tank, the more it will shake.

    I have made a video to give a clearer perspective of what's going wrong.
    Here is the video link.
    And here is a download link with my project only basd on the tank movement.
    (input with xBox controller).

    Can someone help me out with this problem?
    Don't forget that my tank needs to point in the same direction of my joystick.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TankControl : MonoBehaviour
    6. {
    7.     private Transform _transform;
    8.     public Transform TurretUpper;
    9.     //Movement joystick
    10.     private float _angle;
    11.     private float _joyStickForce;
    12.     private float _turretAngle;
    13.     private Vector3 _joyStickVector = Vector3.zero;
    14.  
    15.     public float TankSpeed = 4;
    16.  
    17.     void Start()
    18.     {
    19.         _transform = transform;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         RotateAndMovePlayer();
    26.         TurretRotate();
    27.     }
    28.  
    29.     private void RotateAndMovePlayer()
    30.     {
    31.         //Joystick
    32.         _joyStickVector.x = Input.GetAxis("TankH");
    33.         _joyStickVector.z = Input.GetAxis("TankV");
    34.         if (Mathf.Abs(_joyStickVector.x) >= 0.19f || Mathf.Abs(_joyStickVector.z) >= 0.19f)
    35.         {
    36.             //Direction
    37.             _angle = 90 - Mathf.Atan2(_joyStickVector.z, _joyStickVector.x) * Mathf.Rad2Deg;
    38.             _angle = _angle - _transform.eulerAngles.y;
    39.             _transform.Rotate(_transform.up, _angle, Space.Self);
    40.  
    41.             //Moving
    42.             //Joystick speed
    43.             if (Mathf.Abs(_joyStickVector.x) >= Mathf.Abs(_joyStickVector.z))
    44.             {
    45.                 _joyStickForce = Mathf.Abs(_joyStickVector.x);
    46.             }
    47.             else
    48.                 _joyStickForce = Mathf.Abs(_joyStickVector.z);
    49.  
    50.             //UNCOMMENT IF YOU WANT THE TANK TO BE ABLE TO MOVE
    51.             //_transform.Translate(Vector3.forward * TankSpeed * Time.deltaTime * _joyStickForce);
    52.         }
    53.     }
    54.  
    55.     private void TurretRotate()
    56.     {
    57.         _joyStickVector.x = Input.GetAxis("TurretH");
    58.         _joyStickVector.z = Input.GetAxis("TurretV");
    59.         if (Mathf.Abs(_joyStickVector.x) >= 0.19f || Mathf.Abs(_joyStickVector.z) >= 0.19f)
    60.         {
    61.             _turretAngle = 90 - Mathf.Atan2(_joyStickVector.z, _joyStickVector.x) * Mathf.Rad2Deg;
    62.             float parentAngle = _transform.eulerAngles.y;
    63.             _turretAngle -= parentAngle;
    64.             TurretUpper.localEulerAngles = new Vector3(0, _turretAngle, 0);
    65.         }
    66.     }
    67. }
    68.  
     
    Last edited: May 30, 2017
  2. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    ref ...
    Code (csharp):
    1.  
    2.   _angle = _angle - transform.eulerAngles.y;
    3.  
    What does this line do? its feeding back the new Y angle back into the next new angle.
    If you remove this line do you get the shake? (Tank may face the wrong way tho).
     
  3. TitusMachine_

    TitusMachine_

    Joined:
    Apr 24, 2017
    Posts:
    13
    If you remove this line do you get the shake? (Tank may face the wrong way tho).[/QUOTE]
    the rotate methods doesn't override the origional angle id will ADD.
    The rotate methods doesn't override the origional angle id will ADD.
    Imagine the y angle is 70 and the new incoming angle is 10->
    .Rotate will automaticly add the 10 and I don't want 80 because i want it to become 10.
    So that's why I use this line of code.

    And yes It will shake alot. (the adding makes it shake and i think somehow the rotattion gets confused when some values are going below 0 or higher than 360.
    So I'm pretty sure that I will have to add some if statements before rotating
     
    Last edited: May 29, 2017
  4. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    If you switch to local space when setting the rotation, does it resolve the issue?.

    Im stiill a little confused why you need to make use of the transform.eulerAngles.y. as part of the _angle.
    Are you trying to get it to move slowly to face the joystick or follow it directly.
    If directly then why not set the Y angle directly relative to the joystick angle?
     
  5. TitusMachine_

    TitusMachine_

    Joined:
    Apr 24, 2017
    Posts:
    13
    If I change the Y angle directly relative to that angle it wont rotate on it's local axis anymore. The x an z wont change and I want them to rotate with because thats how a local rotation suposes to work. If you want me to I can make another video for you where I once delete that line and where I once set the y directly relative to the angle.
     
  6. TitusMachine_

    TitusMachine_

    Joined:
    Apr 24, 2017
    Posts:
    13
    I posted a video anyway. Here is the link. You will see what happens when I try out one of those 2 ways that you told me about.
     
  7. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    I would create an empty parent game object that I update to follow the terrain and make the tank a child. Rotating the tank around the local.y then becomes a lot simpler and does not affect the angle on how it sits on the terrain.
     
  8. TitusMachine_

    TitusMachine_

    Joined:
    Apr 24, 2017
    Posts:
    13
    And what about the colliders and physics? I get what you are trying to say but it wont work. I once had a glimpse of the same thoughts but it wont work. I think that I will have to look for some if statements and some weird maths before rotating.

    Tank (box collider, rigidbody, script(also rotating this whole gameobject))
    upper(rotates in parent's script)
    lower
     
  9. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    As the tank will be a child all the colliders and physics should work the same. It adds a transform giving you access to local angles.

    At the moment your code is taking the angle of the tank in world space and not the angle it is facing. Then using that angle to calculate the amount to rotate to face the right direction. When the tank is flat this works ok but as it goes up on its side the results become more elliptical and less accurate.
     
  10. TitusMachine_

    TitusMachine_

    Joined:
    Apr 24, 2017
    Posts:
    13
    I tried it but this happens: link
    For some reason it still gives the same problem pff :S
     
  11. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    Looked like it was working until the tank went upside down?

    Can you post you rotation code and direction code bits
     
    Last edited: May 30, 2017
  12. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    I think the problem is that it’s using the angle of the tank and not the direction of the tank.
    I would take a forward vector of the tank
    Reset the Y vale to 0:
    Normalise the vector and then use this vector/angle for any calculations.
     
  13. TitusMachine_

    TitusMachine_

    Joined:
    Apr 24, 2017
    Posts:
    13
    How do you mean? can you write it down in short for me? And it does use the direction because I used localEulerAngles, isn't it?
     
  14. TitusMachine_

    TitusMachine_

    Joined:
    Apr 24, 2017
    Posts:
    13
    Didn't you see my code in the video? I would like to just send my whole project but I'm not sure if that's a good idea because it's for an asignment and I think that sharing isn't allowed.
     
  15. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    It’s a lot easier to talk about the code when it is posted within the forum. Others may also be able to help.
     
  16. TitusMachine_

    TitusMachine_

    Joined:
    Apr 24, 2017
    Posts:
    13
    Here is the download link to my project only based on the tank movement :) I hope you have a xbox controller? If you have a ps3 controller i'd say to just change each input axis in the input manager it should take about 1 minute to do that.

    Owh! And I also placed the link in my thread
     
    Last edited: May 30, 2017