What is the difference between Transform and transform in Unity? - c#

I know Transform is used to get position, rotation and scale of an gameobject but what does transform do?
{
public Transform player;
private void Update()
{
transform.position = player.position;
}
}
here Transform gets the position, rotation and scale and then stores it in the variable player. I also understand that from the line "transform.position" that it is used to convert the position to player position but is that the only use and am I right about this?

Transform is a type, and is described in the Unity Documentation as
Position, rotation and scale of an object.
Every object in a Scene has a Transform. It's used to store and
manipulate the position, rotation and scale of the object. Every
Transform can have a parent, which allows you to apply position,
rotation and scale hierarchically. This is the hierarchy seen in the
Hierarchy pane.
Every C# script that inherits MonoBehaviour in Unity will have a transform. Inherting MonoBehaviour will allow for a script to be attached to a GameObject in Unity. The MonoBehaviour class has many fields, one of them is the transform field, which has the type of Transform. The Unity Documentation describes the field as
The Transform attached to this GameObject.
Every GameObject has a Transform component, meaning that every Unity script using MonoBehaviour will have a non-null field called transform.
If your script is attached to the player GameObject, then there would be no need for you to store a reference to the player, and could instead use the transform variable.

Transform is a class in UnityEngine while transform is a property of type Transform from the GameObject attached to the script.

Related

Player not moving down with gravity after transforming position - UNITY 2D

I'm making a game in which I have to transform the players position after he completes the objective, but when I transform him, he's stuck in the air on the new transform position, even though I have a Rigibody2D set on it with a gravity scale of 2. When I move him manually by dragging and releasing, he falls to the ground normally. I'm fairly new so any help would be appreciated. I can't seem to identify the problem.
Here's the code,
public GameObject player;
public Transform nextPart;
public Camera cam;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (PlayerController.canTransform)
{
player.transform.position = nextPart.position;
cam.transform.position = new Vector3(nextPart.position.x, nextPart.position.y, -10);
}
}
Here's a picture too,
Your code is within the Update function. This means that so long as canTransform is true your player will be placed at that transform every frame.
You need to set that boolean back to false after moving your player or have some other check to ensure you don't move to that position more than once.
It might be better to instead have the player set to that position in a separate function and simply call it one time when you reaches that objective.

Moving a GameObject using tags not working Unity

