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. Dismiss Notice

i need help with 3d objects and Time.delta time

Discussion in 'Scripting' started by djlevana18, Jan 13, 2021.

  1. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    i have problem i have runner game when i put another game object or move position y of existing object it changes fwdspeed i want to make it constant
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [System.Serializable]
    5. public enum SIDE {Left, Mid, Right}
    6.  
    7.  
    8. public class playermove : MonoBehaviour
    9. {
    10.     public SIDE m_side = SIDE.Mid;
    11.     float NewXpos = 0f;
    12.     [HideInInspector]
    13.     public bool SwipeLeft, SwipeRight;
    14.     public bool Swipeup, Swipedown;
    15.  
    16.     public float JumpPower = 7f;
    17.     private float y;
    18.     public float XValue;
    19.     public bool Injump;
    20.     public bool InRoll;
    21.     private CharacterController m_char;
    22.     private float x;
    23.     public float speeddodge;
    24.     private float ColHight;
    25.     private float ColCenterY;
    26. //forward speed that's what changing if i add another 3D object value it's the same but speed is different
    27.     float FwdSpeed = 0.8f;
    28.    
    29.  
    30.     void Start()
    31.     {
    32.         m_char = GetComponent<CharacterController>();
    33.         ColHight = m_char.height;
    34.         ColCenterY = m_char.center.y;
    35.         transform.position = Vector3.zero;
    36.     }
    37.     void Update()
    38.     {
    39.         SwipeLeft = Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow);
    40.        
    41.         SwipeRight = Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow);
    42.         Swipeup = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow);
    43.         Swipedown = Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow);
    44.         if (SwipeLeft)
    45.         {
    46.             if(m_side == SIDE.Mid)
    47.             {
    48.                 NewXpos = -XValue;
    49.                 m_side = SIDE.Left;
    50.             }else if(m_side == SIDE.Right)
    51.             {
    52.                 NewXpos = 0;
    53.                 m_side = SIDE.Mid;
    54.             }
    55.         }
    56.         else if (SwipeLeft)
    57.         {
    58.             if (m_side == SIDE.Left)
    59.             {
    60.                 NewXpos = -XValue + -2;
    61.                 m_side = SIDE.M_Left;
    62.             }
    63.             else if (m_side == SIDE.Left)
    64.             {
    65.                 NewXpos = -XValue;
    66.                 m_side = SIDE.Left;
    67.             }
    68.         }
    69.         else if (SwipeRight)
    70.         {
    71.             if (m_side == SIDE.Mid)
    72.             {
    73.                 NewXpos = XValue;
    74.                 m_side = SIDE.Right;
    75.             }
    76.             else if (m_side == SIDE.Left)
    77.             {
    78.                 NewXpos = 0;
    79.                 m_side = SIDE.Mid;
    80.             }
    81.         }
    82.         x = Mathf.Lerp(x, NewXpos, Time.deltaTime * speeddodge);
    83.         Vector3 MoveVector = new Vector3(x - transform.position.x, y*Time.deltaTime, FwdSpeed);
    84.         m_char.Move(MoveVector);
    85.         Jump();
    86.         Roll();
    87.  
    88.     }
    89.     public void Jump()
    90.     {
    91.         if (m_char.isGrounded)
    92.         {
    93.           Injump = false;
    94.             if (Swipeup)
    95.             {
    96.                 y = JumpPower;
    97.                 Injump = true;
    98.  
    99.             }
    100.         }
    101.         else
    102.         {
    103.             y -= JumpPower * 2 * Time.deltaTime;
    104.             if (m_char.velocity.y < 2f)
    105.             {
    106.  
    107.             }
    108.  
    109.         }
    110.     }
    111.     internal float RollCounter;
    112.     public void Roll()
    113.     {
    114.         RollCounter -= Time.deltaTime;
    115.         if(RollCounter <= 0)
    116.         {
    117.             RollCounter = 0f;
    118.             m_char.center = new Vector3(0, ColCenterY, 0);
    119.             m_char.height = ColHight;
    120.             InRoll = false;
    121.         }
    122.         if (Swipedown)
    123.         {
    124.             RollCounter = 0.2f;
    125.             y -= 10f;
    126.             m_char.center = new Vector3(0, ColCenterY/2f, 0);
    127.             m_char.height = ColHight/2f;
    128.             InRoll = true;
    129.             Injump = false;
    130.          
    131.  
    132.         }
    133.     }
    134. }
    135.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Can't really tell what you're asking, but you need to multiply FwdSpeed by Time.deltaTime if you want it to be a smooth constant motion over time.
     
  3. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    well problem is i have a runner game so whenever i put obsticles on my plane speed of player changes so i'm designin level when you should avoid at certain rithm so i design my level but during design if i add more obstacles or changing position of obstacles it runs at different speed
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    All I can say is that the code you have currently will move your character forward at 0.8 meters per frame. Since framerate is not consistent, that does not represent a consistent forward speed over time. Adding or removing objects from your game world may be changing your framerate and therefore your character's movement speed. If you multiply the speed by Time.deltaTime, it changes from a per-frame speed to a per-second speed, no matter the framerate.
     
  5. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    omg it worked as second i did not know about that thank you man a lot i did not know it was issue it was so simple i feel really dumb