Holas.
Tengo un problema :(
Supongamos que en la ventana Scene ajusto la rotación del player, de modo que cuando se inicie el juego, él comience mirando hacia 'x' dirección...
Bueno, pues resulta que al iniciar el juego, el jugador ignora la rotación que yo le asigné y en su lugar se orienta hacia el eje Z... siempre.
Mirad esta pequeña secuencia (https://i.gyazo.com/cc77a18ef9051282e99fd6179823541b.mp4) de ejemplo.
Este es el script del jugador:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
private static CharacterController _cc;
private static float _currentSpeed = 2f;
private static bool _canMove = true;
public static CharacterController CC { get { return _cc; } set { _cc = value; } }
private void Awake() {
_cc = GetComponent<CharacterController>();
}
private void Update() {
float hAxis = Input.GetAxis("Horizontal") * _currentSpeed;
float vAxis = Input.GetAxis("Vertical") * _currentSpeed;
Vector3 movement = new Vector3(hAxis, PlayerJump.Gravity, vAxis);
movement = transform.rotation * movement;
if (_canMove)
_cc.Move(movement * Time.deltaTime);
}
}
A ver si alguien me echa el guante.
Muchas gracias, de verdad (:
Hola Null lo vi por arriba y pense que asignabas algun transform.rotation pero NO, solo tenes Move del characterController, no entiendo porque hace eso, revisaste si es el Unico script que tenes, que pudiera estar creando el problema? probaste quitando todo el scirpt, desactivandolo, y ver si ocurre lo mismo? podra ser algun scirpt de la camara?
saludos
1º Para realizar un movimiento con "Motion" tienes que tener un AXIS en modo RAW.
2º Debes normalizar los axis.
3º Transformar la dirección del jugador en el sentido adelante.
4º Multiplicar el de dirección por la velocidad.
5º Utilizar CharacterController.Move con nuestro vector de movimiento.
6º Aplicar simulación de gravedad en el eje Y.
private float m_speed = 10;
private float m_gravity = 20;
private Vector3 m_move = Vector3.zero;
private Vector3 m_axis = Vector3.zero;
private void Update()
{
//1
m_axis.x = Input.GetAxisRaw("Horizontal");
m_axis.z = Input.GetAxisRaw("Vertical");
//2
m_axis.Normalize();
//3
m_move = transform.TransformDirection(m_axis);
//4
m_move *= m_speed;
//5
m_controller.Move(m_move * Time.deltaTime);
//6
m_move.y -= m_gravity * Time.deltaTime;
}
Proba de esta manera, aunque esto no afecta la rotación en ningún aspecto.
Hola @lightbug! Desactivé todos los scripts y probé uno por uno. Sí, el causante parece ser el script de la cámara, el cual es este:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
private static bool _canMoveCamera = true;
private float _xSensitivity = 1.5f;
private float _ySensitivity = 1.5f;
private float _xRot;
private float _yRot;
private float _xRotSpeed;
private float _yRotSpeed;
private float _currentXRot;
private float _currentYRot;
private float _smoothTime = .055f;
public static bool CanMoveCamera { get { return _canMoveCamera; } set { _canMoveCamera = value; } }
private void Update() {
if (_canMoveCamera) {
_xRot -= Input.GetAxis("Mouse Y") * _xSensitivity;
_yRot += Input.GetAxis("Mouse X") * _ySensitivity;
_xRot = Mathf.Clamp(_xRot, -70f, 70f);
_currentXRot = Mathf.SmoothDamp(_currentXRot, _xRot, ref _xRotSpeed, _smoothTime);
_currentYRot = Mathf.SmoothDamp(_currentYRot, _yRot, ref _yRotSpeed, _smoothTime);
transform.rotation = Quaternion.Euler(_currentXRot, _currentYRot, 0f);
transform.parent.rotation = Quaternion.