I know this is quite the noob question, but whatever, there's only one way to learn.
I've created an empty GameObject in Unity, attached a script that is supposed to move a cube(my player) and gave my cube the tag "Player". After creating the cube, I was hoping to be able to move the cube without having to put the script on the cube itself. When the script is on the cube, it moves without a problem (I know this is how it probably should be done, but for trying to learn new things I wanted to do it this way).
Player Controller script
Cube properties
After failing to find the answer through Google, any insight at all is greatly appreciated!
Thank you
Update!
Here's the code as text now, since it was asked for.
public class GameCoreController : MonoBehaviour {
private GameObject PlayerMove;
public Rigidbody rb;
void Start ()
{
PlayerMove = GameObject.FindGameObjectWithTag("Player");
rb = GetComponent<Rigidbody>();
}
void Update()
{
// character movement
if (Input.GetKey(KeyCode.W))
{
PlayerMove.transform.Translate(0, 0, 0.25f);
}
if (Input.GetKey(KeyCode.S))
{
PlayerMove.transform.Translate(0, 0, -0.25f);
}
if (Input.GetKey(KeyCode.A))
{
PlayerMove.transform.Translate(-0.25f, 0, 0);
}
if (Input.GetKey(KeyCode.D))
{
PlayerMove.transform.Translate(0.25f, 0, -0);
}
}
I've updated the code from before to include PlayerMove.transform.Translate but I still have the same issue with the cube note moving. I've also included screenshots of my scene with the cube and the GameCoreController; the empty GameObject holding the script that is supposed to control the cube.
Thanks again for the help guys.
Update 2!
After deleting the cube and re-insert it into the scene it now moves. Thanks for the help everyone.
The reason that the cube won't move because in your code you didn't move its transform but you move the transform of the gameobject that you attached this script to.
transform.Translate move the transform of the gameobject that this script attach to. So if you want to move the cube, all you need to do is change from transform.Translate to PlayerMove.transform.Translate which will move the transform of PlayerMove gameobject which is your cube with "Player" tag on it
^ All of the above. Plus, in the screenshot, your rigidbody is not set to "is kinomatic". This means that physics will still be applied to it (like gravity). Rule of thumb: If you have a moving object where collision is important, it will need a rigidbody and a collider. If the object is not moved with physics commands (eg rigidbody.AddForce()) but rather manipulating the transform as you are, set the rigidbody "isKinomatic" property to true.

Unity C#, Camera facing sprite and rigidbody not working together

I have a small piece of code to make a sprite (in a 3D world) always face the camera (It has to be in 3D space).
public class CS_CameraFacingBillboard : MonoBehaviour {
private Camera m_Camera;
private void Start()
{
m_Camera = Camera.main;
}
void Update()
{
transform.LookAt(transform.position + m_Camera.transform.rotation *
Vector3.forward, m_Camera.transform.rotation * Vector3.up);
}
}
This code ensures the sprite is always facing the camera, causing it to lean backwards as the camera in above the sprite facing down in a 45 degree agle. When I put a rigidbody on the sprite, the sprite moves on its own towards the direction its leaning. The rigidbody works fine without this code attached.
How can I have a sprite that always faces the camera, and has a rigidbody attached?
It seems you've left the rigidbody as Dynamic, you should set it to Kinematic.
EDIT: After you comments, I checked myself inside Unity, and probably I've recreated the behaviour you described. It happens to me too IF I use a Box Collider on the sprite without locking its rigidbody rotation.
So you have three possible solutions:
Use a Box Collider and under Constraints of the rigidbody freeze the rotation:
Use a Sphere Collider (or another one that doesn't behave like the box one, you can check them out in play mode).
Split the components over two game object, a parent and a child. The parent will have all the components except the sprite renderer and the camera script, which will be on the child. This option is the most flexible and less restraining. You can have the box collider without freezing rotations, etc.
Another thing, you can avoid the use of the LookAt method, by simply using:
transform.rotation = m_Camera.transform.rotation;
they have the same outcome.

Unity rigidbody FixedUpdate basic movement does not work

I'm trying to implement this tutorial on the unity site. I've went over the unity blog and havn't found the solution to my problem there.
I have a simple Rigidbody sphere object over a plane.
The sphere is default sized, and set on: (0,0.5,0).
The plane is also default sized, and set on the origin (0,0,0). Those are the only components I use.
What I'm trying to do is to write a simple C# script behavior for the sphere that will move it across the plane, like so:
public class Controller : MonoBehaviour {
private Rigidbody rb; // Holds the body this script is affecting.
// Called at the start, to set variables.
void Start()
{
rb = GetComponent<Rigidbody>(); // Get the body, if there is one.
}
//For physical changes.
void FixedUpdate()
{
float Horizontal = Input.GetAxis ("Horizontal"); // Get horizontal movement from input.
float Vertical = Input.GetAxis ("Vertical"); // Get vertical movement from input.
Vector3 Movement = new Vector3 (Horizontal, 0.0f, Vertical); // Declaring the movement I'd like to add to the RB. Y axis is irrelevant. X,Z - controlled by user input.
rb.AddForce (Movement); // Making the movement.
}
}
I attached this behavior to the sphere, expecting It'd move when I hit some input key.
Despite this, when I play the project, everything compiles fairly well but the sphere just doesn't move regardless of what I type.
What am I missing?
EDIT: If it's relevant, I also have problems opening the Unity c# code editor (forgot it's name). Whhenever I click open, it just instantly closes. I do everything on Visual Studio.
EDIT 2: My bad, I just figured out I have console Errors. I get the following one:
MissingComponentException: There is no 'Rigidbody' attached to the "Player" game object, but a script is trying to access it.
You probably need to add a Rigidbody to the game object "Player". Or your script needs to check if the component is attached before using it.
UnityEngine.Rigidbody.AddForce (Vector3 force) (at C:/buildslave/unity/build/artifacts/generated/common/modules/NewDynamics.gen.cs:706)
Controller.FixedUpdate () (at Assets/_Scripts/Controller.cs:20)
"Player" is the name I gave the sphere.
I forgot to attach the Rigidbody to the sphere.

How to Draw a Ray Along a Sprite in Unity

I am trying to cast a ray along a sprite in Unity. I have Created an empty GameObject and made it as Parent to different Textures of Head, Hand, Chest etc of a Character to easily Animate it, Now for Melee Combat I would like to Cast a Ray along the Hand Texture while it Animates the Attack but I am Unable to get the Centre of the Hand Texture in the Scene.
I am trying to Access the Sprite by the following Code
Sprite Hand = gameObject.GetComponentsInChildren <Transform> () [4].GetComponentsInChildren <Transform> () [0].gameObject.GetComponent <SpriteRenderer> ().sprite;
This Code is Working for accessing the Hand Transform I verified by Drawing a Ray from the transforms centre
Rather than trying to find it at run time, just link it together in the prefab/gameobject. It's pretty simple to just expose it in the inspector then drag the sprite to the exposed variable. You could do a sprite or a transform, in the example below I used a transform, but if you need to get more data from it then go on ahead and use a sprite.
public Transform Head;
public Transform Hand;
public Transform Chest;
Then you can get the position with Head.position
If you are looking to check for collisions consider placing a collider at each location, then you can toggle them on/off when you want (so the hand collider is disabled unless if the character is punching, then you turn it on for the duration (or part of it) of the punch.) Then you can have a component on just that collider for doing damage or finding a wall or whatever you are trying to accomplish.

Categories