Hey People,
I have a problem with the Rigidbody.
I want use it for move main player…
But, I want to set the max velocity…
I want create one opposite force to set constant velocity when velocity are going to max value,
but in my code don’t work.
Ty for the help, and sorry for my bad english.
public class basicMovment : MonoBehaviour {
private Rigidbody m_CharacterController;
private Vector3 m_MoveDir;
private Vector3 j_MoveDir;
public float strong;
public float strong2;
public float slowDownSpeed;
public float velocity;
public float maxvelocity;
// Use this for initialization
void Start () {
m_CharacterController = GetComponent<Rigidbody>();
m_MoveDir.x = 0f;
m_MoveDir.y = 0f;
m_MoveDir.z = 0f;
j_MoveDir.x = 0f;
j_MoveDir.y = 0f;
j_MoveDir.z = 0f;
velocity = m_CharacterController.velocity.z;
}
// Update is called once per frame
void Update () {
//m_CharacterController.velocity = new Vector3 (0f, 0f, 5f);
velocity = m_CharacterController.velocity.z;
if (Input.GetKey ("w")) {
m_MoveDir.z=+1;
}
if (Input.GetKey ("s")) {
m_MoveDir.z=-1;
}
if (Input.GetKey ("a")) {
m_MoveDir.x=-1;
}
if (Input.GetKey ("d")) {
m_MoveDir.x=+1;
}
if (Input.GetKey ("space")) {
j_MoveDir.y=+1;
}
m_CharacterController.AddRelativeForce (m_MoveDir.normalized * Time.deltaTime * strong);
m_MoveDir = new Vector3 (0f, 0f, 0f);
m_CharacterController.AddRelativeForce (j_MoveDir * Time.deltaTime * strong2);
j_MoveDir = new Vector3 (0f, 0f, 0f);
if (velocity >= 0)
if (velocity > maxvelocity) {
m_CharacterController.AddRelativeForce (m_MoveDir.normalized * Time.deltaTime * -strong);
Debug.Log ("hello");
}
/*if(velocity<0)
if (Mathf.Abs(velocity) > maxvelocity)
m_CharacterController.velocity = new Vector3 (m_CharacterController.velocity.x, m_CharacterController.velocity.y, -maxvelocity);
//Distance() devo lavorare su distance mi sa; e uso il normalize;
*/
/*if(Input.GetKeyUp("w")){
m_CharacterController.velocity =new Vector3(0f, 0f,m_CharacterController.velocity.z/4);
}
if (Input.GetKey ("s")) {
m_CharacterController.AddRelativeForce (m_MoveDir2 * Time.deltaTime * strong2);
}
if(Input.GetKeyUp("s")){
m_CharacterController.velocity =new Vector3(0f, 0f,m_CharacterController.velocity.z/4);
}*/
}
}