Unity C# problem with camera, camera goes white - c#

I made a script for my camera for following the player.When I play the game, game view goes white, even if on scene view all is fine
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 playerpos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
playerpos.x = player.position.x;
transform.position = playerpos;
}
}

The problem might be that the player is blocking the camera (because the camera is inside the player). Try adding some offset by adding a Vector3 as a variable and adding it on to the transform.position.
The offset could be used so that the camera is in front of the player, or in a third person angle.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 playerpos;
public Vector3 offset;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
playerpos.x = player.position.x;
transform.position = playerpos + offset;
}
}
Hope this helps.

Related

I'm trying to add multiple camera switches to a vehicle in Unity and I am not sure how to code it

My project is from the "Create with Unity" tutorial unit 1 with a simple vehicle moving down a road. I originally had 1 camera which worked, but when I changed the code to work with multiple cameras I couldn't get the compiler to work.
The 3 cameras are supposed to display different angles of the tank while controlling the vehicle. I assume something is wrong with how I wrote the FollowPlayer.cs "void LateUpdate()" method, but I don't know how to make it work. It all went wrong when I changedthe FollowPlayer class from 1 cam to 3 cams.
The picture shows the unity visuals and all relevant class coding is below.
I'm new to unity and c#, so any help would be appreciated!
MY CAM SWITCH CLASS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamSwitch : MonoBehaviour
{
public GameObject cam1;
public GameObject cam2;
public GameObject cam3;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetButtonDown("Switch1"))
{
cam1.SetActive(true);
cam2.SetActive(false);
cam3.SetActive(false);
}
if (Input.GetButtonDown("Switch2"))
{
cam1.SetActive(false);
cam2.SetActive(true);
cam3.SetActive(false);
}
if (Input.GetButtonDown("Switch3"))
{
cam1.SetActive(false);
cam2.SetActive(false);
cam3.SetActive(true);
}
}
}
THE CLASS I THINK I MESSED UP
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public GameObject player;
public Vector3 offset1= new Vector3(0, 5, -7);
public Vector3 offset2 = new Vector3(0, 3.83, -7);
public Vector3 offset3 = new Vector3(0, 7.68, -11.8);
public GameObject cam1;
public GameObject cam2;
public GameObject cam3;
cam1.SetActive(true)
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
if (cam1.SetActive(true))
{
transform.position = player.transform.position + offset1;
}
if (cam2.SetActive(true))
{
transform.position = player.transform.position + offset2;
}
if (cam3.SetActive(true))
{
transform.position = player.transform.position + offset3;
}
}
}
First: remove the line cam1.SetActive(true) under public GameObject cam3;. Its missing a semicolon and it must be placed in a function scope anyway, not in class declaration scope.
Second: this statement (and the other two) if (cam1.SetActive(true)) cant work because SetActive does not return a bool value. You probably wanted if (cam1.activeSelf).
You may spend some time improving your knowledge C# coding basics in Unity

Make object follow rotation with very little difference

I am making my object follow the rotation of another object. I want my object to rotate with very little difference, that is, from its current rotation it should not rotate completely with the other object.
There should be a difference u pto its rotation such that it should rotate 10% of what the main object rotates. How do I do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowRotationWithLimit : MonoBehaviour {
public GameObject objectToFollow;
// Start is called before the first frame update
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.eulerAngles = new Vector3 (this.transform.eulerAngles.x, objectToFollow.transform.eulerAngles.y, this.transform.eulerAngles.z);
}
}
I am not able to get that logic of creating that difference.
I would rather call it FollowRotationWithFactor then and simply use a multiplier
public class FollowRotationWithFactor : MonoBehaviour
{
public GameObject objectToFollow;
public float factor = 0.1f;
// Update is called once per frame
void Update ()
{
var eulerAngles = transform.eulerAngels;
eulerAngles.y = objectToFollow.transform.eulerAngles.y * factor;
transform.eulerAngles = eulerAngles;
}
}

Unity clone won't appear

