Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_00413E49C2684EBC7B13 · Mar 29, 2021 at 09:16 PM · ballrespawnbreakoutpaddle

Help with respawning paddle and ball in Breakout 3D,Respawning a paddle and ball in Breakout game

I'm making a breakout game in 3D and it's my first time making a game and using Unity so I'm a bit clueless. I've got to the point where my game works fine up until the ball goes off the screen and into the "dead zone". Can someone advise how to respawn the paddle and ball together and carry on with the game? I've included my ball and paddle scripts below, I have a script for the bricks as well but not sure that was relevant. I also made a prefab of the ball and paddle together but no idea what to do with it. Thanks to anyone who can help :)


Code for my ball

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class BallScript : MonoBehaviour
 {
 public Rigidbody rbody;

 public float MinVertMovement = 0.1f;
 public float MinSpeed = 10f;
 public float MaxSpeed = 10f;
 private bool hasBeenLaunched = false;

 void Start()
 {

 }
 private float minVelocity = 10f;
 private Vector3 lastFrameVelocity;

 void FixedUpdate()
 {

   if (hasBeenLaunched == false)
   {
     if (Input.GetKey(KeyCode.Space))
     {
       Launch();
     }
   }

   if (hasBeenLaunched)
   {
     Vector3 direction = rbody.velocity;
     direction = direction.normalized;
     float speed = direction.magnitude;
     if (direction.y>-MinVertMovement && direction.y <MinVertMovement)
     {
       direction.y = direction.y < 0 ? -MinVertMovement : MinVertMovement;
       direction.x = direction.x < 0 ? -1 + MinVertMovement : 1 - MinVertMovement;
       rbody.velocity = direction * MinSpeed;
     }

     if (speed<MinSpeed || speed>MaxSpeed)
     {
       speed = Mathf.Clamp(speed, MinSpeed, MaxSpeed);
       rbody.velocity = direction*speed;
     }
   }
   lastFrameVelocity = rbody.velocity;

 }

 void OnCollisionEnter(Collision collision)
 {

   Bounce(collision.contacts[0].normal);

 }

 private void Bounce(Vector3 collisionNormal)
 {
   var speed = lastFrameVelocity.magnitude;
   var direction = Vector3.Reflect(lastFrameVelocity.normalized, collisionNormal);
   Debug.Log("Out Direction: " + direction);
   rbody.velocity = direction * Mathf.Max(speed, minVelocity);
 }

 public void Launch()
  {
     rbody = GetComponent<Rigidbody>();

     Vector3 randomDirection = new Vector3(-5f, 10f, 0);
     randomDirection = randomDirection.normalized * MinSpeed;
     rbody.velocity = randomDirection;
     transform.parent = null;
     hasBeenLaunched = true;
   }


 }



Code for my paddle

 public class PaddleScript : MonoBehaviour
 {
 private float moveSpeed = 15f;

 void Start()
 {

 }

 void Update()
 {
     if (Input.GetKey(KeyCode.RightArrow) && transform.position.x<9.5)
       transform.Translate(moveSpeed *Input.GetAxis("Horizontal")*Time.deltaTime, 0f, 0f);
     if (Input.GetKey(KeyCode.LeftArrow) && transform.position.x>-7.5)
       transform.Translate(moveSpeed *Input.GetAxis("Horizontal")*Time.deltaTime, 0f, 0f);

 }


}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

113 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change direction of ball based on where it hits paddle -- 3d breakout game 0 Answers

Scoring not working 2 Answers

How to make a thread(string) streatchable? 0 Answers

How do you make a shoot up feature like in dx-ball? 2 Answers

When my Player respawns, it does not stop moving (it's a ball). 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges