How to get hingeJoint2D anchor point in unity 2d? - c#

I am trying to Instantiate a particle system at the same location of a hinge joint.
Here is my current code:
void OnJointBreak2D(Joint2D brokenJoint){
Instantiate(blood_particles, brokenJoint.transform.position, Quaternion.identity);
}
This instantiates the particle at the location of the gameobject that the joint was attached to.

Related

How to make the camera follow the player in unity

Hey i am making a unity game called Cube-Runner. There is a cube and obstacles and you have to go between them. Not going into the game a lot but the problem is how to follow the player. I can't parent it as if i do that while the cube falls the camera will also move with the rotating cube and will make the player(At least myself) dizzy.
Please give me a script
There needs to be a offset Vector3 which i can change from the inspector.
The offset Vector3 may work.
It should be written in C#.
NOTE: I AM NEW TO C# AND UNITY DO NOT JUDGE BY QUESTION
if you dont want to make the player the parent of the camera, then you can do this :
Create a C# script called CameraMovement and attach it to the camera
add this to CameraMovement
using UnityEngine;
class CameraMovement : MonoBehavior
{
public Transform player;
public Vector3 offset;
void Update()
{
//get the players position and add it with offset, then store it to transform.position aka the cameras position
transform.position = player.position + offset;
}
}
click on the camera and look at the inspector, you should see that there is a script called CameraMovement and 2 fields : player and offset. assign player with your player (drag and drop) and offset with the relative position between your camera and the player (where the camera is with the player being the center).
and you're done, play the game and see the results
You could try using the cinemachine tool, it will make you camera follow smoothly to the player. You could check any tutorial on youtube but I recommend you to check the one "Brackeys" did. No code needed for cinemachine

How to reference a GameObject's position in a prefab script in Unity

I'm just starting out with Unity and for my first game I'm trying to make these enemy cubes chase the player. The enemies are spawned at a random position and I'm trying to make them move towards the position of the player cube. But when I try to reference the player's Transform, it won't let me drag it on top, any fixes?
using UnityEngine;
public class enemyFollow : MonoBehaviour {
public Transform player;
public Rigidbody rb;
public float movementForce;
// Update is called once per frame
void FixedUpdate()
{
if (player.position.x > transform.position.x){
rb.AddForce(movementForce * Time.deltaTime,0,0);
}
if (player.position.x < transform.position.x){
rb.AddForce(-movementForce * Time.deltaTime,0,0);
}
if (player.position.z > transform.position.z){
rb.AddForce(0,0,movementForce * Time.deltaTime);
}
if (player.position.z < transform.position.z){
rb.AddForce(0,0,-movementForce * Time.deltaTime);
}
}
}
Unity won't let me drag the player from the gameobjects window onto the script on the prefab. The error says "the variable player of enemyFollow has not been assigned"
It won't let me assign Player.
You can add this in a void Start() method:
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
If I'm understanding correctly, you're trying to assign the player to the enemy in the prefab configuration. That won't work because the prefab isn't in the scene. Since the prefab could be instantiate in any scene, you can't reference scene objects. You need to do it during playtime, unless all the enemies are already in the scene when you're setting it up (in which case you'd need to do it to an instantiated enemy and then copy paste it).
You can use FindGameObjectWithTag() as in Jeff's answer, but make sure to assign a "Player" tag to the player GameObject or prefab.
Another method which I'd recommend would be to have whatever is instantiating the enemies assign their target, that way you don't need to search for a tag with a string you could mistype, and you can just change the reference in one place when you need to change something.

How to create particles in Unity at a point of impact

So I'm working on a Pong project as a learning tool to help me get used to Unity as a development studio. What I want to do is create a Particle System every time the ball hits either paddle. So I put a particle system as a child under each paddle, and made it so it called that particle system every time the ball made contact with the paddle in C#. It doesn't work, and I want to make it so it calls the particle system at the point of impact between the ball and the paddle. Here's the code I currently have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExplosionScript : MonoBehaviour {
public GameObject explosionParticle;
void OnCollisionEnter2D (Collision2D coll) {
if (coll.collider.CompareTag ("Player")) {
Explode ();
}
}
void Explode () {
Instantiate (explosionParticle, Vector3.zero, Quaternion.identity);
}
}
This code is attached to the ball GameObject.
Try
explosionParticle.Play(); after instantiating.
typically I create a separate ParticleSystem gameobject and not a child so I can prefab or modify it. Then I declare
public ParticleSystem explosion; then attach the ParticleSystem gameobject to the ball explosion property.
In code I would do just this
Instantiate(explosion, transform.position, Quaternion.identity);
explosion.Play();

camera follows object when it rotates

I am trying to make a game where the camera follows the user, as they move around. I made the camera a child of the player and in the editor, the camera rotates around the player fine. When I play the game (in Unity) and rotate the player, the camera literally rotates instead of rotating around the player to keep the same regular following distance.
I use transform.Rotate() to rotate the player by the way.
In Summary:
Looking for camera that follows player in 3D game, that does not look different when the player turns.
issue is that the camera appears in the editor to rotate around the player perfectly, but not when transform.Rotate() is called on it during runtime in Unity.
All help is appreciated, tyvm for help.
I made the camera a child of the player and in the editor
Everything went down by doing this. You don't make the camera a child if you want it to follow the player.
What you do is to get the distance between the camera and the player in the Start() function. This is also called offset. In the LateUpdate() function, continuously move the camera to the Player's position, then add that offset to the camera's position. It is as simple as that.
public class CameraMover: MonoBehaviour
{
public Transform playerTransform;
public Transform mainCameraTransform = null;
private Vector3 cameraOffset = Vector3.zero;
void Start()
{
mainCameraTransform = Camera.main.transform;
//Get camera-player Transform Offset that will be used to move the camera
cameraOffset = mainCameraTransform.position - playerTransform.position;
}
void LateUpdate()
{
//Move the camera to the position of the playerTransform with the offset that was saved in the begining
mainCameraTransform.position = playerTransform.position + cameraOffset;
}
}

My camera rotates with my ball when i have ball as a parent gameobject of camera. what should i do?

I'm using my camera as a child gameobject of my ball, so when I move my ball camera comes with him. But the problem is I'm using rigidbody.addForce(), so when ball rotates the camera rotates with it, too.
So what should I do not to rotate my camera but only move it with my ball?
void FixedUpdate()
{
rigidbody.addForce(Input.getAxis("Horizontal"), 0, Input.getAxis("vertical"));
}
There are several ways to solve this, I'll list a few.
The first, if you don't care if your ball rotates or not, you can just disable the rotation on the ball. If you open the Constraints section in the rigidbody component, you can freeze the rotation so that the ball won't rotate.
Alternatively, you can write a script that keeps the camera always oriented a certain way. Depending on if you want the camera to rotate around the ball on any plane depends on the way you would implement this.
The third option, which is cleanest, is to not have the camera be a child of the ball. A minimal component to do this would look like this:
public class TargetFollow : MonoBehaviour
{
public Transform Target;
public float DistanceFromTarget;
void Update()
{
transform.position = Target.position + new Vector3(0, 0, DistanceFromTarget);
transform.LookAt(Target);
}
}
Just drag your ball into the 'Target' slot in your component. Keep in mind, this is super bare bones. You may want to add more variables to better control the direction the camera should be from the ball, or perhaps something that grabs a snapshot of the direction and distance the camera is from the ball in the Start method.

Categories

Resources