Question by
danmct1995 · Apr 28, 2020 at 02:34 AM ·
unity 2drigidbody2dvelocityscripting beginner
Rigidbody not able to be linked to object within unity for some reason?
I am attempting to link my rigid body here, but for some reason it will not show up. I plan to change the velocity of an object when it reaches a specific speed on the x & y axes. I am attaching relevant code below along with a picture of the area:
/* THIS IS TO CORRECT THE Y AXIS BUG */
public Rigidbody rb;
private bool ballIsStuck = false;
/* Variables over */
void Start()
{
paddleToTheBall = transform.position - paddle1.transform.position;
myAudioSource = GetComponent<AudioSource>();
// rigidbody to rb
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{ // ball lock & stick
if (!hasStarted)
{
LockBallToPaddle();
LaunchOnClick();
}
}
public void FixedUpdate()
{
// Ball velocity adjustment to prevent loop
if (rb.velocity.x > -0.1 && rb.velocity.x < 0.1)
{
rb.velocity = new Vector3(0.5f, 0.5f, 0f);
ballIsStuck = true;
}
if (rb.velocity.y > -0.1 && rb.velocity.x < 0.1)
{
rb.velocity = new Vector3(0.5f, 0.5f, 0f);
ballIsStuck = true;
}
}
rigidbody.png
(57.2 kB)
Comment
Best Answer
Answer by danmct1995 · Apr 28, 2020 at 02:54 AM
I found the problem, I needed to recognize it as a 2D instead of a regular rigidbody component.
Your answer
Follow this Question
Related Questions
Instantiate GameObject with Velocity 1 Answer
Enemys velocity in 2D platformer 0 Answers
WHY IK affects Rigidbody???? 0 Answers