Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Infinite_Gamer · Jan 25, 2014 at 04:44 AM · c#movement

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Infinite_Gamer · Jan 25, 2014 at 06:08 AM 0
Share

Ok thanks

avatar image Infinite_Gamer · Jan 25, 2014 at 06:18 AM 0
Share

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

avatar image MDragon · Jan 25, 2014 at 07:07 AM 0
Share

Seek no more! http://forum.unity3d.com/threads/175410-Finally-an-interactive-tutorial-series-that-WILL-$$anonymous$$ch-you-C-for-Unity3D

avatar image Infinite_Gamer · Jan 25, 2014 at 05:25 PM 0
Share

Thanks!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges