i need help using 2 codes in Unity

im starting a new proyect in Unity and I want my character to jump and move at the same time but the codes don't work well together. the character finds it difficult to move. both work perfectly separately

this is the code im using to move around:

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;

public class PlayerMovement : MonoBehaviour  
{  
    public float speed = 30f;              
    public float turnSpeed = 80f;  
    
    private float horizontalInput;          
    private float verticalInput;
    
    
    
    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");   //teclas
        verticalInput = Input.GetAxis("Vertical");
        
        transform.Translate(speed*Time.deltaTime*Vector3.forward*verticalInput);    //movimiento
        transform.Rotate(turnSpeed*Time.deltaTime*Vector3.up*horizontalInput);      //rotar
    }
}

and this one is a tutorial i found for jumping:

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;   
  
public class JumpControl : MonoBehaviour  
{  
    private CharacterController controller;  
    private float verticalVelocity;  
    private float gravity = 14.0f;  
    private float jumpForce = 10.0f;  
    
    void Start()  
    {  
        controller = GetComponent<CharacterController>();  
    }  
  
    private void Update(){  
        if (controller.isGrounded)  
        {  
            verticalVelocity = -gravity * Time.deltaTime;  
            if (Input.GetKeyDown(KeyCode.Space))  
            {  
                verticalVelocity = jumpForce;  
            }  
        }  
        else  
        {  
            verticalVelocity -= gravity * Time.deltaTime;  
        }  
  
        Vector3 moveVector = new Vector3(0, verticalVelocity, 0);  
        controller.Move(moveVector * Time.deltaTime);  
    }  
}  


Read more here: https://stackoverflow.com/questions/67928963/i-need-help-using-2-codes-in-unity

Content Attribution

This content was originally published by Nicolas Hume at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: