Unity 2d PONG tutorial has confusing mention in code section - c#

I'm following this tutorial to create PONG in Unity 2d :-
http://unity.grogansoft.com/beginners-guide-create-pong-clone-in-unity-part-6/
And understand the code for the most part, but this section confuses me. I have highlighted the confusing part in bold. I can't see in any of the code examples where the name of the ball is being checked? What am I missing?
Code:
void OnCollisionExit2D(Collision2D other)
{
float adjust = 5 * direction;
other.rigidbody.velocity = new Vector2(other.rigidbody.velocity.x, other.rigidbody.velocity.y + adjust);
}
We make sure the item hitting the paddle is the ball by checking
its name, then we apply a force to its rigidbody in the direction
of the paddle’s movement. This also has the pleasant side effect of
adding a little extra speed to the ball, making it faster and faster
as the game goes on.

I think you are correct in your thinking: they don't really "check the name". But, to clarify for you without really having gone through the tutorial, the code you quote appears to be the "Paddle" class ("PaddleScript"?).
The input parameter "other" is the ball--the only object that can strike the paddle.
So, their text is a bit misleading. Perhaps there was supposed to be another object floating around.

Related

How can i get the Direction where my Object is moving?

I have started to make Pacman and would like to know how can i write in my code the direction where the Ghost is going ? So if the transform.position.y is growing, its obvious Up Direction and so on....
This is for the Direction Change if they're hit the Wall.
Any suggestions?
It depends on how you've got your game set-up. One manual way you could go about it is by saving the position in a frame, and in the next frame you calculate the difference between the two positions (the previous frame and the actual frame), and divide that by the time that has passed (Time.deltaTime).
Another way you could go about it (and I would recommend if possible) is simply getting the Rigidbody component and checking the velocity attribute: https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
Keep in mind, since this is a beginner's question, that the fact that an object is moving in a certain direction may not come from a ribidbody. As I said, this depends on your exact set-up.

Detect an OffMeshLink without using an Agent

I'm doing a AI using the NavMesh pathfinding solutions, and I don't to want to use a NavMeshAgent because of some limitations (like teleporting when a carved object comes close, etc.),
Thus I made a sort of fusion, moving a Rigidbody on the path of NavMesh.CalculatePath(), this is working quite good so far, but I have trouble detecting the OffMeshLinks on my path (because it's just an array of Vector3).
I can "trick" a bit using NavMesh.Raycast() like above, but it's not working all the time and sometimes detecting OffMeshLinks that aren't one
NavMeshHit hit;
//Raycast between two corners of a path, because OffMeshLink are most of the time over a hole
if (NavMesh.Raycast(_pathCorners[0], _pathCorners[1], out hit, NavMesh.AllAreas))
{
//If the hit position is really close to the corner, it's surely a offmeshlink generate from the NavMesh
//Or if the second corner is higher than the first one, it's surely a offmeshlink I generate myself
if ((hit.position - _pathCorners[0]).sqrMagnitude < 1
|| _pathCorners[0].y < _pathCorners[1].y)
{
ActivateJump(_pathCorners[0], _pathCorners[1]);
}
}
Thanks in advance :)

How to make an object follow another object's path in Unity3D?