I'm new to coding and i'm trying to spawn a clone of a ball in unity, but it won't appear.
It does spawn at the spawn point but it doesn't appear.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public GameObject ball;
public Transform spawnPoint;
// Start is called before the first frame update
void Start()
{
SpawnBall();
}
// Update is called once per frame
void Update()
{
}
void SpawnBall()
{
Instantiate(ball, spawnPoint.position, Quaternion.identity);
}
}
Edit: it's a 3d game and the mesh renderer is enabled.
Okay nevermind: i just redid the ball and it worked.
¯_(ツ)_/¯

Collision detection with character controller in Unity 3d

I have my player which uses character controller for moving. I placed a sprite in the scene and I'd like for when my player collides with the sprite to disable the sprite, like if the player grabs the sprite (which is Doom's 64 chainsaw).
The sprite's collisions of course work well with everything, but not with the player. How can I get proper collision between them?
You could do it like this:
1-Attach "Pickable" script to the sprite.
2-Attach "Player" script to the character controller.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickable : MonoBehaviour
{
public float radius = 1f;
private void Start()
{
SphereCollider collider = gameObject.AddComponent<SphereCollider>();
collider.center = Vector3.zero;
collider.radius = radius;
collider.isTrigger = true;
}
}
Here is the other script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
Pickable pickable = other.GetComponent<Pickable>();
if(pickable != null)
{
Destroy(other.gameObject);
}
}
}

How can i make that the user can select if the camera will be behind the player or not?

This is the original code make the camera follow the player:
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public GameObject objectToFollow; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// 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 - objectToFollow.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 = objectToFollow.transform.position + offset;
transform.LookAt(objectToFollow.transform);
}
}
And this is what i tried to do but then the player it self(ThirdPersonController) is not rotating according to where he move.
With the original script above he does.
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public GameObject objectToFollow; //Public variable to store a reference to the player game object
public bool behindPlayer = false;
private Vector3 cameraStartPos;
// Use this for initialization
void Start()
{
cameraStartPos = transform.position;
// Put the camera behind the player
if (behindPlayer == true)
{
transform.position = (objectToFollow.transform.position - (objectToFollow.transform.forward * 5) + (objectToFollow.transform.up * 2));
}
}
private void Update()
{
if (behindPlayer == true)
{
transform.position = (objectToFollow.transform.position - (objectToFollow.transform.forward * 5) + (objectToFollow.transform.up * 2));
}
else
{
transform.position = cameraStartPos;
}
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
transform.position = objectToFollow.transform.position;
transform.LookAt(objectToFollow.transform);
}
}
I want that the camera will be automatic behind the player already without the need to change the camera position in the scene if the user want by using the bool variable.
But the script now is not like in the above:
With this line in the LateUpdate:
transform.position = objectToFollow.transform.position;
Using the bool false/true it's not changing the camera position at all.
Without this line the player will move and the camera will follow but the player will not rotate and not move as above.
What i want is just like the first script but with the behindPlayer variable.
A working script:
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
[SerializeField]
private Transform target;
[SerializeField]
private Vector3 offsetPosition;
[SerializeField]
private Space offsetPositionSpace = Space.Self;
[SerializeField]
private bool lookAt = true;
private void Update()
{
Refresh();
}
public void Refresh()
{
if (target == null)
{
Debug.LogWarning("Missing target ref !", this);
return;
}
// compute position
if (offsetPositionSpace == Space.Self)
{
transform.position = target.TransformPoint(offsetPosition);
}
else
{
transform.position = target.position + offsetPosition;
}
// compute rotation
if (lookAt)
{
transform.LookAt(target);
}
else
{
transform.rotation = target.rotation;
}
}
}
In the inspector set the offset for example to 0,5,-12 and as target set for example ThirdPersonController. And attach the script to the Camera(Camera must be tagged as MainCamera if the target is ThirdPersonController since the ThirdPersonController ThirdPersonUserControl script is looking for the MainCamera).
And the camera doesn't have to be a child of the target the camera can be in any place in the Hierarchy.
Anyway it's working.

Categories