I placed the prefabs where they need to be, I’m going to start a proper unity course shortly after I finish the one for c# made by Brackeys. This is part of a code shown to me by someone else.
{
public GameObject Ball;
public Rigidbody2D ballRB;
void move() { // also tried update.
if (ballRB.velocity.x > 0) { // also tried position.
Debug.Log ("Hey") }
}
I have no idea how I’m failing at something so simple.
The ball moves, the script is there, the prefabs are placed on the Paddle A.I., I have no idea why I can’t even get a response.
The function “move”(BTW function should be capitalized, just a consistency thing it’s not mandatory or something)
even if it does work, you are not calling it anywhere, the function Update is a magic-unity-function that runs once a frame update(you have FixedUpdate that runs everytime the physics engine does a tick and many others, see the MonoBehaviour API), that’s why you don’t need to call it yourself
also, velocity and any other vector 3 is made up of 3 floats, if you don’t know the difference between float and int: int is whole nubmers(IE 0,1,2,3…) and float is a number with decimals(floating point, IE 0.3,7.5,2.0…), it may be a casting(converting) issue between them, because you are comparing the y value(float) to 0(int), you need to say “0f” to say it’s a float, better to say 0.0f, but you need the “f” there to say it’s a float
{
public GameObject Ball;
public Rigidbody2D ballrb2d;
public GameObject GOPlayer2;
public Rigidbody2D rb2dPlayer2;
public float Speed = 300f;
[Space]
[Space]
public float ReactionTimeMIN = 0.07f;
public float ReactionTimeMAX = 0.10f;
void Start()
{
//Continuously Invokes Move every x seconds (values may differ)
InvokeRepeating("Move", ReactionTimeMIN, ReactionTimeMAX);
}
// Movement for the paddle
void Move()
{
//checking x direction of the ball
if (ballrb2d.velocity.x > 0.0f)
{
//checking y direction of ball
if (Ball.transform.position.y < GOPlayer2.transform.position.y - 1f)
{
//move ball down if lower than paddle
rb2dPlayer2.velocity = -transform.up * Speed * Time.deltaTime;
}
else if (Ball.transform.position.y > GOPlayer2.transform.position.y + 1f)
{
//move ball up if higher than paddle
float movementDirection = 1f * Speed * Time.deltaTime;
rb2dPlayer2.velocity = new Vector2(0, movementDirection);
}
}
}
}
This is the whole code as given to me, I simply removed everything to try and invoke a response from Debug.Log, tried changing 0 to 0.0f too, still no answer. Also, should probably have figured not to remove the Move mentioned above, but I wanted the minimum of code possible to try and invoke a reaction and… nothing.
Note the InvokeRepeating call in the Start method of your full code. That’s causing the Move function to be called every .1 seconds. Your modified version of this code didn’t include the InvokeRepeating call, so nothing was calling your method.
This is honestly the first I’ve seen of InvokeRepeating. I don’t know why I’d ever use this over a much more clear Coroutine approach.
Anyway, here’s the Coroutine equivalent of what you’re trying to do:
void Start() {
// Kick off this method.
StartCoroutine(MoveRepeatedly());
}
private IEnumerable MoveRepeatedly() {
// Wait a bit before starting.
yield return new WaitForSeconds(ReactionTimeMIN);
// From now on, every ReactionTimeMAX, call Move().
while (true) {
Move();
yield return new WaitForSeconds(ReactionTimeMAX);
}
}
private void Move() {
// Do your move code here.
}
Again, the takeaway here is that you need to call the methods somehow. Your Move() method was never called in your original code.
So I attempted plastering Debug.Log on the ball script itself, which worked perfectly, obviously.
What I can’t understand is why this code:
{
public class PaddleBot : MonoBehaviour
public GameObject Ball;
public Rigidbody2D ballrb2d;
void Start()
{
ballrb2d = GetComponent<Rigidbody2D>();
if (ballrb2d.velocity.x > 0.0f)
Debug.Log("hey");
}
Doesn’t work. Am I trying to access the ball wrong way?
Can you explain what your question is at this point?
As LurkingNinjaDev pointed out, in response to your earlier statement, your previous code wasn’t checking whether the object was “stationary”. It was merely checking whether it was not moving left or right. The object could have been moving up/down, but you were only checking the x-component of its velocity.
If you want to check that the object is not moving, you’d need to check velocity.x and velocity.y are both 0.
The ball, as shown in the script above, has both a random X and a random Y value that happens upon execution, which causes it to move.
The paddle, does not detect any movement.
{
public float difficultyMultiplier = 1.13f;
public float minXSpeed = 0.8f;
public float maxXSpeed = 1.2f;
public float minYSpeed = 0.8f;
public float maxYSpeed = 1.2f;
private Rigidbody2D ballRigidbody;
// Use this for initialization
void Start()
{
ballRigidbody = GetComponent<Rigidbody2D>();
ballRigidbody.velocity = new Vector2(
Random.Range(minXSpeed, maxXSpeed) * (Random.value > 0.5f ? -1 : 1),
Random.Range(minYSpeed, maxYSpeed) * (Random.value > 0.5f ? -1 : 1)
);
}
This is the code of the ball, which has velocity applied upon start.
{
public class PaddleBot : MonoBehaviour
public GameObject Ball;
public Rigidbody2D ballrb2d;
void Start()
{
ballrb2d = GetComponent<Rigidbody2D>();
if (ballrb2d.velocity.x > 0.0f)
Debug.Log("hey");
}
And this is the code of the paddle, where I want it to detect the value from the code above.
I’m just trying to have it tell me “That object you told me about is moving”, but it’s telling me that it’s not moving for some reason, even though in the unity component itself, the velocity clearly shows that x != 0.
The ball is moving in both Y and X, I just want to see if it figures out that the X changes, and the editor shows that it does. But it only answers when I change the sign to ==, which means that it doesn’t detect any movement, even though the ball is constantly moving in both axis.