Question by
CamRidesBikes · Jun 15, 2020 at 03:46 AM ·
c#pong
Random start movement for pong ball. Ball moves only up and down on x axes.
Here is my code the problem is in the line after comment. It has no errors but does not move on both x and y just x. using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float speed;
public Rigidbody2D rb;
public Vector3 startPostition;
void Start()
{
startPostition = transform.position;
Launch();
}
public void Reset()
{
rb.velocity = Vector2.zero;
transform.position = startPostition;
Launch();
}
private void Launch()
{
//problem
float x = Random.Range(0, 2) == 0 ? 1 : -1;
float y = Random.Range(0, 2) == 0 ? 1 : -1;
rb.velocity = new Vector2(x * speed, y * speed);
}
}
Comment
Your answer
Follow this Question
Related Questions
Accessing A Variable from another script C# 2 Answers
2D Enemy AI problems. 1 Answer
Update instatiate doesn't work? 1 Answer