- Home /
Elevator Motion Going Up Smooth, Going Down Not Smooth
Hey all, I'm having trouble wracking my brain trying to figure out why my elevator going up is smooth but coming down it is very jerky in motion. Any help would be greatly appreciated. Thanks for your time.
using UnityEngine;
using System.Collections;
public class ElevatorController : MonoBehaviour {
public float elevator_speed;
public bool going_down = true;
public bool going_up = false;
public GameObject Elevator1;
// Use this for initialization
void Start ()
{
going_up = false;
elevator_speed = 0;
}
void OnCollisionStay2D(Collision2D coll)
{
var xfer = GetComponent<Transform>();
if (coll.gameObject.tag == "hero")
if (going_down == true)
{
elevator_speed = 2;
xfer.Translate(Vector3.down.normalized * elevator_speed * Time.deltaTime); //going down is herky jerky
}
else if (going_down == false && xfer.position.y < -0.57f)
{
//gameObject {Get Elevator1}
//elevator = gameObject.tag == "elevator";
elevator_speed = 2;
xfer.Translate(Vector3.up * elevator_speed * Time.deltaTime); //going up is smooth
}
if (coll.gameObject.tag == "elevator_bottom")
{
going_down = false; // does not work
elevator_speed = 0; // does not work
}
}
}
Could it be cause of the normalization? "Vector3.down.normalized" Why not just Vector3.down?
You can also try copying your going up code and making it negative:
xfer.Translate(-Vector3.up * elevator_speed * Time.deltaTime);
Vector3.down.normalized is the same as Vector3.down. So, pointless, but also harmless.
Thank you very much JoeStrout! Being very new to Unity I hadn't thought about that. @Jessespike - I was just trying different things and left normalized on there.
Answer by JoeStrout · Apr 22, 2015 at 05:24 PM
I bet it's because you're doing this motion in OnCollisionStay2D. The elevator only moves when something is colliding with it. Going up, it continues to collide with whatever's on top of it, so you get (mostly by accident) smooth motion. Going down, it departs from whatever's on top of it, stops moving, waits for whatever that is to fall down and hit it, and then moves again.
You should move your elevator in the Update (or perhaps FixedUpdate) method instead. If you need it to respond in some way to things colliding with it, then use the collision events only to set the flags that make it move (e.g. going_up and going_down).
Your answer
Follow this Question
Related Questions
Decrease players health depending on distance of enemy. 1 Answer
2D Sprite in a 3D Game 1 Answer
2d physics problem 1 Answer
Projectile speed changing by distance to target 1 Answer
Should I use Vector3D in a 2D game? 1 Answer