Question by
dama1994 · Oct 06, 2019 at 08:54 PM ·
cameramoverotate object
Character rotation for Y equal to the camera
My camera with the mouse rotates to a position, and I want my character to turn the same direction only, but for one reason the camera turns faster than the character, I don't know if it's because the results are doubled.
public class MoviemientoPlayer : MonoBehaviour { private Vector3 movement; Rigidbody rb; public int contadorSaltos, totalSaltos; public float velocidadSalto, velocidad, gravity, arriba, abajo; public GameObject cam, puntoSalto, cams;
public LayerMask layerSuelo;
void Start()
{
rb = GetComponent<Rigidbody>();
cam = GameObject.Find("Main Camera");
puntoSalto = GameObject.Find("PuntoSalto");
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Jump") && contadorSaltos < totalSaltos)
{
rb.velocity = new Vector3(rb.velocity.x, velocidadSalto, rb.velocity.z);
contadorSaltos++;
}
}
private void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if (Physics.OverlapSphere(puntoSalto.transform.position, 0.2f, layerSuelo).Length > 0)
{
contadorSaltos = 0;
velocidadSalto = 14;
}
rb.velocity += Vector3.down * Time.deltaTime * gravity;
Move(h, v);
Rotacion();
}
void Move(float h, float v)
{
movement = new Vector3(h, 0, v);
rb.transform.Translate(movement * velocidad * Time.deltaTime);
}
void Rotacion()
{
//float he = Input.GetAxis("Horizontal");
/* arriba -= velocidadRot * -Input.GetAxis("Mouse X");
transform.eulerAngles = new Vector3(0.0f, arriba, 0.0f);*/
//transform.rotation = Quaternion.Euler(transform.rotation.x, cam.transform.rotation.y, transform.rotation.z);
//transform.Rotate(transform.rotation.x, he, transform.rotation.z);
arriba += cam.gameObject.GetComponent<ROtacionCamara>().velocidadRot * Input.GetAxis("Mouse X");
transform.eulerAngles = new Vector3(0.0f, arriba, 0.0f);
}
public class ROtacionCamara : MonoBehaviour { public float velocidadRot; float arriba, abajo; // Start is called before the first frame update void Start() {
}
// Update is called once per frame
void Update()
{
Rot();
}
void Rot()
{
arriba += velocidadRot * Input.GetAxis("Mouse X");
abajo -= velocidadRot * Input.GetAxis("Mouse Y");
//arriba = Mathf.Clamp(arriba, -60f, 90f);
abajo = Mathf.Clamp(abajo, -90f, 90f);
transform.eulerAngles = new Vector3(abajo, arriba , 0.0f);
}
}
}
Sorry i used Google translator
Thank you so much!.
Comment