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!