- Home /
Change in position going up not smooth
When the object is moving down the transition is smooth, but when click the mouse the transition up is sudden. Here's my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour {
Vector3 velocity;
public Vector3 gravity;
bool didFlap = false;
public Vector3 flapVelocity;
// Use this for initialization
void Start () {
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
didFlap = true;
}
}
// Update is called once per frame
void FixedUpdate () {
velocity += gravity * Time.deltaTime;
if (didFlap == true)
{
didFlap = false;
velocity += flapVelocity;
}
transform.position = velocity * Time.deltaTime;
}
}
Comment
Answer by unit_nick · Oct 14, 2017 at 01:45 PM
use Time.fixedDeltaTime in FixedUpdate
// Update is called once per frame
void FixedUpdate () {
velocity += gravity * Time.fixedDeltaTime;
if (didFlap == true)
{
didFlap = false;
velocity += flapVelocity;
}
transform.position = velocity * Time.fixedDeltaTime;
}
Your answer
Follow this Question
Related Questions
C# Smoothing Out transform.Translate 0 Answers
How to stop player velocity completely 1 Answer
Vector3 wont move to right position 0 Answers
Complex 2D movement (X,Y) using velocity. 1 Answer
How to smooth my camera (Vector3.Lerp) 0 Answers