- Home /
This question was
closed Jun 26, 2017 at 09:47 PM by
Hamilcar-games for the following reason:
The question is answered, right answer was accepted
Question by
Hamilcar-games · Jun 12, 2017 at 09:08 AM ·
movementrigidbodyfixedupdaterigidbody.addforcerigidbody.velocity
how to have a fixed movement using rigidbodies without animations
im making a game like crossy road and i want that my player can have a fixed movement
this is my script :
using System.Collections.Generic; using UnityEngine;
public class playermovement : MonoBehaviour {
private bool is_grounded=true;
private int rot;
public Rigidbody rb;
public float speed=10;
public float jump_force;
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetKeyDown (KeyCode.UpArrow)) {
rot = 0;
if (is_grounded == true) {
jump ();
}
transform.eulerAngles = new Vector3(0,rot,0);
rb.velocity = Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKeyDown (KeyCode.DownArrow)) {
rot = 180;
if (is_grounded == true) {
jump ();
}
transform.eulerAngles = new Vector3(0,rot,0);
rb.velocity = -Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKeyDown (KeyCode.RightArrow)) {
rot = 90;
if (is_grounded == true) {
jump ();
}
transform.eulerAngles = new Vector3(0,rot,0);
rb.velocity = Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
rot = -90;
if (is_grounded == true) {
jump ();
}
transform.eulerAngles= new Vector3(0,rot,0);
rb.velocity = Vector3.left * speed * Time.deltaTime;
}
}
void OnCollisionStay(Collision others)
{
if (others.gameObject.tag == "Ground") {
is_grounded = true;
}
}
void jump()
{
//if (is_grounded == true) {
rb.AddForce (new Vector3 (0, jump_force, 0) * Time.deltaTime);
is_grounded = false;
}
}
Comment