Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Script for a ball

Discussion in 'Scripting' started by Josedelgado98, May 1, 2024.

  1. Josedelgado98

    Josedelgado98

    Joined:
    Sep 14, 2023
    Posts:
    4
    I am looking to make a script for a ball to act a lot like the ball in lethal league blaze, where each time it is hit it gets faster and faster and doesn't hit the player who hit. At the moment I have a basic script for it to make it move when it but the physics of the ball getting hit is also very weird and inconsistent any suggestions for making the code? am also new to scripting


    ]{
    // Drag and drop Rigidbody in Inspector
    public Rigidbody ball;
    public Vector3 velocity;


    void Start()
    {
    // Add force once at start
    ball.AddForce(Vector3.forward * 0.0f, ForceMode.VelocityChange);
    }

    void Update()
    {
    // Track velocity, it holds magnitude and direction (for collision math)
    velocity = ball.velocity;

    }

    void OnCollisionEnter(Collision collision)
    {
    // Magnitude of the velocity vector is speed of the object (we will use it for constant speed so object never stop)
    float speed = velocity.magnitude;

    // Reflect params must be normalized so we get new direction
    Vector3 direction = Vector3.Reflect(velocity.normalized, collision.contacts[0].normal);

    // Like earlier wrote: velocity vector is magnitude (speed) and direction (a new one)
    ball.velocity = direction * speed;






    }



    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,005
    I would start with basic tutorials on moving stuff around in Unity. There might even be a tutorial for doing exactly what you want.

    If there is no tutorial for "lethal league blaze" then you'll have to triangulate to a similar motion that there IS a tutorial for and begin to understand the elemental parts of that movement. Every form of movement is unique.


    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    You can use IgnoreCollision to make the ball not collide with the player that last hit the ball. Or you can use Layers.

    To make the ball bounce around indefinitely you should add a new physics material to the ball and set the material's friction to 0 and the Bounciness to 1. Then set Friction Combine to minimum and Bounce Combine to maximum.

    When it comes to actually batting the ball around I suspect the game doesn't use collision detection. Instead when the player performs a move it will use the player's input and position in relation to the ball's position to determine in which direction to send the ball.

    I've never played the game and so this is a total guess.
     
  4. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    150
    I think zulo3d's suggestion for using the built-in physics bounces is the easiest solution for now. Once the ball reaches Lethal League velocities you may need a different solution, but this is a good place to start.

    For your original code, I think you need to be careful about the difference between Update() and FixedUpdate(). Update happens at the render rate and FixedUpdate happens in sync with the physics rate. I think that methods like OnCollisionEnter happen when physics is processed, so somewhat in sync with FixedUpdate.

    In your code, you are sampling velocity in Update and then using that value in OnCollisionEnter. This could be causing some weird interactions between when physics calculates the new rigidbody velocity value and when you are sampling the velocity value. In general, it is best to do anything related to physics and rigidbodies in FixedUpdate().
     
  5. Josedelgado98

    Josedelgado98

    Joined:
    Sep 14, 2023
    Posts:
    4
    Alright thanks for the suggestions, I will try them out and see how they work, sorry for the late replies I only work on this game in class
     
  6. Josedelgado98

    Josedelgado98

    Joined:
    Sep 14, 2023
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Move : MonoBehaviour
    6. {
    7.     // Drag and drop Rigidbody in Inspector
    8.     public Rigidbody ball;
    9.     public Vector3 velocity;
    10.    
    11.  
    12.     void Start()
    13.     {
    14.         // Add force once at start
    15.         ball.AddForce(Vector3.forward * 0.0f, ForceMode.VelocityChange);
    16.     }
    17.    
    18.     void Update()
    19.     {
    20.        
    21.     }
    22.  
    23.  
    24.  
    25.      void FixedUpdate()
    26.     {
    27.         velocity = ball.velocity;
    28.  
    29.  
    30.     }
    31.  
    32.  
    33.     void OnCollisionEnter(Collision collision)
    34.     {
    35.         // Magnitude of the velocity vector is speed of the object (we will use it for constant speed so object never stop)
    36.         float speed = velocity.magnitude;
    37.  
    38.         // Reflect params must be normalized so we get new direction
    39.         Vector3 direction = Vector3.Reflect(velocity.normalized, collision.contacts[0].normal);
    40.  
    41.         // Like earlier wrote: velocity vector is magnitude (speed) and direction (a new one)
    42.         ball.velocity = direction * speed;
    43.  
    44.  
    45.      
    46.  
    47.        
    48.  
    49.     }
    50.  

    this is the new script all I did was change the ball velocity to fixed and hitting the ball is a lot more consistent, The bat no longer clips through it, would I need to put the code for when the ball goes faster after getting hit in void update? am also using 2 sphere colliders one on the ball and the bat to hit it around for now this game is suppose to be a very rough version of it nothing to fancy since it is a school project and sorry if am asking very simple things to code in the teacher decided to not teach us scripts
     
  7. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    150
    If the ball is going to be going very fast, then you should set the Collision Detection mode to Continuous on the ball's Rigidbody.

    It may still be worth experimenting with commenting out your OnCollisionEnter function to see how the ball bounces on its own.

    Do you have a script on the bat which will send the ball in a different direction when it is hit? Or are you just relying on the ball bouncing off of the bat's collider in the same way that it is bouncing off of walls?
     
  8. Josedelgado98

    Josedelgado98

    Joined:
    Sep 14, 2023
    Posts:
    4
    Currently i have no script telling the ball where to go if it gets hit, its going off of the colliders which I need to change, as well as the bat's collider going away when the player is not swinging, I have been watching videos on basic scripting so am trying to make a mock up script at the moment, but commenting out the code you suggested doesn't seem to affect much, thanks for the help