I've tried asking this question on the Unity answers site, but since I've yet to receive an answer I figured I'd ask this question here as well. I am trying to make a 3D version of the game "Snake", but I am running into serious problems trying to make the first snake segment follow the head of the snake. The GameObjects I am using are spheres with rigidbody components, with the player having control only over the "head" of the snake. Then, as the snake grows, more spheres are added that should follow the path that the main sphere makes.
I would like to know if there is either a more elegant solution to what I am trying to achieve, or at least a little help into what I may do to fix the current implementation. I am attaching the code of the segment that is supposed to follow the head. The naming for the objects are in Spanish, but it may not be that hard to figure out what is going on from a non-Spanish speaking person's perspective.
I have also properly commented the code, so that you can understand what I am doing at each sentence. The main idea that partially works right now is to send the snake segments information about where exactly the head made a turn so that when the segments get to a particular turning point, they can make a turn in the direction the head turned. The problem I have is that sometimes the segment travels past by the point where it should make a turn, and I don't understand if it is because of precision problems (I am making float comparisons to determine if a segment has reached a certain position where it turns) or if it's something else.
using UnityEngine;
using System.Collections.Generic;
using Clases;
public class ControladorSegmento : MonoBehaviour {
//This is a generic list of Objects that store both turning position and turning direction that the head has made
public List<PosicionCambioDireccion> listaPosicionesCambioDireccion;
//This would be the head in the case of only one segment that is following
public GameObject lider;
//Speed
public float rapidez;
//Direction
public Vector3 direccion;
//This is an index that is used to iterate over the list of turning points
private int indiceCambioDireccion;
// Use this for initialization
void Start () {
indiceCambioDireccion = 0;
listaPosicionesCambioDireccion = new List<PosicionCambioDireccion>();
//First find the Head of the snake so that we can access its position in order to determine segment spawning position
lider = GameObject.Find("Cabeza");
//Set the position of the new segment 2 units right behind the head
transform.position = lider.GetComponent<Transform>().position - lider.GetComponent<ControladorCabeza>().direccion * 2f;
//Get the current direction of the head so that the segment inmediately moves in its direction
direccion = lider.GetComponent<ControladorCabeza>().direccion;
}
void LateUpdate () {
//Check if there has been a change in direction that the segment has to follow
if ((listaPosicionesCambioDireccion.Count > 0) && (listaPosicionesCambioDireccion.Count > indiceCambioDireccion)) {
//Compare how close we are to the turning position. If we are sufficiently close, change segment direction
if (Mathf.Abs(transform.position.x - listaPosicionesCambioDireccion[indiceCambioDireccion].posicion.x) < 0.0999999 &&
Mathf.Abs(transform.position.z - listaPosicionesCambioDireccion[indiceCambioDireccion].posicion.z) < 0.0999999) {
//Change segment direction at the current position
direccion = listaPosicionesCambioDireccion[indiceCambioDireccion].direccion;
//Increment turning positions list index so that we get the next turning point in the list (if there is any)
indiceCambioDireccion++;
}
}
}
void FixedUpdate() {
//Change the velocity
GetComponent<Rigidbody>().velocity = direccion * rapidez;
}
}
I am still really new working with Unity and it seems I have still much to learn. And again, if you have a more elegant solution to what I am trying to achieve, please let me know. Right know I am aware that this implementation could cause a sort of memory leak in the sense that, as long as the head keeps changing direction, the list of turning points that is stored is going to keep growing and growing which obviously is something one should avoid.
You may record each segment's position in last frame in a list(include head), at the next frame, create a vector from the current position of the head segment to the last position recorded last frame. Then step the distance of the sum of both head & first segment's radius from the current position of the head, and place your first segment there. Repeat these steps for all other segments consist your snake. Hope this can help you.

How do I freeze rotation of a Rigidbody programatically?

