Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Correct my code Joaquina

Discussion in 'Unity Hub' started by Joaquina341, Oct 3, 2023.

  1. Joaquina341

    Joaquina341

    Joined:
    Oct 3, 2023
    Posts:
    1
    Hi! I have this code in an open basic world. With this script I'm able to move my character in all directions. I'm new in this and I would like to know how would you improve it.

    Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EmMoc : MonoBehaviour
    6. {
    7.     private Vector3 posi;
    8.     bool jump;
    9.     private Rigidbody rb; // Agregamos un componente Rigidbody para manejar la física
    10.     private Transform cameraTransform; // Referencia a la transformación de la cámara
    11.  
    12.     public float jumpForce = 2f; // Controla la fuerza del salto
    13.     public float movementSpeed = 4f; // Controla la velocidad de movimiento
    14.     public AudioClip oh;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         posi = new Vector3(0, 2f, 0);
    20.         jump = false;
    21.  
    22.         // Obtén el componente Rigidbody
    23.         rb = GetComponent<Rigidbody>();
    24.  
    25.         // Obtén la transformación de la cámara
    26.         Camera mainCamera = Camera.main;
    27.         if (mainCamera != null)
    28.         {
    29.             cameraTransform = mainCamera.transform;
    30.         }
    31.  
    32.         GetComponent<AudioSource> ().playOnAwake = false;
    33.         GetComponent<AudioSource> ().clip = oh;
    34.     }
    35.  
    36.     void OnCollisionEnter(Collision collision)
    37.     {
    38.         if (collision.gameObject.tag == "Terre")
    39.         {
    40.             jump = true;
    41.         }
    42.         if (collision.gameObject.tag == "Caps")
    43.         {
    44.             GetComponent<AudioSource> ().Play ();
    45.             // Get the Renderer component from the new cube
    46.             var cubeRenderer = GetComponent<Renderer>();
    47.  
    48.             // Create a new RGBA color using the Color constructor and store it in a variable
    49.             Color customColor = new Color(0.4f, 0.9f, 0.7f, 1.0f);
    50.  
    51.             // Call SetColor using the shader property name "_Color" and setting the color to the custom color you created
    52.             cubeRenderer.material.SetColor("_Color", customColor);;
    53.         }
    54.     }
    55.  
    56.     // Update is called once per frame
    57.     void Update()
    58.     {
    59.         // Movimiento horizontal
    60.         float horizontalInput = Input.GetAxis("Horizontal"); // Obtén la entrada horizontal (A y D)
    61.         float verticalInput = Input.GetAxis("Vertical"); // Obtén la entrada vertical (W y S)
    62.  
    63.          // Transforma los inputs de acuerdo con la rotación de la cámara
    64.         Vector3 cameraForward = cameraTransform.forward;
    65.         Vector3 cameraRight = cameraTransform.right;
    66.         cameraForward.y = 0; // Elimina la componente vertical para mantenerse en el plano horizontal
    67.         cameraRight.y = 0; // Elimina la componente vertical
    68.  
    69.         Vector3 moveDirection = (cameraForward.normalized * verticalInput + cameraRight.normalized * horizontalInput) * movementSpeed;
    70.         rb.velocity = new Vector3(moveDirection.x, rb.velocity.y, moveDirection.z);
    71.  
    72.        
    73.         // Mantén la rotación constante en los ejes Z y Y
    74.         transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
    75.  
    76.         // Si presionas la tecla "W" o la flecha arriba y el personaje está en el suelo, aplica un impulso en el eje Z
    77.         if (Input.GetKeyDown(KeyCode.Space) && jump)
    78.         {
    79.             rb.AddForce(posi * jumpForce, ForceMode.Impulse);
    80.             jump = false;
    81.         }
    82.     }
    83. }
    84.  
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,204
    Some things I would improve:

    - cache AudioSource instead of calling GetComponent<AudioSource> () every time
    - Use CompareTag instead of tag == string
    - Some say only call rigidbodies, like velocity, in FixedUpdate. I personally don't see an issue with doing this in Update, as it works and I've never had issues with it myself

    I didn't look much into the functionality. I'm more into if it works it works