Este fue mi codigo para resolver el ejercicio del triangulo, es que no entendia muy bien como hacerla asi que intente buscarla por mi cuenta jsjs, que le parece profe? public class PatronCuadrado : MonoBehaviour { public float speed = 2f; public float longitud = 10f; private int fase = 0; void Update() { float velocidad = speed * Time.deltaTime;
switch (fase) { case 0: // Mover a la derecha if (transform.position.x < longitud) { transform.position += new Vector3(2,0,0) * velocidad; } else { fase = 1; // Cambiar a la siguiente fase } break; case 1: // Mover hacia arriba diagonal if (transform.position.z < longitud) { transform.position += new Vector3(0,0,2) * velocidad; transform.position += Vector3.left * velocidad; } else { fase = 2; // Cambiar a la siguiente fase } break; case 2: // Mover hacia abajo diagonal if (transform.position.z > longitud-longitud) { transform.position += new Vector3(0,0,-2) * velocidad; transform.position += Vector3.left * velocidad; } else { fase = 0; // Volver a la primera fase } break; } } }
Le agradecemos inmensamente profesor por siempre colocar estos cursos de excelente calidad y muy buena explicación. Para mi uno de los mejores en RUclips. ¿Me gustaría saber si puedo colocar en mi Curriculum estos cursos luego de terminarlos, utilizando el nombre del Canal? Bendiciones de lo Alto para usted! Gracias por existir!
Mi solucion para el movimiento triangular, se que no uso Switch, pero no es muy eficiente usarlo pensando que se puede solucionar con vectores public class moveTrianglePattern : MonoBehaviour { // Start is called before the first frame update public float speed = 10.0f; // Speed of the movement public float sideLength = 10.0f; // Length of each side of the square private Vector3[] directions; // Directions to move in private int currentDirectionIndex = 0; // Current direction index private float distanceMoved = 0.0f; // Distance moved in current direction void Start() { // Initialize the directions directions = new Vector3[] { Vector3.right, (float)(0.5*Math.Sqrt(3))*Vector3.up + 0.5f*Vector3.left, (float)(0.5*Math.Sqrt(3))*Vector3.down + 0.5f*Vector3.left, }; } // Update is called once per frame void Update() { float movement = speed * Time.deltaTime; transform.Translate(directions[currentDirectionIndex] * movement); distanceMoved += movement; if (distanceMoved >= sideLength) { distanceMoved = 0.0f; // Reset the distance moved currentDirectionIndex = (currentDirectionIndex + 1) % directions.Length; // Switch to the next direction } } } El cuadrado es muy similar, cambiando el vector
void Update() { // Puntos en X y Y de las coordenadas del movimiento Diagonal Izquierda. float y_finalDiagIz = calcularAltura_y((double)lado); float x_fianlDiaIqz = hallarX(lado) * (-1);
// Calcular el movimiento en cada fps. float y_mov = y_finalDiagIz / velocidad; float x_mov = x_fianlDiaIqz / velocidad;
// Enlazar movimiento movDiagonalArribaIzquierda = new Vector3(x_mov,y_mov, 0); movDiagonalAbajoIzquierda = new Vector3(x_mov, y_mov * (-1), 0); speed = velocidad * Time.deltaTime; Vector3 pivot = direction switch { derechaAbajo => movDerechaAbajo, diagonalArribaIzquierda => movDiagonalArribaIzquierda, diagonalAbajoIzquierda => movDiagonalAbajoIzquierda, init => inicio, _ => throw new System.NotImplementedException() }; transform.position += pivot * speed; move = transform.position; if ( direction == init) {direction = derechaAbajo;Debug.Log("1.Moving to Right " + move);} else if(direction == derechaAbajo && move.x >= inicio.x + lado) { direction = diagonalArribaIzquierda; Debug.Log("2.Moving to Up and Left " + move);} else if (direction == diagonalArribaIzquierda && move.x >= x_fianlDiaIqz && move.y >= y_finalDiagIz) { direction = diagonalAbajoIzquierda; Debug.Log("3.Moving to Down and Left " + move + " Altura: " + y_finalDiagIz); } if ( direction == diagonalAbajoIzquierda && move.x
Este fue mi codigo para resolver el ejercicio del triangulo, es que no entendia muy bien como hacerla asi que intente buscarla por mi cuenta jsjs, que le parece profe?
public class PatronCuadrado : MonoBehaviour
{
public float speed = 2f;
public float longitud = 10f;
private int fase = 0;
void Update()
{
float velocidad = speed * Time.deltaTime;
switch (fase)
{
case 0: // Mover a la derecha
if (transform.position.x < longitud)
{
transform.position += new Vector3(2,0,0) * velocidad;
}
else
{
fase = 1; // Cambiar a la siguiente fase
}
break;
case 1: // Mover hacia arriba diagonal
if (transform.position.z < longitud)
{
transform.position += new Vector3(0,0,2) * velocidad;
transform.position += Vector3.left * velocidad;
}
else
{
fase = 2; // Cambiar a la siguiente fase
}
break;
case 2: // Mover hacia abajo diagonal
if (transform.position.z > longitud-longitud)
{
transform.position += new Vector3(0,0,-2) * velocidad;
transform.position += Vector3.left * velocidad;
}
else
{
fase = 0; // Volver a la primera fase
}
break;
}
}
}
Le agradecemos inmensamente profesor por siempre colocar estos cursos de excelente calidad y muy buena explicación. Para mi uno de los mejores en RUclips.
¿Me gustaría saber si puedo colocar en mi Curriculum estos cursos luego de terminarlos, utilizando el nombre del Canal?
Bendiciones de lo Alto para usted! Gracias por existir!
Puedes hacerlo, aunque realmente no tendrás un certificado oficial de completado
Mi solucion para el movimiento triangular, se que no uso Switch, pero no es muy eficiente usarlo pensando que se puede solucionar con vectores
public class moveTrianglePattern : MonoBehaviour
{
// Start is called before the first frame update
public float speed = 10.0f; // Speed of the movement
public float sideLength = 10.0f; // Length of each side of the square
private Vector3[] directions; // Directions to move in
private int currentDirectionIndex = 0; // Current direction index
private float distanceMoved = 0.0f; // Distance moved in current direction
void Start()
{
// Initialize the directions
directions = new Vector3[] {
Vector3.right,
(float)(0.5*Math.Sqrt(3))*Vector3.up + 0.5f*Vector3.left,
(float)(0.5*Math.Sqrt(3))*Vector3.down + 0.5f*Vector3.left,
};
}
// Update is called once per frame
void Update()
{
float movement = speed * Time.deltaTime;
transform.Translate(directions[currentDirectionIndex] * movement);
distanceMoved += movement;
if (distanceMoved >= sideLength)
{
distanceMoved = 0.0f; // Reset the distance moved
currentDirectionIndex = (currentDirectionIndex + 1) % directions.Length; // Switch to the next direction
}
}
}
El cuadrado es muy similar, cambiando el vector
Mi solución para el Primero 😅:
public float velocidad = 5f, magnitud = 10f;
private bool arriba = false, derecha = false, puntoInicial = true;
private float movimientoX = 0, movimientoY = 0, stop = 0;
void Start()
{
}
void Update()
{
if (transform.position.x > magnitud && puntoInicial == true) derecha = true;
if (transform.position.y > magnitud) arriba = true;
if (transform.position.x < magnitud) derecha = false;
if (transform.position.y < magnitud) arriba = false;
if (derecha == false && arriba == false && puntoInicial == true )
{
UnityEngine.Debug.Log("1 Derecha abajo");
movimientoX = velocidad * Time.deltaTime;
movimientoY = stop * Time.deltaTime;
}
if (derecha == true && arriba == false )
{
UnityEngine.Debug.Log("2 arriba derecha");
movimientoX = stop * Time.deltaTime;
movimientoY = velocidad * Time.deltaTime;
}
if(derecha == true && arriba == true )
{
UnityEngine.Debug.Log("3 izquierda arriba");
movimientoX = -velocidad * Time.deltaTime;
movimientoY = stop * Time.deltaTime;
puntoInicial = false;
}
if (derecha == false && arriba == true && transform.position.x
Mi solución del Movimiento triangular, hice lo que pude:
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using System;
using static UnityEditor.PlayerSettings;
public class MovTriangular : MonoBehaviour
{
public float velocidad = 5f, lado = 10f;
private float speed;
private Vector3 inicio, move;
private const string init = "", derechaAbajo = "DERECHA-ABAJO", diagonalArribaIzquierda = "DIAGONAL-ARRIBA-IZQUIERDA", diagonalAbajoIzquierda = "DIAGONAL-ABAJO-IZQUIERDA";
private string direction = init;
private Vector3 movDerechaAbajo = new Vector3(1, 0, 0), movDiagonalArribaIzquierda = new Vector3(-1, 1, 0), movDiagonalAbajoIzquierda = new Vector3(-1, -1, 0);
private float calcularAltura_y(double l)
{
double number = 3.0;
double result = (Mathf.Sqrt((float)number) / 2.0) * l;
return (float)result;
}
private float hallarX(float l)
{
return l / 2;
}
void Start()
{
inicio = transform.position;
}
void Update()
{
// Puntos en X y Y de las coordenadas del movimiento Diagonal Izquierda.
float y_finalDiagIz = calcularAltura_y((double)lado);
float x_fianlDiaIqz = hallarX(lado) * (-1);
// Calcular el movimiento en cada fps.
float y_mov = y_finalDiagIz / velocidad;
float x_mov = x_fianlDiaIqz / velocidad;
// Enlazar movimiento
movDiagonalArribaIzquierda = new Vector3(x_mov,y_mov, 0);
movDiagonalAbajoIzquierda = new Vector3(x_mov, y_mov * (-1), 0);
speed = velocidad * Time.deltaTime;
Vector3 pivot = direction switch
{
derechaAbajo => movDerechaAbajo,
diagonalArribaIzquierda => movDiagonalArribaIzquierda,
diagonalAbajoIzquierda => movDiagonalAbajoIzquierda,
init => inicio,
_ => throw new System.NotImplementedException()
};
transform.position += pivot * speed;
move = transform.position;
if ( direction == init) {direction = derechaAbajo;Debug.Log("1.Moving to Right " + move);}
else if(direction == derechaAbajo && move.x >= inicio.x + lado) { direction = diagonalArribaIzquierda; Debug.Log("2.Moving to Up and Left " + move);}
else if (direction == diagonalArribaIzquierda && move.x >= x_fianlDiaIqz && move.y >= y_finalDiagIz) { direction = diagonalAbajoIzquierda; Debug.Log("3.Moving to Down and Left " + move + " Altura: " + y_finalDiagIz); }
if ( direction == diagonalAbajoIzquierda && move.x
Mi solución Seguimiento y Esquivo:
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class SeguirEsquivar : MonoBehaviour
{
private Vector3 Cubo1Position, Cubo2Position;
public GameObject Cubo2;
public float distanciaSeguimiento = 9f;
public float distanciaEsquivo = 3f;
private float velocidad = 2f;
private float speed = 0;
private const int estatico = 0, seguimiento = 1, esqquivo = 2;
private int fase = estatico;
private Color colorRojo = Color.red, colorVerde = Color.green, colorAzul = Color.blue;
private Renderer Cubo1Properties, Cubo2Properties;
void Start()
{
transform.Translate(0, 3, 0);
Cubo2.transform.Translate(10, 3, 0);
Cubo2Properties = Cubo2.GetComponent();
Cubo2Properties.material.color = colorVerde;
Cubo1Properties = GetComponent();
Cubo1Properties.material.color = colorVerde;
}
void Update()
{
Cubo1Position = transform.position;
Cubo2Position = Cubo2.transform.position;
speed = velocidad * Time.deltaTime;
float x = Math.Abs(Cubo1Position.x - Cubo2Position.x);
float z = Math.Abs(Cubo1Position.z - Cubo2Position.z);
Vector3 movimiento = Vector3.zero;
// Seguimiento
fase = estatico;
if (x distanciaEsquivo && z Cubo2Position.x) movimiento += new Vector3((1 / velocidad) * (-1), 0, 0);
if (Cubo1Position.z < Cubo2Position.z) movimiento += new Vector3(0, 0, 1 / velocidad);
if (Cubo1Position.z > Cubo2Position.z) movimiento += new Vector3(0, 0, (1 / velocidad) * (-1));
Cubo2Properties.material.color = colorRojo;
Cubo1Properties.material.color = colorAzul;
Debug.Log("Seguimiento"); break;
case esqquivo:
if (Cubo1Position.x < Cubo2Position.x) movimiento += new Vector3((1 / velocidad) * (-1), 0, 0);
if (Cubo1Position.x > Cubo2Position.x) movimiento += new Vector3(1 / velocidad, 0, 0);
if (Cubo1Position.z < Cubo2Position.z) movimiento += new Vector3(0, 0, (1 / velocidad) * (-1));
if (Cubo1Position.z > Cubo2Position.z) movimiento += new Vector3(0, 0, 1 / velocidad);
Cubo2Properties.material.color = colorAzul;
Cubo1Properties.material.color = colorRojo;
Debug.Log("Esquivo"); break;
case estatico:
Cubo2Properties.material.color = colorVerde;
Cubo2Properties.material.color = colorVerde;
Debug.Log("Estático"); break;
}
transform.position += movimiento.normalized * speed;
}
}
Mi solucion para el tercer ejercico c: public class HideNSearch : MonoBehaviour
{
public GameObject Capsule1, Capsule2;
private Color Capsule1Color, Capsule2Color;
public float SPEED = 1f;
public float MIN_DISTANCE = 2f;
public float MAX_DISTANCE = 10f;
public float DISTANCE = 0;
void Start() {
DISTANCE = Vector3.Distance(Capsule1.transform.position, Capsule2.transform.position);
Capsule1Color = Capsule1.GetComponent().material.color;
Capsule2Color = Capsule2.GetComponent().material.color;
}
void Update() {
DISTANCE = Vector3.Distance(Capsule1.transform.position, Capsule2.transform.position);
if (DISTANCE > MAX_DISTANCE) {
Capsule1.GetComponent().material.color = Color.gray;
Capsule2.GetComponent().material.color = Color.gray;
return;
}
Capsule1.GetComponent().material.color = Capsule1Color;
Capsule2.GetComponent().material.color = Capsule2Color;
Vector3 MoveVector = Vector3.MoveTowards(Capsule2.transform.position, Capsule1.transform.position,
(DISTANCE < MIN_DISTANCE ? -SPEED : SPEED) * Time.deltaTime
);
if (DISTANCE > MIN_DISTANCE - .3f && DISTANCE < MIN_DISTANCE + .3f) {
MoveVector = Capsule2.transform.position;
}
Capsule2.transform.position = MoveVector;
}
}