Hello. The player I’ve created moves too fast, and it also seems to gain speed as it walks. The script I used is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class JugadorController : MonoBehaviour {
private Rigidbody rb;
private int contador;
public Text textoContador, textoGanar;
public float turnSpeed = 10f;
public float velocidad;
Animator m_Animator;
Rigidbody m_Rigidbody;
Vector3 m_Movement;
Quaternion m_Rotation = Quaternion.identity;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
contador = 0;
setTextoContador();
textoGanar.text = "";
m_Animator = GetComponent<Animator> ();
m_Rigidbody = GetComponent<Rigidbody> ();
}
void FixedUpdate () {
float movimientoH = Input.GetAxis("Horizontal");
float movimientoV = Input.GetAxis("Vertical");
Vector3 movimiento = new Vector3(movimientoH, 0.0f, movimientoV);
rb.AddForce(movimiento * velocidad);
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
m_Movement.Set(horizontal, 0f, vertical);
m_Movement.Normalize ();
bool hasHorizontalInput = !Mathf.Approximately (horizontal, 0f);
bool hasVerticalInput = !Mathf.Approximately (vertical, 0f);
bool isWalking = hasHorizontalInput || hasVerticalInput;
m_Animator.SetBool ("IsWalking", isWalking);
Vector3 desiredForward = Vector3.RotateTowards (transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f);
m_Rotation = Quaternion.LookRotation (desiredForward);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Coleccionable"))
{
other.gameObject.SetActive (false);
contador = contador + 1;
setTextoContador();
}
}
void setTextoContador(){
textoContador.text = "Contador: " + contador.ToString();
if (contador >= 5){
textoGanar.text = "You won";
}
}
void OnAnimatorMove ()
{
m_Rigidbody.MovePosition (m_Rigidbody.position + m_Movement * m_Animator.deltaPosition.magnitude);
m_Rigidbody.MoveRotation (m_Rotation);
}
}
What might be the problem??