how to fix my enemy turning invisible in play mode UNITY - unity3d

So, here's my error! im designing a FPS game in unity and im scripting an AI enemy (for now it just moves around randomly) im also using NeoFPS that should be all the background info out of the way as for whats happening:
i can see the enemy in scene view and i can see it in the game view before i press play, when i do press play the enemy disappears in game view but i can still see it trying to move around the little box i put it in and stuff of the sorts via navmesh etc. the collider for the enemy is still there when i go into the box however i cant see it and i shoot through it and do no damage to it whatsoever i will include screenshots and my AI code below (the white capsule is the enemy w AI script inside a little box, the camera is the FPS player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AICharacterController : MonoBehaviour
{
private NavMeshAgent m_Agent;
private void Awake()
{
m_Agent = GetComponent<NavMeshAgent>();
}
Vector3 movePosition;
void Update()
{
if (movePosition == Vector3.zero
|| Vector3.Distance(transform.position, m_Agent.destination) < 1)
{
SelectNewDestination();
}
}
void SelectNewDestination()
{
Vector2 pos = Random.insideUnitCircle * 50;
movePosition.x = pos.x;
movePosition.y = pos.y;
NavMeshHit hit = default;
if (NavMesh.SamplePosition(movePosition, out hit, 4, NavMesh.AllAreas))
{
m_Agent.SetDestination(hit.position);
}
}
public void Die()
{
Destroy(gameObject, 0.25f);
}
}
(if you dont see an image below then stackoverflow replaced it with a link and its not working)

If things are invisible in a camera, but not in the scene view then that means that the "Culling Mask" property on the camera is set to ignore the layers the object is on. NeoFPS uses a spawning system, so its quite possible that the camera in the character that's spawned has a different culling mask setup to the camera in the scene that you're looking through outside of play mode. To fix that you would have to find the camera in the character prefab's hierarchy (usually called PlayerCameraSpring) and add your AI's layer to its culling mask.

Related

Unity Character flies above the ground whenever I start my game

Every time I start my game the character starts to rise and fly above the ground. I don't understand how to fix it. I've tried to move the ground up but nothing's changed. 2D game if it matters.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Controller: MonoBehaviour
{
public float speed = 0.1f;
private void Update()
{
transform.position += new Vector3(speed, 0, 0) * Input.GetAxis("Horizontal");
transform.position += new Vector3(0, speed, 0) * Input.GetAxis("Vertical");
if(Input.GetAxis("Horizontal") < 0)//если влево
{
GetComponent<SpriteRenderer>().flipX = true;
}
if(Input.GetAxis("Horizontal") > 0)//если вправо
{
GetComponent<SpriteRenderer>().flipX = false;
}
}
}
You should be able to just add the Rigidbody Component to your Character GameObject, then enable gravity in the component and your character should just fall on the ground.
EDIT: If you find your character in the Unity Hierarchy (left hand side of the screen by default) and then to the inspector window (right hand side of the screen by default. At the bottom of the inspector, there should be an ‘Add Component’ button. Click that button and then search ‘Rigidbody’ in the search field that appears. Add the Rigidbody Component. By default, the enable gravity option should be ticked but if it isn’t, just make sure you tick that box. When you run your game with gravity enabled, gravity should act on the GameObject with the ‘Rigidbody’ Component, pulling it to the ground
Also, make sure that you follow the same process as above to add the ‘Mesh Collider’ Component to your platform/ground - this will make your ground a ‘Physical’ object which will prevent your character falling straight through it
Hope this helps - let me know if it doesn’t work

Player disappears on Game mode in Uniity 2D while switching scenes

I'm making a function that it switches 2 scenes.
When the player enters the trigger, automatically unity switches the scene and in the "Scene view", the player is there, as I wanted. But in the game view, the player dissappears, I don't know why.
I tried to remove a script that it's function is teleport the player to a specific point of the new scene, and it worked.
But obviously I don't want that, because unity automatically teleports the player to a random point, how can I fix that?
Here are the scripts:
TeleportScript:
public class EnterScene : MonoBehaviour
{
public string transitionName; //Also 1-1
void Start()
{
if (transitionName == PlayerController.sharedInstance.areaTransitionName)
{
PlayerController.sharedInstance.transform.position = transform.position; //Moves the player to GameObject position
}
}
// Update is called once per frame
void Update()
{
}
}
https://pastebin.com/jdSPhR3s <-----SceneLoader
https://pastebin.com/KcwHVBfQ <----PlayerController
And a screenshot of my problem:
Problem

Camera Zoom when Player Died and Focus the player position c#

i am developing endless game . when player dies then camera zoom and focus on player position. i try and almost done. To zoom the camera i use orthographicSize and focus to the player i use transform.LookAt() that focus the player position when player dies.but the problem is when camera Zoom the scene then entire scene gets rotate. i have created CameraScript and attached to the maincamera .
[SerializeField]
private Camera gameCam;
[SerializeField]
private Transform[] target;
IEnumerator ZoomIn(){
while (gameCam.orthographicSize > 3) {
yield return new WaitForSeconds (0.01f);
transform.LookAt (target [index]);
gameCam.orthographicSize -= 0.1f;
}
}
public void ZoomCam(){
StartCoroutine (ZoomIn ());
}
Help me to that Script if any mistake..Thanks..
I think it's happening because you're not setting the worldUp parameter when you're calling transform.LookAt(...).
I suggest using the camera's own current up vector as that parameter, i.e.
transform.LookAt (target [index], transform.up);
I hope that helps!
EDIT:
Of course, this would only work if your camera is originally oriented as you intend it to be. Else, you'll have to use the vector you choose as the second parameter there.

Google Cardboard - How to detect focus on an object?

I try to create a VR scene in unity using google cardboard sdk. I add a cube and CardboardMain.prefab to scene. There is an example scene that detect focus on cube. Its hierarchy view is :
I don't know how to add GUIReticle object or prefab like as the image.
How can I detect focus on an object?
Actually you could make the script by your own and it is quite simple.
You could detect whether the user is looking at your object or not by using RayCast from the Main Camera. If the RayCast hit your object, then it is being focused on.
For example:
using UnityEngine;
using System;
[RequireComponent(typeof(Collider))]
public class LookableObject : MonoBehaviour {
[SerializeField]
Transform cam; // This is the main camera.
// You can alternately use Camera.main if you've tagged it as MainCamera
bool focus; // True if focused
Collider gazeArea; // Your object's collider
public void Start () {
gazeArea = GetComponent<Collider> ();
}
public void Update () {
RaycastHit hit;
if (Physics.Raycast (cam.position, cam.forward, out hit, 1000f)) {
focus = (hit.collider == gazeArea);
} else {
focus = false;
}
}
}
Edit: This is just an example. You probably would want to make a script to do the Raycast only just once instead of doing the Raycast on each of your object over and over again to make your project runs faster.

GameObject position changes after I press the play button on Unity

The game has the following scenario:
The other objects have been disabled only to demonstrate the problem. I think to deactivate them do not influence the game, right?
This is what happens when I click play in Unity:
Note that the Ground and Ground_2 objects were moved to the position (0,0) and yes, the sprite pivot is BottomLeft, so it is not centered.
Game settings:
Background
Camera
Camera Script
using UnityEngine;
using System.Collections;
public class CameraSettings : MonoBehaviour {
private float targetRatio = 9f/16f;
// Use this for initialization
void Start () {
Camera cam = GetComponent<Camera>();
cam.aspect = targetRatio;
}
}
Ground (Ground, Ground_2 and Ground_3)
Ground Sprite (Within the GameObject "Ground" there is a sprite)
What is wrong?
You are using Rect Transform for your ground objects. Rect Transform is part of the UI system and should be used for objects inside a Canvas. Just use the standard Transform for your ground objects.