- Home /
Make object jump to fixed y position
Hi there,
I'm trying to replicate a game for good'ol'practice's sake. It's getting there, but I've had issues with my player/ball movement ever since I started the project.
Here's a little video showing my problem: YOUTUBE LINK
Problem is my ball won't jump a fixed height. In fact, it seems to change the more I move my mouse around. I guess moving in the x axis has an effect on the speed?
Current settings for the players Rigidbody2D:
Current Physics2D settings:
And finally, the script attached to the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallControl : MonoBehaviour {
private bool hasStarted = false;
public GameObject smoke;
public bool hitSomething = false;
private Vector2 bounceTweak;
void Start ()
{
}
void FixedUpdate ()
{
MoveWithMouse();
if(!hasStarted)
{
this.GetComponent<Rigidbody2D> ().gravityScale = 0;
//Wait for a mousepress to launch.
if(Input.GetMouseButtonDown(0))
{
hasStarted = true;
print("Mouse Clicked! Game has started!");
this.GetComponent<Rigidbody2D>().velocity = new Vector2 (0f, 10f);
this.GetComponent<Rigidbody2D> ().gravityScale = 1;
}
}
}
void MoveWithMouse()
{
Vector3 ballPos = new Vector3(0f, transform.position.y, 0f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 6;
ballPos.x = Mathf.Clamp(mousePosInBlocks, 0f,6f);
this.transform.position = ballPos;
}
public void OnCollisionEnter2D(Collision2D col)
{
hitSomething = true;
if (hitSomething == true)
{
print ("WE HIT SOMETHING");
//Vector2 bounceTweak = new Vector2 (Random.Range(0f, 10f), Random.Range(0f, 10f));
//Vector2 tweak = new Vector2 (Random.Range(0f, 0.5f), Random.Range(0f,0.2f));
hitSomething = false;
}
if (hasStarted == true)
{
SmokeEffect();
this.GetComponent<AudioSource>().Play();
//this.GetComponent<Rigidbody2D>().velocity += bounceTweak;
//this.GetComponent<Rigidbody2D>().velocity += tweak;
}
}
void SmokeEffect()
{
GameObject smokePuff = Instantiate(smoke, this.transform.position, Quaternion.identity);
smokePuff.GetComponent<ParticleSystem>().startColor = this.GetComponent<SpriteRenderer>().color;
}
}
I've googled around and bumped into a MoveTowards function. Could that work? If so, how do I limit MoveTowards to just affect one axis?
Cheers!
Answer by Matthewj866 · May 11, 2017 at 10:42 PM
Hi there.
I am unsure as to what is causing the problem, however the way I tend to get things to fixed distances using physics is good old speed = distance / time.
So, when the ball collides with one of the blocks at the bottom, you can simply have it adjust the velocity in FixedUpdate()
so when the collision occurs you set some boolean to true
to send it upwards a fixed distance. You could then recalculate this every frame to make it gradually slow down until it hits that specific point on the Y axis.
Example:
void FixedUpdate()
{
if(isTravellingUp)
{
if(transform.position.y >= vectorForLimit.y)
{
isTravellingUp = false;
}
else
{
_rigidBody.velocity = (vectorForLimit.y - transform.position.y) / yourTimeValue;
}
}
}
You can then just use the transform
to make the ball follow left or right as you were originally doing.
Hope this helps!