I am creating a 2D platformer game, and I already wrote the script that moves the sprite forwards and backwards successfully. However, I am extremely new to Unity and C#, so I have no idea how to freeze rotation of the sprite.
I tried to do it programatically (because the use gravity option did not appear in inspector) like so-
void Update () {
anim.SetBool("Grounded", grounded);
anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
rb2d.freezeRotation.freezeRotation = true;
}
but it obviously doesn't work. How do I correctly freeze rotation of the sprite with c#? Where do I put this code?
Thanks in advance-
George :)
What you could do to constraint the rotation on one axis of your object is:
Create a variable float freezeRotationX = 5.0f; //5.0 is just an example
And write this transfrom.rotation = Quaternion.Euler(freezeRotationX, transfrom.rotation.y, transfrom.rotation.z); This line should be in the Update methode.
This will fix the rotation on the X axis and leave the other ones with their current value.
Of course you can create a variable for each axis.
Hope that helps you.
This is very simple, you really don't have to use C#, you can just set the angular velocity of a rigidbody2D really high, but if that doesn't work, try constantly setting the transforms rotations to 0, (the code would look like this in the update function,
getcomponent<yourgameobject>().transform.rotation.z = 0
getcomponent<yourgameobject>().transform.rotation.x = 0
getcomponent<yourgameobject>().transform.rotation.y = 0
the syntax probably isn't correct, and it might slow down the game, but if your beginning unity it is a simple solution.

Rigidbody not acting as expected

For the past few days I have been working on some custom car physics as a coding exercise.
What my ultimate goal is to have some semi-realistic car physics something in between the quality of the older GTA games and the recent GTA 5.
Now what I got so far is a quite intricate process which eventually gives me a float which is the exact speed I want the car to go until the next frame, so far I have used the following code to do that;
this.transform.Translate (0, 0, speed * Time.deltaTime);
However because I want to use the Unity build in physics for collision detection I realised this wouldn't work because transform.Translate (as far as I know) literally places the object at that position, thus the object could (if going fast enough) suddenly be halfway stuck through a wall or just completely ignore the collision and appear on the other side of the wall. Now instead I decided to do this:
rb.velocity = transform.forward * speed;
To quickly note, I am doing both of these in my FixedUpdate and I am currently only using the second example (or at least trying to use it and am miserably failing at it). Due to it being in the fixed update I should be able to at least test it to a certain extend without Time.deltaTime usage, which is why you aren't seeing that in the second example right now.
To continue my story, for some reason when I set the velocity of the rigidbody instead of using transform.Translate my collision acts really strangely. The car becomes all floaty and nudges forward (stands on its nose) when I hit the wall, I have no clue what could be causing this seeing as I am using my own gravity (the gravity from the original Rigidbody is turned off in favor of my own custom gravity, which does result in a downward rigidbody force) and the drag, and angular drag of the rigidbody have been turned down to 0.
Basically my question is do any of you guys have an idea of what could be causing my rigidbody to be reacting in such a manner, literally the only difference between my normal code and the code that is acting up is that single line change.
I've looked in to using rigidbody.AddForce() instead but that just doesn't seem to do anything at all and on top of that the whole reason why I am writing my car script is to determine the velocity the object should be moving at thus making something like rigidbody.AddForce() which adds it to the velocity of the car more or less redundant and shouldn't be necesarry.
The only reason why I'm even using a rigidbody in the first place is so I can do two things: 1. Easy collisions. It means I can add simple forces such as gravity to it and not have to worry about it going through the floor. 2. easy rotations when affected by physics such as collisions.
If anyone has any ideas on how to do these two things in a custom script I'd much rather do that because as much as I like unity the Unity rigidbody physics are practically worthless in my eyes, however a must have because I can't write my own custom rigidbody and I can't find the source code of the Unity Rigidbody either. What I'd rather have is a stripped down version of the rigidbody which allows for easy collisions and easy rotation but without all the stuff like the build in drag or velocity because these are the exact things I want to be able to control myself and have been able to control myself so far when I use transform.Translate, but I lose this control when I have to use rigidbody.velocity.
according to the documentation you shouldn't use rb.velocity to change the velocity regularly - it could result in unrealistic physics
http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
instead use rb.addforce
http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
hopefully that will solve the issue!
Replace the Translate with the following:
rb.MovePosition(transform.position + transform.forward * (speed * Time.deltaTime));
MovePosition basically acts as a translate (if you do it like this) except it also counts collisions in to it.
If you want super realistic 3D car physics, should look into ready assets like:
Edy's Vehice Physics https://www.assetstore.unity3d.com/en/#!/content/403
Also from Unite2015 talks, see example project: "EasySuspension" to build sample 3D car by script:
http://bit.ly/1y8ucNW (from video: https://youtu.be/WGvuP6vV6j4?t=17m8s )
Or is the game top down 2D game? (like early GTA's)

Categories