Character rotate at collider with camera script - c#

I am using a simple script for the Camera to follow a Player target.
// Use this for initialization
void Start()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = player.transform.position + offset;
}
but when I start the game the player starts rotating and ignores colliders when hitting a collider for the first time.
This started happening after I have put this script on the player. I even tried to freeze the rotation but if I freeze the rotation I can not rotate my player with animations. How can I fix this?

Attach this script to the 'Camera' not the 'Player' and you should be just fine.

Related

I am working on a game project on unity and I am stuck at a camera problem

It has two camera angles. First person for engaging gameplay and top-down for 2D game experience. But in top down camera view I can't figure out the rotation of the camera with respect to player.
I wrote the following script for the main(top-down) camera view.
public class FollowPlayer : MonoBehaviour
{
public GameObject player;
private Vector3 offset = new Vector3(0f, 24.953f, -0f);
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
transform.position = player.transform.position + offset;
}
}`
I tried
transform.rotation = player.transform.rotation;
but it didn't work since camera is on top of player. If I could somehow do
transform.rotation = player.transform.rotation + X axis 90 degree;
That would be perfect but I don't know how to do that.
If I understand you correctly, you want to follow the player from above with your second camera. Leaving some flexibility, the best option I think would be Unity's built in Transform.LookAt(target) method (LookAt), which automatically rotates an object (in your case the camera) so that it faces the target.
Therefore, you could do something like this in your Update, assuming your script is attached to the Camera. Otherwise substitute transform with Camera.main.transform:
transform.LookAt(player);
Note: If you plan to have your camera fixed above your player at all times, it is sufficient if you perform the LookAt once, e.g. in Start and then attach the camera as a child to the player. If your camera does not move with your player in the world but you want it to focus the player anyway, do do it in Update. Hope I addressed your problem:)
My problem has been resolved. I was trying to rotate the wrong axis. My bad.
mouseInput = Input.GetAxis("Mouse X");
transform.Rotate(Vector3.forward * speed * -mouseInput);
I needed to get mouse input from user and change the Z-axis according to it. Game is working fine now.

How to instantiate an object to the where the camera is looking at in Unity

I have been making a first person game called "shoot the ball into the bin". The camera is in a cylinder as a player also the cylinder has collision.
I have been struggling with the main driver of the game. Like in most first person shooter games, you shoot out from the center point of the camera or the cursor.
I have trouble replicating that. The code I have so far for the shooting is:
Instantiate(prefab, new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, Camera.main.transform.position.z), Camera.main.transform.rotation);
(Credits to #derHugo for helping me instantiate a prefab)
But that just spawns it on top of the player, not in front of it. How do I make it shoot away from the player, but also in front of the player, not on top.
In order to spawn in front of that position simply use
// Already reference this via the Inspector if possible
[SerializeField] private Camera _camera;
// Adjust how far in front of the camera the object should spawn
[SerializeField] private float distance;
private void Awake ()
{
// otherwise get it ONCE
if(!_camera) _camera = Camera.main;
}
And then simply add an offset in the direction of Transform.forward of the camera like
Instantiate(prefab, _camera.transform.position + _camera.transform.forward * distance, _camera.transform.rotation);
so it seems when you instantiate the bullets, you are spawning them directly on the position of the camera. You should adjust those values so that it spawns in the center of the screen. To do so, you should divide the y position by 2 or 1.5 depending on how high up the gun is from the ground. Then change the z so that the bullets pushed further in front of the player.
Instantiate(prefab, new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y / 2, Camera.main.transform.position.z + somearbitraryvalue), Camera.main.transform.rotation);
Next, to get give the bullets a velocity, you should create a separate script for the bullet prefab that is in charge of moving the bullets.
public Rigidbody2D bullet;
bullet = GetComponent<Rigidbody2D>()
void Update(){
bullet.velocity = new Vector 3(0,0,1)
}
Hope this answer helps!

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.

unity falling sprite

So I am trying to figure out how to get my square (sprite) when I click on to fall and collide with my other square (sprite).
I know that I have to write a c# script to get it going with the Method:
private void OnMouseDown(){
}
but I don't know how to change the coordinates in this method please help !
Thanks,
my whole projekt
To change the coordinates of the transform the script is attached to (your player), you must access the transform. If you want to translate it, you should multiply it by Time.deltaTime to ensure that it will remain a constant speed at any framerate.
//On mouse down call
void OnMouseDown(){
//Define your speed
float speed = 1.0f;
//Translate the y position downwards
Vector3 newPos = this.transform.position;
newPos.y -= Time.deltaTime * speed;
this.transform.position.y = newPos.y;
}
However, I'm not sure if you would even want this. It would be a lot better if you set up 2D physics. To do this each object in the scene needs a collider and the player object must have a rigid body. To access these components go to the object and press "Add Component" towards the bottom. Here is an image of the dropdown that will appear:
Then click the highlighted "Physics 2D". Here you want to select "Box Collider 2D" for all the physics game objects and then only "Rigidbody 2D" for the player. When you start the game the 2D player should fall (if done correctly).

Camera Controller in Unity

There are two similar scripts that makes the same thing(The Camera is following the player).The first, is what I wrote and the second one, is from the unity tutorial.Which one is more reliable and why?
1st
public class CameraController : MonoBehaviour {
public GameObject player;
void LateUpdate () {
transform.position = new Vector3 (player.transform.position.x, player.transform.position.y,-10f);
}
}
2nd
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
transform.position = player.transform.position + offset;
}
There is no such thing as reliability between the two scripts. Although, I would encourage you to use the second script.
The first script is assigning the camera's position with the position of the player. The only thing it doesn't assign is the z-axis which you hard-coded to -10. When game starts, the camera will be moved to where the player is. The z- axis of the camera will be set to -10. This happens every late update.
The second script is assigning the camera's position with the position of the player and offset value taken in the start function. When game starts, the offset distance between the camera and the player is retrieved in the Start function. In the LateUpdate function, the player's position is retrieved and the offset is added to it and is then applied to the camera's position.
The result of the first script is that the camera will always be position where the player is but the z axis will always be set to -10. I am sure you don't want the z axis to not be moving.
The result of the second script is that the camera will always have the-same distance between it and the player. Even if you move the camera or player from the Editor, the second script will automatically adjust the position and make sure that the distance between the camera and the player before the game started is always the-same. I am sure this is what you are looking for but I wouldn't call this reliable. The first code is simply not appropriate for what you are doing , the second code is.
I would bet that the second one is more reliable because it doesn't have any hardcoded variables. But it's also easy to use to programmers and none programmers since it calculates the offSet you want to give in any direction based on the position you set on the scene.

Categories