- Home /
Help on Player Movement Script C#
I have a problem with my script.When I have this script on my player and I put rigidbody on it and play the game and then I try to jump the cube freaks out and starts to flip flop evertywhere. Here is my script.
using UnityEngine; using System.Collections;
public class Movement : MonoBehaviour { //Variables Start
//Public Variables
public float PlayerSpeedWalk = 5.0f;
public float PlayerSpeedSprint = 8.0f;
public float PlayerSpeedCrawl = 3.0f;
public float PlayerSpeedCrouch = 2.0f;
public float PlayerJumpHeight = 2.0f;
public bool IsSprint = false;
public bool IsCrawl = false;
public bool IsCrouch = false;
//Private Variables
//Variables End
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//Walking Movement
if(IsSprint == false && IsCrawl == false && IsCrouch == false)
{
float moveX = Input.GetAxis("Horizontal") * PlayerSpeedWalk * Time.deltaTime;
float moveY = Input.GetAxis("Jump") * PlayerJumpHeight * Time.deltaTime;
float moveZ = Input.GetAxis("Vertical") * PlayerSpeedWalk * Time.deltaTime;
transform.Translate(moveX,moveY,moveZ);
}
//Sprint Bool
if(Input.GetKeyDown(KeyCode.LeftShift) && IsSprint == false)
{
IsSprint = true;
}
else if(Input.GetKeyUp(KeyCode.LeftShift) && IsSprint == true)
{
IsSprint = false;
}
//Sprint Movement
if(IsSprint == true)
{
float moveXS = Input.GetAxis("Horizontal") * PlayerSpeedSprint * Time.deltaTime;
float moveYS = Input.GetAxis("Jump") * PlayerJumpHeight * Time.deltaTime;
float moveZS = Input.GetAxis("Vertical") * PlayerSpeedSprint * Time.deltaTime;
transform.Translate(moveXS,moveYS,moveZS);
}
//Crawl Bool
if(Input.GetKeyDown(KeyCode.Z) && IsCrawl == false)
{
IsCrawl = true;
}
else if(Input.GetKeyUp(KeyCode.Z) && IsCrawl == true)
{
IsCrawl = false;
}
//Crawl Movement
if(IsCrawl == true)
{
float moveXCl = Input.GetAxis("Horizontal") * PlayerSpeedCrawl * Time.deltaTime;
float moveZCl = Input.GetAxis("Vertical") * PlayerSpeedCrawl * Time.deltaTime;
transform.Translate(moveXCl,0,moveZCl);
}
//Crouch Bool
if(Input.GetKeyDown(KeyCode.C) && IsCrouch == false)
{
IsCrouch = true;
}
else if(Input.GetKeyUp(KeyCode.C) && IsCrouch == true)
{
IsCrouch = false;
}
//Crouch Movement
if(IsCrouch == true)
{
float moveXC = Input.GetAxis("Horizontal") * PlayerSpeedCrouch * Time.deltaTime;
float moveZC = Input.GetAxis("Vertical") * PlayerSpeedCrouch * Time.deltaTime;
transform.Translate(moveXC,0,moveZC);
}
}
}
I looked over other script and I dont see anything that could cause this problem.It might be because I am new to unity and to C#.
So I guess the thing I am asking is how do you fix this problem?
Answer by MDragon · Jan 25, 2014 at 05:44 AM
Are you using the Character Controller? Even if you're not, a rigidbody is meant to be used with forces and such. Your code is not using any features of a rigidbody, so you should remove the rigidbody itself. The Character Controller is made in mind for replacing the rigidbody, essentially.
Also, if you're using some feature of the rigidbody that's absolutely necessary and you can't do without, then at least turn off "Use Gravity" and turn on "Is Kinematic" (which basically tells Unity not to use forces with this object and its rigidbody).
Hope it helps!
I think I am going to make a new movement script based off of what you just told me.
This will be the first time I make a rigid-body code so if anyone knows of a good C# tutorial for this type of stuff it would help a lot.Or if you have advice with this.
Thanks
Infinite Gamer
Your answer
![](https://koobas.hobune.stream/wayback/20220613131355im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
Distribute terrain in zones 3 Answers
Multi-player Moving Script Help 0 Answers
[Answered]Rigid body Movement C# 0 Answers