Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

Ayuda con esta script por favor

Iniciado por Lucky-chan, Mayo 19, 2018, 06:06:47 PM

Tema anterior - Siguiente tema
Hola querìa saber como hago para usar esta misma script pero cambiar el rigidbody por un character controller, gracias de antemano
 

[RequireComponent(typeof(Rigidbody))]
public class ThirdPersonControllerCustom : NetworkBehaviour {
Rigidbody _rb;
Animator _anim;
Transform _Cam;
FreeLookCam _FreeCam;
 
public float Velocity;
// Use this for initialization
void Awake () {
   _Cam = Camera.main.transform;
   _rb = GetComponent<Rigidbody> ();
   _anim = GetComponent<Animator> ();
   _FreeCam = GameObject.FindObjectOfType<FreeLookCam> ();
}

// Update is called once per frame
void Update ()
{
   if (!isLocalPlayer)
      return;
   if (_FreeCam.Target != transform)
      _FreeCam.SetTarget(transform);
 
   if (_anim.GetCurrentAnimatorStateInfo (0).IsName ("Locomotion"))
   {
      float vertical = Input.GetAxis ("Vertical");
      if (vertical > 0) {         
         _anim.SetFloat ("Velocidad", vertical);
         transform.eulerAngles = new Vector3 (transform.eulerAngles.x, _Cam.eulerAngles.y, transform.eulerAngles.z);
         _rb.velocity = transform.forward * Velocity * vertical * (Input.GetKey (KeyCode.LeftShift) ? 2 : 1);
      }
      if (Input.GetMouseButtonDown (0)) {
         _anim.SetTrigger ("Ataque");
                Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
      }
        }
   
}
    }

 
 

hola, si solo usas del "_rb" la "velocity" es muy facil:
 

//tan facil como cambiar
Rigidbody _rb;
//por
CharacterControler _rb;
//y cambiar
_rb = GetComponent<Rigidbody>();
//por
_rb = GetComponent<CharacterControler>();

 
 

Cita de: Igor date=1526747949hola, si solo usas del "_rb" la "velocity" es muy facil:
   
   

//tan facil como cambiar
Rigidbody _rb;
//por
CharacterControler _rb;
//y cambiar
_rb = GetComponent<Rigidbody>();
//por
_rb = GetComponent<CharacterControler>();

   
       
   


gracias, eso ya lo habìa intentado pero no funciona gracias a esta parte
 

if (vertical > 0) {         
         _anim.SetFloat ("Velocidad", vertical);
         transform.eulerAngles = new Vector3 (transform.eulerAngles.x, _Cam.eulerAngles.y, transform.eulerAngles.z);
         _rb.velocity = transform.forward * Velocity * vertical * (Input.GetKey (KeyCode.LeftShift) ? 2 : 1);
}

 
específicamente en _rb.velocity, por que me dice que el velocity es de solo lectura

no se porque quieres usar un characterControler...
 
es mejor hacerte tu uno.
 
asi puede ser "personalizado" justo como quieres que sea....
 
 
 
si no te deja cambiar la velocity...
 
prueva con 
 

_rb.SimpleMove(transform.forward * .....);

 
 

Cita de: Lucky-chan date=1526746007Hola querìa saber como hago para usar esta misma script pero cambiar el rigidbody por un character controller, gracias de antemano
   

Cita de: Lucky-chan date=1526746007_rb.velocity = transform.forward * Velocity * vertical * (Input.GetKey (KeyCode.LeftShift) ? 2 : 1);
   


Hola, no hay mucha vuelta, si sabés algo de algebra vectorial no es más que "alimentar" un vector a un componente, sea la variable X del Componente Y o la Z del componenete W, en la API está muy clarito esto (y como se realiza el movimiento):
 
https://docs.unity3d.com/ScriptReference/CharacterController.Move.html
 
-> Dice :
 


   A more complex move function taking absolute movement deltas.

 


   Attempts to move the controller by motion, the motion will only be constrained by collisions. It will slide along colliders. CollisionFlags is the summary of collisions that occurred during the Move. This function does not apply any gravity.


    


   Es decir, no es exactamente lo mismo, fijate que das deltas de posición, y no velocidades, es decir, vos tenés absoluto control de donde va a estar el personaje.


   La velocity que te da CharacterController la usas a modo de saber cuanto vale, no recuerdo si la podés escribir, aunque no tiene mucho sentido e internamente un character controller mueve al personaje a su modo, así que realizá TODOS los calculos vectoriales antes, y realizá una sola vez el Move.


   Note: The velocity returned is simply the difference in distance for the current timestep before and after a call to CharacterController.Move or CharacterController.SimpleMove. The velocity is relative because it won't track movements to the transform that happen outside of the CharacterController (e.g. character parented under another moving Transform, such as a moving vehicle).


    


   ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░


    


   Entonces haciendo Euler te queda:


// ΔPos = Vel * ΔTiempo
deltaPos = Vel * Time.deltaTime;
characterController.Move( deltaPos );


   Ah! y por favor por Jesus santo y la virgen maría, si vas a usar un characterController dedicá 3 segundos de tu vida a ponerle un nombre acorde y no Rigidbody plis .


    


   Saludos


    

Etiquetas: