Issue With Jumping
I have an issue where when a ball sprite isn't rolling it will jump as I expect it to; however, when I have the ball rolling, it doesn't fall properly or reach the height is should be. Here is my script, using UnityEngine; using System.Collections;
public class BallController : MonoBehaviour {
Vector3 pos;
float posSmooth = 1.0f;
float rotSmooth = 0.318f;
public bool rollingEnabled = true;
public Rigidbody2D rb;
public GameObject sprite;
float thrust = 600f;
Vector2 zero = new Vector2(0f, 0f);
bool jumping = false;
// Use this for initialization
void Start ()
{
pos = transform.position;
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown (KeyCode.Space))
{
jumping = true;
}
}
void FixedUpdate ()
{
if(jumping)
{
jumping = false;
rb.velocity = zero;
rb.AddForce (transform.up * thrust);
}
if(rollingEnabled)
{
pos.x += posSmooth * Time.deltaTime;
transform.position = pos;
sprite.transform.Rotate (Vector3.back, 180.0f * Time.deltaTime * rotSmooth, Space.Self);
}
}
}
I thought it might be an issue with Update or FixedUpdate interfering with the other, but after setting my code up like I probably should've in the first place, nothing change.
Answer by Kodoka · Aug 30, 2015 at 06:46 PM
I still don't know why it wasn't working the way I had it, but I have found a solution, so if anyone else runs into this issue, at least here is a work around. You can, of course, change the direction of the movement by modifying the values of the Vector3, movement.
Vector3 movement = new Vector3(1.0f, 0.0f, 0.0f); //Goes just after MonoBehaviour.
transform.position += movement *Time.deltaTime; //Goes in fixed update.
Your answer
Follow this Question
Related Questions
[C#] Can't Control Movement While Jumping 0 Answers
Jump and movement doesn't works simultaneously 1 Answer
COMPILER ERROR NOT FOUND 1 Answer