- Home /
The question is answered, right answer was accepted
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;
}
}
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.
@STUDIOCRAFTapps oh that works perfectly! do you have a way to make the jumps better as well?
I'm currently searching way to do it... I'll probably reply the solution in one hour or two... #WorstEnglishEver...
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.
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.
@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?
try this ins$$anonymous$$d
myRigidBody.velocity += new Vector3(0, jumpPower, 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;
}
}
}
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.