Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Aug 09, 2016 at 07:54 PM by willleo10 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by willleo10 · Aug 08, 2016 at 09:44 PM · c#movementtransform3djumping

a better movement code C#

i have a movement script that works but it feels so bad to play and i wanted to see if anyone have a better solution to my code.

using UnityEngine; using System.Collections;

public class movement : MonoBehaviour {

 private bool grounded = true;
 public float jumpPower = 190;
 public float Power = 10f;
 private bool hasJumped = false;
 public Rigidbody rb;

 // Use this for initialization
 void Start () 
 {
     rb = GetComponent<Rigidbody>();
 }

 // Update is called once per frame
 void Update () 
 {
     // Do something

     if(!grounded && rb.velocity.y == 0) {
         grounded = true;
     }
     if (Input.GetKey (KeyCode.W) && grounded == true) {
         hasJumped = true;
     }

     if (Input.GetKey(KeyCode.D))
     {
         transform.Translate(Vector3.right * Power * Time.deltaTime, Space.World);
             
     }

     if (Input.GetKey(KeyCode.A))
     {
         transform.Translate(Vector3.right * -Power * Time.deltaTime, Space.World);

     }
 }

 void FixedUpdate (){
     if(hasJumped){
         rb.AddForce (transform.up*jumpPower);
         grounded = false;
         hasJumped = false;
     }
 }
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

3 Replies

  • Sort: 
avatar image
0

Answer by StuwuStudio · Aug 08, 2016 at 10:55 PM

Hi, To make your player feel better to control, you should try to replace transform.Translate by rb.velocity = Vector2.left/right * Power; That will add acceleration and slow down time, not a linear movement.

Comment
Add comment · Show 7 · 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 willleo10 · Aug 08, 2016 at 11:14 PM 0
Share

@STUDIOCRAFTapps oh that works perfectly! do you have a way to make the jumps better as well?

avatar image StuwuStudio willleo10 · Aug 08, 2016 at 11:37 PM 0
Share

I'm currently searching way to do it... I'll probably reply the solution in one hour or two... #WorstEnglishEver...

avatar image StuwuStudio willleo10 · Aug 08, 2016 at 11:45 PM 0
Share

Oh! I found a bug: replace rb.velocity = Vector2... with rb.velocity = new Vector2(Power or -Power, rb.velocity.y); $$anonymous$$oving will no longer affect gravity.

avatar image StuwuStudio willleo10 · Aug 09, 2016 at 12:09 PM 0
Share

After hour of thinking: Try to do like Super $$anonymous$$ario, longer you press the button, longer player will jump. A fast press will make the player jump low.

Show more comments
avatar image willleo10 · Aug 09, 2016 at 05:35 PM 0
Share

@mrpmorris when i do that it makes the player shake weird when it touches another object. and i can't move and jump at the same time?

avatar image mrpmorris willleo10 · Aug 09, 2016 at 10:33 PM 0
Share

try this ins$$anonymous$$d

 myRigidBody.velocity += new Vector3(0, jumpPower, 0);

avatar image
0

Answer by angelx3 · Aug 09, 2016 at 01:30 PM

Best way is use Coroutine

 public class BetterMovement : MonoBehaviour
 {
     public Transform target;
     public float Distance = 8.5f;
     public float Lift = 2.5f;
     public bool cameraTurnIsFixed = false;
     public float dampingPosition = 30.0f;
     public float dampingRotation = 30.0f;
     public Vector3 velosity;
     private bool _isTurnOnPricel = false;
 
     void Update()
     {
         if (target)
         {
             var targetPosition = (target.position + target.up * Lift - target.forward * Distance);
             if (Vector3.Distance(transform.position, targetPosition) > 0.05f)
             {
                 StopCoroutine("Movement");
                 StartCoroutine("Movement", targetPosition);
             }
         }
     }
 
     IEnumerator Movement(Vector3 newTargetPosition)
     {
         while (Vector3.Distance(transform.position, newTargetPosition) > 0.05f)
         {
             transform.position = Vector3.SmoothDamp(transform.position, newTargetPosition, ref velosity, Time.deltaTime * dampingPosition);
             transform.eulerAngles = target.transform.eulerAngles;
             yield return null;
         }
     }
 }
Comment
Add comment · 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
0

Answer by Holy_Crapf · Aug 09, 2016 at 07:05 PM

Well first off, don't move an object with a rigibody by translating or setting its transform position. Instead you have a couple of options for horizontal movement:

  • Set the Rigidbody velocity equal to your input multiplied by an acceleration or speed.

  • If you want something similar to transform.Translate(), then you can use Rigidbody.MovePosition().

Also make sure to set the velocity or use moveposition inside FixedUpdate(), but check for the input inside Update() like you are doing with jumping.

For jumping, you can do something like this:

 public class Movement : MonoBehaviour
 {
     Rigidbody Body;
     
     bool JumpEnter;
     bool JumpExit;
     bool Grounded;
     
     public float MinJumpForce;
     public float MaxJumpForce;
     
     void Update()
     {
         JumpEnter = false;
         JumpExit = false;
         
         //Check if it was pressed down.
         if(Input.GetKeyDown(KeyCode.W) && Grounded)
             JumpEnter = true;
         //Check if key was released.
         if(Input.GetKeyUp(KeyCode.W))
             JumpExit = true;
     }
     
     void FixedUpdate()
     {
         if(JumpEnter)
         {
             //Set velocity Y to a jumpforce.
             Body.velocity = new Vector2(Body.velocity.x, MaxJumpForce);
         }
         if(JumpExit)
         {
             //If key is released and Y is > MinJumpForce 
             //Then set it equal to MinJumpForce.
             if(Body.velocity.y > MinJumpForce)
                 Body.velocity = new Vector2(Body.velocity.x, MinJumpForce);
         }
     }
 }

This allows for variable jump heights. This is all bare minimum stuff, and you can find some more complex platformer physics with a few google searches. Hope this helps get you in the direction though.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Modifying the Transform of a GameObject 1 Answer

How do i maintain the same speed in the air? 1 Answer

How can i synchronize prefab with NetworkTranform? 1 Answer

Problem with jumping 2 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