Player Movement Freezes in Unity3D [Please help, no answers!]
Hello all, I have created a player controller script like this:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Rigidbody rb;
public Animator anim;
public float jumpPower;
private bool grounded;
private float distToGround;
public float hits;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
void OnCollisionStay(Collision collisionInfo)
{
grounded = true;
}
void OnCollisionExit(Collision collisionInfo)
{
grounded = false;
}
// Update is called once per frame
void Update () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
rb.velocity = new Vector3(moveHorizontal *speed, rb.velocity.y, moveZ*speed);
if(Input.GetKeyDown(KeyCode.Space) && grounded == true)
{
Jump();
}
}
void Jump()
{
rb.AddForce(Vector3.up * jumpPower);
// animation.Play("jump_pose");
}
void FixedUpdate()
{
//
}
}
This script, however, does not seem to be working. It usually ends up freezing the player where you cannot move the player at all with any keys. This is demonstrated here: https://embed.gyazo.com/768346efa3444041f574b65912f852c2.gif It is also demonstrated here showing the movement before it: https://gyazo.com/1b516af1ae6b493297139a7ad9ca61f1
Could you please advise me on how to fix this? I would appreciate any help.
Thanks,
8Development
Answer by Zoogyburger · Feb 17, 2016 at 06:37 PM
I made some minor changes to your script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
private Animator anim;
public float jumpPower;
private bool grounded;
private float distToGround;
public float hits;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
void OnCollisionStay(Collision collisionInfo)
{
grounded = true;
}
void OnCollisionExit(Collision collisionInfo)
{
grounded = false;
}
// Update is called once per frame
void Update () {
float input_x = Input.GetAxisRaw("Horizontal");
float input_y = Input.GetAxisRaw("Vertical");
anim.SetFloat("x", input_x);
anim.SetFloat("y", input_y);
transform.position += new Vector3(input_x, input_y, 0).normalized * Time.deltaTime;
}
void FixedUpdate(){
if(Input.GetKey (KeyCode.Space)){
rb.AddForce(Vector3.up * jumpPower);
}
}
}
Your answer
Follow this Question
Related Questions
How to move 3 players at the same time in V form,How to move 3 characters in the same time in V form 0 Answers
Reduce Player Movement in Air 0 Answers
My player wont move and i dont know why 1 Answer
Player only moves when the key is pressed, not when it's held. 0 Answers
Moving forward seems to be specific to the original direction of the object being controlled 0 Answers