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;
}
}
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.
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;
}
}
}
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.