When camera position set to object's, camera doesn't work - c#

I have a camera with all of the default settings with the following C# script below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera1 : MonoBehaviour
{
// Update is called once per frame
void Update()
{
transform.position = GameObject.Find("Thing").transform.position;
}
}
The project runs normally (The player still moves), but I get this

Under the culling mask you need to create a layer for the gameObject your camera is set to follow, otherwise it will not see through it. You can also set its transform position just to be above/in front of it.

Related

Why the gameobject blue axis is facing forward but the gun is on the red axis and not the blue?

I tried to create a empty gameobject and put the first gameobject as child but it didn't fix the problem.
Just rotating the object a script is fine but if I'm using LookAt it's not rotating good since the gun is pointing on the red axis and the blue axis is on the body.
Wrong axis
I tried simple LookAt :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAT : MonoBehaviour
{
private GameObject target;
// Use this for initialization
void Start()
{
target = GameObject.FindGameObjectWithTag("Enemy");
}
// Update is called once per frame
void Update()
{
transform.LookAt(target.transform);
}
}
Then I tried this : This was a bit better it seems like it was facing with the red axis forward with the gun but not perfect still it didn't target perfect the target I think not sure and not sure if this is a good solution :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAT : MonoBehaviour
{
public float speed;
private GameObject target;
// Use this for initialization
void Start()
{
target = GameObject.FindGameObjectWithTag("Enemy");
}
// Update is called once per frame
void Update()
{
Vector3 direction = target.transform.position - transform.position;
Quaternion toRotation = Quaternion.FromToRotation(transform.right, direction);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.time);
}
}
The second solution I tried seems the red axis with the gun is facing the target now but the gun and the rings on the base are stuttering and the rings rotating too but they should not rotate I think or they should not be part of the looking on the target.
In this screenshot the whole rings on the base are kind of looking at the target and make the whole turret looks strange. So I'm not sure how ot make this rings and other stuff rotating while only the turret turn object will face the target.
Wrong stuff is rotating and looking facing the target
Looks like the model has axis that don't line up with how you want to use it. Its reccomended that you fix the model, but alternatively you can also add a parent transform on top so you can add some rotation to your model.
Check this out for a more detailed explanation.
To get the two discrete axis of rotation thing I think you are describing at the end of your post you can add two parents as described in the post I linked and apply rotations to each of those parents individually.

How to make an object move in Unity

I just started to use Unity and I found on Youtube a tutorial for my question(making an object move with arrow keys), but I have a problem. My object is constantly moving into the right bottom corner(x is going to minus and z is going to +, y is zero). I did the same thing as the guy in the tutorial.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime, 0f, Input.GetAxis("Vertical") * Time.deltaTime);
}
}
Do you have a Rigidbody component added to your object?
If you do, uncheck "Use Gravity" and check "Is Kinematic" in the Unity Editor, inspector window.
That might help solve the problem.

How do I move the Main Camera in Unity when I click on a sprite?

I am making a simple VR Tour in Unity, and I want to move the camera to the next sphere (or at all at this point) when I click on a sprite that is hovering in the air. Right now, when I run this code, I get the "Sprite Clicked" message in the console, but the camera doesn't move at all. Any help is greatly appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DebugOnClick : MonoBehaviour {
// Use this for initialization
void Start () {
}
void OnMouseDown () {
Debug.Log("Sprite Clicked");
Camera.main.transform.position = new Vector3(5.0f, 5.0f, 5.0f);
}
}
It doesn't let me comment (not enough rep) so I must pop this in as an answer.
If you're using some given VR scripts already, it could be possible that they're syncing the camera position constantly to where they should be relative to the player's headset position.
Meaning that after this move via script, the camera will be moved back instantly.
If that is the case, you need to move the player avatar themselves to the correct position rather than the camera.

C# Unity: How to reference image effects attached to a main camera via script

Title says everything... How to reference image effects attached to a main camera via script
First, add using UnityStandardAssets.ImageEffects; to the top of your script. The rest is easy. You use Camera.main to access the main camera. You can then use GetComponent to get the Image Effect script instance.
using UnityEngine;
using UnityStandardAssets.ImageEffects;
public class MyScript : MonoBehaviour
{
void Start()
{
Bloom bloomEffect = Camera.main.GetComponent<Bloom>();
Blur blurEffect = Camera.main.GetComponent<Blur>();
Tonemapping toneMappingEffect = Camera.main.GetComponent<Tonemapping>();
}
}

script causing odd scaling behaviour on child object in unity

I made a small turret object and a target object that moves about my scene.
In the turret's script I make them aim at the object.
using UnityEngine;
using System.Collections;
public class indivMover : MonoBehaviour
{
public GameObject t1;
public GameObject target;
// Update is called once per frame
void Update ()
{
t1.transform.LookAt(target.transform);
}
}
this worked fine until I parented many of the turrets to a larger object so I could move them around as one. Now they all distort as they point away from their starting position. Why is this happening?

Categories

Resources