Question by
rowanfitz5 · May 25, 2019 at 07:02 AM ·
rigidbody2daddcomponentinput.getkey
How do I change when my RigidBody 2D initializes? C# Question/Noob,How do I change when rigidbody initializes?
Currently, the ball immediately drops when i play the game, not waiting for me to press space. I want the ball to stay in a constant position until I press the space bar.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movestartscreen : MonoBehaviour
{
public GameObject ballz;
public Rigidbody2D ballzRigidBody;
public Vector2 joe;
public int x;
public int y;
// Start is called before the first frame update
void Start()
{
ballzRigidBody = ballz.AddComponent<Rigidbody2D>();
ballzRigidBody.mass = 0;
}
// Update is called once per frame
void Update()
{
transform.position = new Vector2(x, y);
if (Input.GetKeyDown("space")) {
x = x + 400;
y = y + 400;
ballzRigidBody.mass = 1;
}
}
}
Comment
Best Answer
Answer by Hellium · May 25, 2019 at 07:04 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movestartscreen : MonoBehaviour
{
public GameObject ballz;
public Rigidbody2D ballzRigidBody;
public Vector2 joe;
public int x;
public int y;
void Awake()
{
ballzRigidBody = ballz.AddComponent<Rigidbody2D>();
ballzRigidBody.Sleep();
}
// Update is called once per frame
void Update()
{
transform.position = new Vector2(x, y);
if (Input.GetKeyDown("space"))
{
x = x + 400;
y = y + 400;
if( ballzRigidBody.IsSleeping() )
ballzRigidBody.WakeUp();
}
}
}
Your answer