Home > Blockchain >  having problems with unity transform
having problems with unity transform

Time:04-02

So, I made a basic movement script with wallrunning and wanted to add dashing to it, at first I made parameters for the character to dash and testing it with debug.log worked as intended, but the actual dash command, which was transform.translate(Vector3.forward), didn't work for some reason.

This is the code:

public class PlayerMovement : MonoBehaviour
{

    public Transform player;
    
    public CharacterController controller;
    
    public float speed = 12f;
    public float baseSpeed;
    
    Vector3 velocity;
    
    public float gravity = -9.81f;
    
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    
    public Transform wallCheckL;
    public Transform wallCheckR;
    public float wallDistanceL = -0.4f;
    public float wallDistanceR = 0.4f;
    public LayerMask wallMask;
    public bool touchWallL;
    public bool touchWallR;
    
    public float SlideTime = 10f;
    
    public float Ynormal;
    
    
    
    public bool isGrounded;
    public bool Sprinting;
    public bool Crouching;
    public bool Sliding;
    public bool canDash;
    
    // Start is called before the first frame update
    void Start()
    {
        baseSpeed = speed;
        canDash = true;
    }
    
    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    
        touchWallL = Physics.CheckSphere(wallCheckL.position, wallDistanceL, wallMask);
        touchWallR = Physics.CheckSphere(wallCheckR.position, wallDistanceR, wallMask);
    
    
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }
    
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
    
        Vector3 move = transform.right * x   transform.forward * z;
    
        controller.Move(move * speed * Time.deltaTime);
    
        velocity.y  = gravity * Time.deltaTime;
    
        controller.Move(velocity * Time.deltaTime);
    
        if ((isGrounded == true) && Input.GetKeyDown("space"))
        {
            velocity.y = 10f;
        }
    
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            speed = baseSpeed * 1.5f;
            Sprinting = true;
        }
        if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            speed = baseSpeed;
            Sprinting = false;
        }
    
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            Crouching = true;
        }
        if (Input.GetKeyUp(KeyCode.LeftControl))
        {
            Crouching = false;
        }
    
        if (Crouching)
        {
            transform.localScale = new Vector3(1f, 0.5f, 1f);
    
        } else if (Crouching == false)
        {
            transform.localScale = new Vector3(1f, 1f, 1f);
        }
    
        if (Sprinting && Crouching)
        {
            Sliding = true;
        } else if (Sprinting == false && Crouching == false)
        {
            Sliding = false;
        }
    
        if (Sliding)
        {
            speed = baseSpeed * 2;
            transform.localScale = new Vector3(1f, 0.25f, 1f);
        }
    
        Ynormal = transform.localEulerAngles.y;
    
        if (touchWallL)
        {
            gravity = -4.4f;
            transform.localRotation = Quaternion.Euler(0, Ynormal, -20f);
            isGrounded = true;
        } else if (touchWallR)
        {
            gravity = -4.4f;
            transform.localRotation = Quaternion.Euler(0f, Ynormal, 20f);
            isGrounded = true;
        }
        else
        {
            gravity = -9.81f;
            transform.localRotation = Quaternion.Euler(0f, Ynormal, 0f);
        }
    
        if (touchWallR && Input.GetKeyDown(KeyCode.Space))
        {
            velocity.y = 10f;
            transform.position  = Vector3.left * Time.deltaTime * 100;
        }
    
        if (Input.GetKeyDown(KeyCode.E) && canDash)
        {
            Dash();
        }
    
    }
    
    void Dash()
    {
        StartCoroutine(dashTimer());
        player.transform.Translate(Vector3.forward * Time.deltaTime * speed);
    }
    
    IEnumerator dashTimer()
    {
        canDash = false;
        yield return new WaitForSeconds(2f);
        canDash = true;
        
    }
}

The requirements themselves work, I did some testing, but the dash itself didn't. I tried controller.move, transform.position = transform.position, even making a game object empty called dash distance and trying to teleport my character there, but none of it worked, all of it resulted in my character just not doing anything when I tried to dash.

CodePudding user response:

This is due that Transform.Translate actually needs to be updated each frame.

Try refactoring a bit like this:

// add another control flag
private bool isDashing = false;

void Update()
{
    //...
    if (Input.GetKeyDown(KeyCode.E) && canDash)
    {
        StartCoroutine(dashTimer());
    }

    if (isDashing)
    {
        Dash();
    }
    
}

void Dash()
{
    player.transform.Translate(Vector3.forward * Time.deltaTime * speed);
}

IEnumerator dashTimer()
{
    canDash = false;
    isDashing = true;
    yield return new WaitForSeconds(2f);
    canDash = true;
    isDashing = false;   
}
  • Related