Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Missing parts in understanding of Vector3

Discussion in 'Physics' started by dberkay, Sep 26, 2022.

  1. dberkay

    dberkay

    Joined:
    Apr 10, 2021
    Posts:
    4
    Lane change mechanism,I have an algorithm that makes it similar to the one in Subway Surfer and it works. However, as far as I've learned, this code should not work properly because for example, (-2.5,0,0) should be in Vector and let's assume that the target position is 2.5f, so the result (5 ,0,0) but the code works.
    And then we multiply moveVector.x by speed, so it should break the code, but it still works.
    Despite all this, how can my character switch between -2.5 0 and 2.5 perfectly? How does CharacterController.Move work?

    My Code ;
    (LANE_DISTANCE = 2.5F BTW)
    Code (CSharp):
    1. if (desiredLane == 0) //1 Birim sola kay
    2.             targetposition += Vector3.left * LANE_DISTANCE;
    3.         else if (desiredLane == 2) //1 Birim saga kay
    4.             targetposition += Vector3.right * LANE_DISTANCE;
    5.  
    6.        
    7.         //Calculate our move delta
    8.         Vector3 moveVector = Vector3.zero;
    9.         moveVector.x = (targetposition - transform.position).x * playerSpeed;
    10.  
    11.         moveVector.z = playerSpeed;
    12.  
    13.         Vector3 move = new Vector3(moveVector.x, 0,moveVector.z);
    14.         controller.Move(move * Time.deltaTime * playerSpeed);

    All codes in the Script;
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Testcontroller : MonoBehaviour
    6. {
    7.     private const float LANE_DISTANCE = 2.5f;
    8.     //private const float TURN_SPEED = 0.05f;
    9.     private bool isRunning = false;
    10.  
    11.     private CharacterController controller;
    12.     private Vector3 playerVelocity;
    13.     private bool groundedPlayer;
    14.     private float playerSpeed = 2.0f;
    15.     private float jumpHeight = 1.5f;
    16.     private float gravityValue = -25.81f;
    17.     private int desiredLane = 1; // 0 = left, 1 = middle, 2 = right
    18.  
    19.     //Animator
    20.     private Animator anim;
    21.     private Animator coinAnim;
    22.  
    23.     private float originalSpeed = 7.0f;
    24.     private float speedIncreaseLastTick; //En son ne zaman hizi arttirdigimizin kaydini tutuyor
    25.     private float speedIncreaseTime = 2.5f;
    26.     private float speedIncreaseAmount = 0.1f;
    27.  
    28.     private void Start()
    29.     {
    30.         //playerSpeed = originalSpeed; => DUZELT
    31.         controller = gameObject.GetComponent<CharacterController>();
    32.         anim = GetComponent<Animator>();
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         if(!isRunning)
    38.         return;
    39.  
    40.         if(Time.time - speedIncreaseLastTick > speedIncreaseTime)
    41.         {
    42.             speedIncreaseLastTick = Time.time;
    43.             playerSpeed += speedIncreaseAmount;
    44.             //Change the modifier text
    45.             GameManager.Instance.UpdateModifier(playerSpeed - originalSpeed);
    46.         }
    47.  
    48.         groundedPlayer = controller.isGrounded;
    49.         anim.SetBool("Grounded",groundedPlayer);
    50.         if (groundedPlayer && playerVelocity.y < 0)
    51.         {
    52.             playerVelocity.y = 0f;
    53.         }
    54.  
    55.         //Gather inputs on which lane we should be
    56.         if(MobileInput.Instance.SwipeLeft || Input.GetKeyDown(KeyCode.LeftArrow))
    57.         MoveLane(false);
    58.         if(MobileInput.Instance.SwipeRight || Input.GetKeyDown(KeyCode.RightArrow))
    59.         MoveLane(true);
    60.  
    61.         //Calculate where we should be
    62.         Vector3 targetposition = transform.position.z * Vector3.forward; //Burada Vector3.zero yazmanin bir sey degistirmeyecegini fark ettim cunku z ekseni zaten asagida move.z kisminda tekrar ayarlaniyor
    63.  
    64.         if (desiredLane == 0) //1 Birim sola kay
    65.             targetposition += Vector3.left * LANE_DISTANCE;
    66.         else if (desiredLane == 2) //1 Birim saga kay
    67.             targetposition += Vector3.right * LANE_DISTANCE;
    68.  
    69.        
    70.         //Calculate our move delta
    71.         Vector3 moveVector = Vector3.zero;
    72.         moveVector.x = (targetposition - transform.position).x * playerSpeed;
    73.  
    74.         moveVector.z = playerSpeed;
    75.  
    76.         Vector3 move = new Vector3(moveVector.x, 0,moveVector.z);
    77.         controller.Move(move * Time.deltaTime * playerSpeed);
    78.  
    79.         //Karakteri rotate eden kod
    80.         if (move != Vector3.zero)
    81.         {
    82.             gameObject.transform.forward = move;
    83.         }
    84.  
    85.         // Changes the height position of the player..
    86.         if (Input.GetKeyDown(KeyCode.UpArrow) && groundedPlayer)
    87.         {
    88.             anim.SetTrigger("Jump");
    89.             playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
    90.         }
    91.  
    92.         if (Input.GetKeyDown(KeyCode.DownArrow) && groundedPlayer)
    93.         {
    94.             StartSliding();
    95.         }
    96.  
    97.         if(Input.GetKeyDown(KeyCode.DownArrow))
    98.             {
    99.                 playerVelocity.y -= jumpHeight;
    100.             }
    101.  
    102.         playerVelocity.y += gravityValue * Time.deltaTime;
    103.         controller.Move(playerVelocity * Time.deltaTime);
    104.     }
    105.  
    106.     private void MoveLane(bool goingRight)
    107.     {
    108.         desiredLane += goingRight ? 1 : -1;
    109.         desiredLane = Mathf.Clamp(desiredLane, 0, 2);
    110.     }
    111.  
    112.     public void StartRunning()
    113.     {
    114.         isRunning = true;
    115.         anim.SetBool("Running",isRunning);
    116.     }
    117.  
    118.     private void StartSliding()
    119.     {
    120.         anim.SetBool("Sliding",true);
    121.         Invoke("StopSliding",1.0f);
    122.         controller.height /= 2;
    123.         controller.center = new Vector3(controller.center.x,controller.center.y /2,controller.center.z);
    124.     }
    125.  
    126.     private void StopSliding()
    127.     {
    128.         anim.SetBool("Sliding",false);
    129.         controller.height *= 2;
    130.         controller.center = new Vector3(controller.center.x,controller.center.y *2,controller.center.z);
    131.     }
    132.  
    133.     /*
    134. Su an ki Character Controller ayarlari;
    135. Slope limit : 45 - (hic degismedi)
    136. Step Offset : 0.3 - (hic degismedi)
    137. Skin Width : 0.04
    138. Min Move Distance : 0.001
    139. Center X0 Y1 Z0
    140. Radius : 0.3
    141. Height : 2
    142. */
    143.  
    144. //Robot skin width yuzunden yerden 0.04 yukseklikte durdugu icin havada suzuluyor gibi gozukuyordu ancak robot modelinin root child objesini biraz asagi indirdigim zaman bu sorun cozuldu
    145. //Skin Width'i 0.050 olarak degistirdim cunku animasyon titriyordu bu sebeple Raycast degerinide 0.2f + 0.5f olarak degistirdim cunku 0.05 olunca yerden 0.05 yukarida baslatiyor.
    146.     void OnTriggerEnter(Collider other)
    147.     {
    148.        
    149.         if(other.CompareTag("Coin"))
    150.         {
    151.             GameManager.Instance.GetCoin();
    152.             coinAnim = other.GetComponent<Animator>();
    153.             coinAnim.SetTrigger("Collected");
    154.             //Destroy(other,1.0f);
    155.         }
    156.        
    157.     }
    158.  
    159.     private void Crash()
    160.     {
    161.         anim.SetTrigger("Death");
    162.         isRunning = false;
    163.         GameManager.Instance.OnDeath();
    164.     }
    165.  
    166.     private void OnControllerColliderHit(ControllerColliderHit hit)
    167.     {
    168.         switch(hit.gameObject.tag)
    169.         {
    170.             case "Obstacle":
    171.             Crash();
    172.             break;
    173.  
    174.         }
    175.     }
    176.  
    177.    
    178. }
     
  2. dberkay

    dberkay

    Joined:
    Apr 10, 2021
    Posts:
    4
    I fixed the problem but i don't know how to close the topic.
    My solution was delete lerp code in Camera script
    I know I asked question about the vectors system but it thought it was the thing causing jitter problem so the solution may look irrelavant but actually fixed the problem
     
    Last edited: Oct 1, 2022
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    You can mark your post as resolved. TBH, just stating what you did to solve it is all that's needed, for future readers. :)
     
    dberkay likes this.
  4. dberkay

    dberkay

    Joined:
    Apr 10, 2021
    Posts:
    4
    I couldn't figure how to do it :oops:
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    It doesn't really matter TBH but can you see "Thread tools" at the top of the thread? You can edit the thread and change the thread title prefix to "Resolved".
     
    dberkay likes this.
  6. dberkay

    dberkay

    Joined:
    Apr 10, 2021
    Posts:
    4
    Thanks
     
    MelvMay likes this.