I keep getting the error:
NullReferenceException: Object reference not set to an instance of an object
which brings me to the line:
if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, enemy.groundCheckDistance))
I did some testing and found out that the enemy was in fact set to the instance of this object because I printed the value for enemy.groundCheckDistance every update and it successfully printed the correct value from the from the enemy object every time. I even changed it in the inspector and it retrieved the correct value. I later discovered that all of the objects I set in the Start() method are turning to null only outside of the Start() method. (By this I mean the Animator, Rigidbody, Capsule Collider, etc. are turning null) I am 100% sure that an animator, capsule collider, rigidbody, and enemy are attached to this gameobject. However, I am creating them on Awake() in a different class. Could that be why I am getting the error? If it is how do I fix it?
This is the script where I add the components on Awake(). When I run the game I see that they have successfully been added.
using UnityEngine;
using UnityEngine.AI;
namespace MGCO.Characters.EnemyScripts
{
//USED TO CONSTRUCT THE COMPONENTS ON AN ENEMY AT RUNTIME
public class EnemyComponents : MonoBehaviour
{
//INITIALIZE ENEMY OBJECT
Enemy enemy;
void Awake()
{
enemy = GetComponent<Enemy>();
//MAKE SURE ENEMY IS NOT HUMANOID
if (!enemy.isHumanoid)
{
//CHECK TO SEE IF MESH FILTER ALREADY EXISTS
if (!GetComponent<MeshFilter>())
{
//INITIALIZE "MeshFilter"
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
//SET MESH OF MESHFILTER
meshFilter.mesh = enemy.enemyMesh;
}
//CHECK TO SEE IF MESH RENDERER ALREADY EXISTS
if (!GetComponent<MeshRenderer>())
{
//INITIALIZE "MeshRenderer"
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
//SET MESH MATERIAL
meshRenderer.material = enemy.meshMaterial;
}
}
//MAKE SURE ENEMY IS NOT HUMANOID
else
{
//CHECK TO SEE IF ANIMATOR ALREADY EXISTS
if (!GetComponent<Animator>())
{
//INITIALIZE "RuntimeAnimatorController"
Animator animator = gameObject.AddComponent<Animator>();
//SET RUNTIMEANIMATORCONTROLLER
animator.runtimeAnimatorController = enemy.runtimeAnimatorController;
//SET AVATAR
animator.avatar = enemy.avatar;
//SET ROOT ANIMATION BOOL
animator.applyRootMotion = enemy.applyRootMotion;
}
}
//CHECK TO SEE IF RIGIDBODY ALREADY EXISTS
if (!GetComponent<Rigidbody>())
{
//INITIALIZE "Rigidbody"
Rigidbody enemyRigidbody = gameObject.AddComponent<Rigidbody>();
//SET MASS
enemyRigidbody.mass = enemy.mass;
//SET DRAG
enemyRigidbody.drag = enemy.drag;
//SET ANGULAR DRAG
enemyRigidbody.angularDrag = enemy.angularDrag;
//SET USE GRAVITY
enemyRigidbody.useGravity = enemy.useGravity;
}
//CHECK TO SEE IF NAVMESHAGENT ALREADY EXISTS
if (!GetComponent<NavMeshAgent>())
{
//INITIALIZE "NavMeshAgent"
NavMeshAgent navMeshAgent = gameObject.AddComponent<NavMeshAgent>();
//SET NAVMESH SPEED
navMeshAgent.speed = enemy.speed;
//SET BASE OFFSET
navMeshAgent.baseOffset = enemy.baseOffset;
//SET ANGULAR SPEED (HOW FAST ENEMY TURNS CORNERS)
navMeshAgent.angularSpeed = enemy.angularSpeed;
//SET ACCELERATION
navMeshAgent.acceleration = enemy.acceleration;
//SET STOPPING DISTANCE
navMeshAgent.stoppingDistance = enemy.stoppingDistance;
//SET OBSTACLE AVOIDANCE RADIUS
navMeshAgent.radius = enemy.navmeshRadius;
//SET OBSTACLE AVOIDANCE HEIGHT
navMeshAgent.radius = enemy.navmeshHeight;
}
//CHECK TO SEE IF COLLIDER ALREADY EXISTS
if (!GetComponent<CapsuleCollider>())
{
//INITIALIZE "CapsuleCollider"
CapsuleCollider capsuleCollider = gameObject.AddComponent<CapsuleCollider>();
//SET CENTER
capsuleCollider.center = enemy.center;
//SET COLLIDER RADIUS
capsuleCollider.radius = enemy.colliderRadius;
//SET COLLIDER HEIGHT
capsuleCollider.height = enemy.colliderHeight;
}
}
}
}
This is the script that gets the error:
using UnityEngine;
namespace MGCO.Characters.EnemyScripts
{
public class EnemyAnimation : MonoBehaviour
{
//REFERENCE GAMEOBJECTS
Enemy enemy;
Animator animator;
Rigidbody enemyRigidbody;
CapsuleCollider capsuleCollider;
//INITIALIZE VARS
Vector3 groundNormal;
Vector3 capsuleCenter;
private bool isCrouching;
private bool isGrounded;
private float originalGroundCheckDistance;
private float turnAmount;
private float forwardAmount;
private float capsuleHeight;
//VALUE FOR HALF (NEVER CHANGES)
const float half = 0.5f;
//RUNS ON START
void Start()
{
enemy = GetComponent<Enemy>();
animator = GetComponent<Animator>();
enemyRigidbody = GetComponent<Rigidbody>();
capsuleCollider = GetComponent<CapsuleCollider>();
capsuleHeight = capsuleCollider.height;
capsuleCenter = capsuleCollider.center;
//FREEZE RIGIDBODY ROTATION
enemyRigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
//SET LOCAL VARIABLE
originalGroundCheckDistance = enemy.groundCheckDistance;
}
...
//CHECKS POSITION OF GROUND BENEATH PLAYER
void CheckGroundStatus()
{
//INITIALIZE RAYCASTHIT
RaycastHit hitInfo;
//IF RAYCASTS DOWN TO "GroundCheckDistance" VARIABLE
if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, enemy.groundCheckDistance))
{
//SET GROUNDNORMAL TO THE SURFACE OF THE RAYCASTHIT
groundNormal = hitInfo.normal;
//SET JUMPING FALSE
isGrounded = true;
//ANIMATOR APPLY ROOT MOTION
animator.applyRootMotion = true;
}
else
{
//SET JUMPING TO TRUE
isGrounded = false;
//SET GROUNDNORMAL TO GO UP
groundNormal = Vector3.up;
//ANIMATOR DO NOT APPLY ROOT MOTION
animator.applyRootMotion = false;
}
}
}
}
And thank you for your help :)
//REFERENCE GAMEOBJECTS
Enemy enemy;
Animator animator;
Rigidbody enemyRigidbody;
CapsuleCollider capsuleCollider;
Here you declare your references but you are not ASSIGNING them anywhere.
Rigidbody enemyRigidbody = gameObject.AddComponent<Rigidbody>();
Here, you add the rigidbody component and it returns a reference to itself. However, you assigned the reference to a DIFFERENT variable called enemyRigidbody instead of the one you declared above.
Even though the variable names are similar, declaring a variable within a smaller scope/declaration space (One within a class, another within a method) creates a ENTIRELY NEW variable with the same name but ONLY USABLE WITHIN THE METHOD ITSELF.
What you want is:
enemyRigidbody = gameObject.AddComponent<Rigidbody>();
which is a reference to the Rigidbody you declared above.
Related
The main goal of the script in this case is when the player is getting too close to the fire to slowly stop walking automatic wait some seconds then automatic rotate and start walking again and then stop again slowly.
The script is working when the player is entering the collider area this is the target variable position so the player is rotating back to where he was entered from.
The idea is to make something nice to prevent from the player to get into the fire. but i'm facing two problems.
The first problem is that in the script i'm using a box collider as the area to prevent from the player to get closer to the fire and it's hard to dynamic change the collider area shape to cover all sides.
This is a screenshot of the collider area :
I marked to possible places if the player is coming from closer to the fire when he enter/exit the collider area he is already inside the fire and it's hard to change the collider size and scale to cover this places too.
I wonder if using distance check instead of the box collider will be easier ? and if it is how to change the script to use distance check instead the box collider trigger enter and exit events ?
The second problem is that sometimes when the player got close to the fire and then rotating back outside the collider area when he exit at that point(position) the player is making a walking circle around the target position and then continue outside. not sure why the player is making sometimes a moving around the target point ?
The script is attached to the player :
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class SpreadedFireDistanceCheck : MonoBehaviour
{
public Vector3 target;
public float rotationSpeed;
public GameObject descriptionTextImage;
public TextMeshProUGUI text;
public Transform player;
private Animator anim;
private ThirdPersonUserControl thirdPersonUserControl;
private bool startRotBack = false;
private bool rot = false;
private bool doOnce = true;
private bool getTargetPos = false;
float angle = 10;
// Start is called before the first frame update
void Start()
{
anim = player.GetComponent<Animator>();
thirdPersonUserControl = player.GetComponent<ThirdPersonUserControl>();
}
// Update is called once per frame
void Update()
{
if (startRotBack)
{
if (doOnce)
{
anim.SetTrigger("Idle Exit");
StartCoroutine(WaitTime());
doOnce = false;
}
if (rot)
{
anim.SetBool("Walk", true);
if (Vector3.Angle(player.transform.forward, target - player.transform.position) > angle)
{
Vector3 dir = target - player.transform.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);
Vector3 rotation = Quaternion.Lerp(player.transform.rotation, lookRotation, Time.deltaTime * rotationSpeed).eulerAngles;
player.transform.rotation = Quaternion.Euler(0f, rotation.y, 0f);
}
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.name == "kid_from_space")
{
if (rot)
{
rot = false;
anim.SetBool("Walk", false);
anim.SetTrigger("Idle Enter");
thirdPersonUserControl.enabled = true;
getTargetPos = false;
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.name == "kid_from_space")
{
if (getTargetPos == false)
{
target = player.transform.position;
getTargetPos = true;
}
descriptionTextImage.SetActive(true);
text.text = "This flames are so hot, i better stay away.";
thirdPersonUserControl.enabled = false;
doOnce = true;
startRotBack = true;
}
}
private IEnumerator WaitTime()
{
yield return new WaitForSeconds(5f);
text.text = "";
descriptionTextImage.SetActive(false);
rot = true;
}
}
In general the script is working but this two problem i mentioned above is hard to solve.
The player have this components :
Animator , Rigidbody , Capsule Collider , Third Person User Control (Script) , Third Person Character (Script)
I have a physics enabled sphere in my scene and a character that i can move around using WASD. now what i want is that as soon as player hits the ball it should not be physics enabled anymore and move along with player as like the player is holding it in the hands.
What i have tried so far:
i was able to do it but not at all perfect how i want it to be. i tried OnCollisionEnter to detect the collision, i made the sphere a child of the player, i used isKinematic = true as soon as collision is detected and i used Vector3.MoveTowards for the object to follow the position as the player.
I have attached two reference videos below
Reference Clip(The style i am aiming for):https://www.youtube.com/watch?v=fi-GB0RwLr0
MyVersion(This is what i am able to do):https://www.youtube.com/watch?v=JtV11KHY4pU
overall, if you watch the clips you can see how my version is very rigid, stucky, buggy, not so light weight feeling at all. Cut me a slack as well if i did anything way wrong, i started unity like just in the previous month.
This is my Player script through which i am controlling it all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
public float MoveSpeed;
[SerializeField]private Rigidbody playerRig;
[SerializeField] private Rigidbody ballRig;
[SerializeField] private GameObject Ball;
[SerializeField] private GameObject heldObj;
private bool isTouching = false;
private Vector3 offset = new Vector3(0, 1, 3);
private Vector3 force;
public float jump;
private bool isGrounded = true;
private void Start()
{
Ball = GameObject.FindGameObjectWithTag("Ball");
ballRig.GetComponent<Rigidbody>();
playerRig.GetComponent<Rigidbody>();
}
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
playerRig.velocity = new Vector3(x*MoveSpeed, playerRig.velocity.y,z*MoveSpeed);
Vector3 vel = playerRig.velocity;
vel.y = 0;
if (vel.x != 0 || vel.z != 0)
{
transform.forward = vel;
}
if(transform.position.y < -10)
{
GameOver();
}
ReleaseTheBall();
if (isTouching == true)
{
ballRig.transform.position = Vector3.MoveTowards(ballRig.transform.position, playerRig.transform.position, 5f);
}
}
void Jump()
{
force = new Vector3(0, jump, 0);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
isGrounded = false;
playerRig.AddForce(force, ForceMode.Impulse);
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Ball" && heldObj == null)
{
ballRig.isKinematic = true;
// ballRig.constraints = RigidbodyConstraints.FreezeRotation;
ballRig.transform.parent = playerRig.transform;
isTouching = true;
Debug.Log(" BALL PICKED UP AND MOVING ");
heldObj = Ball;
}
}
public void ReleaseTheBall()
{
if (Input.GetKeyDown(KeyCode.Space) && heldObj != null)
{
isTouching = false;
heldObj = null;
ballRig.transform.parent = null;
//ballRig.constraints = RigidbodyConstraints.None;
ballRig.isKinematic = false;
//Vector3 forceDirection = new Vector3(playerRig.transform.position.x, 0, playerRig.transform.position.z);
Vector3 rotationDirection = new Vector3(playerRig.transform.rotation.x, playerRig.transform.rotation.y, playerRig.transform.rotation.z);
ballRig.AddForce(new Vector3(transform.position.x,0,transform.position.z) * 5, ForceMode.Impulse);
ballRig.AddTorque(rotationDirection * 1);
Debug.Log("BALL RELEASED");
}
}
public void GameOver()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
i know there could be some very inefficient lines of code here, feel free to call out those lines as well.
So, basically what i want is that i want my player to pick up the ball as soon as it hits it and the ball should move exactly like player as if he is holding the ball in his hands and when i hit the spacebar i want the ball to be released into the direction i am looking(this part is also very tricky for me), what i am trying to do is i want to do exactly like in the refrence clip. Thanks a lot in advance.
one more thing, when i stop pressing any key to move(WASD), my player transform keeps changing values up and down and it slightly starts moving. i mean when i dont press any key, it still kinda moves.
First of all, you have indeed quite a lot of lines that aren't useful in here and that could be optimized.
Anyway, I hope this will do what you want it to do:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(Rigidbody))]
public class Player : MonoBehaviour {
[Header("Movement Setup")]
[Tooltip("Translation speed in m/s")]
[SerializeField] float _TranslationSpeed;
[Tooltip("Rotation speed in °/s")]
[SerializeField] float _RotationSpeed;
[Tooltip("Interpolation speed [0;1]")]
[SerializeField] float _InterpolationSpeed;
[Tooltip("Strength for jumps")]
[SerializeField] float _JumpingStrength;
[Tooltip("Strength for shoots")]
[SerializeField] float _ShootingStrength;
[Header("Other Settings")]
[Tooltip("Where the helded item will be placed")]
[SerializeField] Transform _ContactPoint;
[Tooltip("Which layers should be considered as ground")]
[SerializeField] LayerMask _GroundLayer;
// The player's rigid body
Rigidbody _Rigidbody;
// Is the player on the ground
bool _IsGrounded;
// Rigidbody of what's the player is helding. Null if the player isn't holding anything
Rigidbody _HeldedItemRigidbody;
// Getter to _IsGrounded, return true if the player is grounded
public bool IsGrounded { get { return _IsGrounded; } }
private void Start() {
_Rigidbody = GetComponent<Rigidbody>(); // The player's rigidbody could be serialized
}
void FixedUpdate() {
float horizontalInput, verticalInput;
// Getting axis
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
// Moving toward the x
Vector3 moveVect = transform.forward * _TranslationSpeed * Time.fixedDeltaTime * verticalInput;
_Rigidbody.MovePosition(_Rigidbody.position + moveVect);
// Rotating the player based on the horizontal input
float rotAngle = horizontalInput * _RotationSpeed * Time.fixedDeltaTime;
Quaternion qRot = Quaternion.AngleAxis(rotAngle, transform.up);
Quaternion qRotUpRight = Quaternion.FromToRotation(transform.up, Vector3.up);
Quaternion qOrientationUpRightTarget = qRotUpRight * _Rigidbody.rotation;
Quaternion qNewUpRightOrientation = Quaternion.Slerp(_Rigidbody.rotation, qOrientationUpRightTarget, Time.fixedDeltaTime * _InterpolationSpeed); // We're using a slerp for a smooth movement
_Rigidbody.MoveRotation(qRot * qNewUpRightOrientation);
// This prevents the player from falling/keep moving if there is no input
if (_IsGrounded) _Rigidbody.velocity = Vector3.zero;
_Rigidbody.angularVelocity = Vector3.zero;
if (transform.position.y < -10) GameOver();
if (Input.GetKey(KeyCode.Mouse0) && _HeldedItemRigidbody != null) ShootTheBall();
if (!_IsGrounded) return; // What's below this won't be executed while jumping
if (Input.GetKey(KeyCode.Space)) Jump();
}
void Jump() {
_IsGrounded = false;
_Rigidbody.AddForce(_JumpingStrength * transform.up, ForceMode.Impulse);
}
private void OnCollisionEnter(Collision collision) {
// Checking if the player encountered a ground object
if ((_GroundLayer & (1 << collision.gameObject.layer)) > 0) _IsGrounded = true;
if (collision.gameObject.tag == "Ball") { // this can be improved as tags are error prone
// Setting the item position to contact point
collision.gameObject.transform.position = _ContactPoint.position;
// Getting the rigidbody
_HeldedItemRigidbody = collision.gameObject.GetComponent<Rigidbody>();
// Setting the parent
_HeldedItemRigidbody.transform.parent = transform;
// Setting the body to be kinematic
_HeldedItemRigidbody.isKinematic = true;
// Stopping any movement or rotation
_HeldedItemRigidbody.velocity = Vector3.zero;
_HeldedItemRigidbody.angularVelocity = Vector3.zero;
}
}
public void ShootTheBall() {
// Reverting what's done in OnCollisionEnter
_HeldedItemRigidbody.transform.parent = null;
_HeldedItemRigidbody.isKinematic = false;
// Adding force for the shoot
_HeldedItemRigidbody.AddForce(transform.forward * _ShootingStrength, ForceMode.Impulse);
// Resetting helding item to null
_HeldedItemRigidbody = null;
}
public void GameOver() {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
You have to add a layer, and set the ground to this new layer.
Set up the variable as you like, don't hesitate to mess up with it a little bit to find what suits you (and don't forget to set up the player's mass (in rigidbody) as well!!)
Exemple of setup for the player script
Last things, you have to add an empty game object to your player which will be where the ball will be placed. I know it's a little bit dirty but place it a bit far from the player (more than the ball's radius) or it will bug. We could do something programatically to fix the ball at a spot and adapt this spot to the ball's radius.
This is an example of player
As you can see, I have the empty "player" gameobject which has the rigidbody, the collider and the player script.
The GFX item which is just the capsule.
If you don't know about this: the player game object is actually on the floor (at the player's feet) so it's better for movements. If you do this, don't forget to adjust the player's capsule collider to fit the GFX.
I also have the contact point.
Forget about the nose stuff, it's just a way to see where the player is facing when you just have a capsule.
If you have any question, feel free to ask.
Im working on a first person shooter. I have an aim function, which puts the pistol right in front of the camera, to make it look like your holding it in front of you. Im trying to make it so the pistol will also rotate with the camera on the Z axis, so that way the pistol wont stay still, because that looks odd and gets in the way. To do this, I tried this:
GPR.gun.transform.rotation = Quaternion.Euler(0, 0, plrCam.transform.rotation.z);, however this ends up rotating the gun very slightly around the z axis, and mainly rotating it around the y axis whenever I move my camera. I am a beginner programmer in Unity so please try to make answers more digestible to beginners so I can understand it. Here is my full script:
using System.Collections.Generic;
using UnityEngine;
public class PistolFire : MonoBehaviour
{
//Gun Properties
public float range = 50f;
public float damage = 10f;
//Sensitivity decrease for looking down the sights
public float downSights = 5f;
//Other vars
private playerGunControls playerGun;
private GameObject plrCam;
private Camera fpsCam;
private ParticleSystem muzzleFlash;
private GameObject impactEffect;
private bool aimed = false;
private GameObject aimPos;
private GunPickupRaycast GPR;
private GameObject handPos;
private GameObject Player;
// Start is called before the first frame update
void Start()
{
//Getting objects because gun is instantiated, so this is necessary
plrCam = GameObject.Find("Player Camera");
playerGun = plrCam.GetComponent<playerGunControls>();
fpsCam = plrCam.GetComponent<Camera>();
muzzleFlash = GetComponentInChildren<ParticleSystem>();
impactEffect = GameObject.Find("Impact Effect");
aimPos = GameObject.Find("aimPos");
GPR = plrCam.GetComponent<GunPickupRaycast>();
handPos = GameObject.Find("Hand Pos");
Player = GameObject.Find("Player");
}
// Update is called once per frame
void Update()
{
//Check for shoot button down
if (Input.GetButtonDown("Fire1"))
{
if (playerGun.holding == "Pistol")
{
Shoot();
}
}
//Check if aim button down
if (Input.GetButton("Fire2"))
{
if (playerGun.holding == "Pistol")
{
Aim();
}
}
//Check if no longer aiming to reset to normal
if (aimed == true && !(Input.GetButton("Fire2")))
{
Unaim();
}
}
void Shoot()
{
muzzleFlash.Play();
RaycastHit hit;
if(Physics.Raycast(plrCam.transform.position, plrCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Health health = hit.transform.GetComponent<Health>();
if (health != null)
{
health.TakeDamage(damage);
}
//Instantiate the Impact Effect
GameObject IE = Instantiate(impactEffect, hit.point, Quaternion.identity);
Destroy(IE, 1.5f);
}
}
void Aim()
{
aimed = true;
Debug.Log("Aiming");
GPR.gun.transform.position = aimPos.transform.position;
GPR.gun.transform.rotation = Quaternion.Euler(0, 0, plrCam.transform.rotation.z);
}
void Unaim()
{
GPR.gun.transform.position = handPos.transform.position;
Debug.Log("No longer aiming");
aimed = false;
}
}
I fixed my problem by making the gun a child of my camera instead of a child of my player.
I'm having an issue instantiating Prefabs in Unity.
I'm working on a game where you have enemies moving towards you and you have to kill them. My original enemy game object moved towards the player with little to no problem, but the instantiations of that object wouldn't move.
To make matters more confusing, when I copied the game object and added it to the scene without instantiation, both game objects would move towards the player just fine.
Enemy script:
public class EnemieController : MonoBehaviour
{
[SerializeField]
float moveSpeed = 1;
[SerializeField]
private Rigidbody2D rb;
public Transform player;
public float health = 50;
void Start()
{
rb = GetComponent<Rigidbody2D>();
Debug.Log(player);
}
// Update is called once per frame
void Update()
{
MoveTowardsPlayer();
}
void MoveTowardsPlayer()
{
Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (player.position - transform.position).normalized;
rb.velocity = new Vector2(direction.x * moveSpeed, direction.y * (moveSpeed * 1));
}
}
Instantiation Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using System;
[System.Serializable]
public class GameManagement : MonoBehaviour
{
public GameObject circleEnemie;
public Transform player;
[SerializeField]
float moveSpeed = 1;
// Start is called before the first frame update
void Start()
{
//Get random position to spawn ball
Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));
GameObject enemie = Instantiate(circleEnemie, screenPosition, Quaternion.identity) as GameObject;
enemie.name = "enemiecircle";
enemie.tag = "Enemie";
}
// Update is called once per frame
void Update()
{
}
}
And if wanted, here are the enemies inspector specifications
Inspector Specifications
Sorry about the link, my reputation points are yet to reach 10 so I can't post images directly.
My guess would be that the Player referenced in your enemy prefab is a prefab itself that never moves.
You should make the prefab field itself of type EnemyController. This makes sure you only can reference a prefab here that actually has an EnemyController attached.
Then after Instantiate you can pass in the player reference of the GameManagement script like
public class GameManagement : MonoBehaviour
{
// Give this field the correct type
public EnemyController circleEnemie;
public Transform player;
[SerializeField]
float moveSpeed = 1;
// Start is called before the first frame update
void Start()
{
//Get random position to spawn ball
Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));
// Instantiate returns the same type as the given prefab
EnemyController enemy = Instantiate(circleEnemie, screenPosition, Quaternion.identity);
enemy.name = "enemiecircle";
enemy.gameObject.tag = "Enemie";
// Now pass in the player reference
enemy.player = player;
}
// NOTE: When not needed better remove Unity message methods
// It would just cause overhead
//void Update()
//{
//}
}
Sidenote: In your EnemyController in MoveTowardsPlayer what do you need this for?
Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Then whenever dealing with Rigidbody do the things in FixedUpdate otherwise it might break the physics and collision detection!
Then also don't use the Transform values but again the Rigidbody
private void FixedUpdate ()
{
MoveTowardsPlayer();
}
private void MoveTowardsPlayer ()
{
var direction = ((Vector2)(player.position - rb.position)). normalized;
rb.velocity = direction * moveSpeed;
}
I stumbled across a problem while working with the book "Unity in Action". At the end of chapter 3 you'd end up with the basics for a simple fps game. It's basically a player (camera attached to it) in a simple and small level which only exists out of a number of cubes forming walls and the floor etc. These cubes all have box collider on them. Now the player is also able to shoot at moving enemies which are also able to shoot. This was done by Raycast/RaycastHit. All this worked perfectly so I wanted to add something that reassembles bullet holes, simply by instantiating a black sphere object on the wall where the fireball (this is the object the enemy and the player shoot) hits it. This works BUT sometimes the fireball object just goes through the wall. In case there is another wall behind it, the fireball object gets destroyed by this second wall and the desired sphere is created on the second wall instead of on the first wall.
I changed the speed in the fireball from 20 to 10 and then back to 20 and at a speed of 10, the success rate is around 19/20, while at a speed of 20 it's around 6/10.
Here is the code of the fireball which is supposed to check whether it hits the player (then health is deducted, that works fine) or hits the enemy (then the enemy falls over, also works fine) or hits the wall in which case the sphere should be created.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fireball : MonoBehaviour {
public float speed = 10.0f;
public int damage = 1;
[SerializeField] private GameObject wallHit;
private GameObject _wallHit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(0,0, speed * Time.deltaTime);
}
void OnTriggerEnter(Collider other){
RaycastHit hit;
PlayerCharacter player = other.GetComponent<PlayerCharacter>();
ReactiveTarget target = other.GetComponent<ReactiveTarget>();
WallBehavior wall = other.GetComponent<WallBehavior>();
if(player != null){
player.Hurt(damage);
}
if(target != null){
target.ReactToHit();
}
if(wall != null){
if(Physics.Raycast(transform.position, transform.forward, out hit)){
wall.WallWasHit(hit);
}
}
Destroy(this.gameObject);
}
}
As you can see one thing I tried was to give each wall a WallBehavior script, which looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallBehavior : MonoBehaviour {
[SerializeField] private GameObject wallHit;
private GameObject _wallHit;
static int count = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void WallWasHit(RaycastHit hit){
count++;
Debug.Log("Wall was hit: " + count);
_wallHit = Instantiate(wallHit) as GameObject;
_wallHit.transform.position = hit.point;
}
}
But none of my attempts to understand why this happens so infrequently was successful so far and I hope someone can help me with this because I feel like might be crucial for my learning before I continue with the book! Thanks in advance. if further information are needed, I am happy to provide them. See the following picture for a better visualization of the issue.
EDIT:
As mentioned in the answers, I replaced the fireball script accordingly but I am not sure how to change the following script likewise. This script is on my camera which is on my player object. In the Update function the Fireball is instantiated and also given a movement, so I guess this causes the problems?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayShooter : MonoBehaviour {
private Camera _camera;
[SerializeField] private GameObject fireballPrefab;
private GameObject _fireball;
void Start () {
_camera = GetComponent<Camera>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void OnGUI(){
int size = 12;
float posX = _camera.pixelWidth/2 - size/4;
float posY = _camera.pixelHeight/2 - size/2;
GUI.Label(new Rect(posX, posY, size, size), "X");
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0)){
Vector3 point = new Vector3(_camera.pixelWidth/2, _camera.pixelHeight/2, 0);
_fireball = Instantiate(fireballPrefab) as GameObject;
_fireball.transform.position = transform.TransformPoint(Vector3.forward * 1.5f);
_fireball.transform.rotation = transform.rotation;
Ray ray2 = _camera.ScreenPointToRay(point);
RaycastHit hit;
if(Physics.Raycast(ray2, out hit)){
GameObject hitObject = hit.transform.gameObject;
ReactiveTarget target = hitObject.GetComponent<ReactiveTarget>();
if(target !=null){
target.ReactToHit();
}
}
}
}
}
This is not how to move a Rigidbody Object and moving it like this could cause so many issues including the one mentioned in your question. Objects with Rigidbody should be moved with the Rigidbody component and with the functions like Rigidbody.MovePosition, Rigidbody.AddForce and Rigidbody.velocity not by the transform or transform.Translate.
Also, you should move the Rigidbody Object in the FixedUpdate function instead of the Update function. If you are moving your other Rigidbody Objects by their transform then you must fix them too. The example below replaces transform.Translate with Rigidbody.MovePosition in the Fireball script:
public float speed = 10.0f;
public int damage = 1;
[SerializeField]
private GameObject wallHit;
private GameObject _wallHit;
public Rigidbody rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
//Move to towards Z-axis
Vector3 pos = new Vector3(0, 0, 1);
pos = pos.normalized * speed * Time.deltaTime;
rb.MovePosition(rb.transform.position + pos);
}
void OnTriggerEnter(Collider other)
{
RaycastHit hit;
PlayerCharacter player = other.GetComponent<PlayerCharacter>();
ReactiveTarget target = other.GetComponent<ReactiveTarget>();
WallBehavior wall = other.GetComponent<WallBehavior>();
if (player != null)
{
player.Hurt(damage);
}
if (target != null)
{
target.ReactToHit();
}
if (wall != null)
{
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
wall.WallWasHit(hit);
}
}
Destroy(this.gameObject);
}
If you still run into issues, use Rigidbody.velocity instead:
void FixedUpdate()
{
Vector3 pos = Vector3.zero;
pos.z = speed * Time.deltaTime;
rb.velocity = pos;
}
Sometimes, depending on the size of the object and the speed it is moving by, you many need to set its Rigidbody Interpolate from None to Interpolate and Collision Detection to Continuous.
Don't forget to remove the code in the Update function.
EDIT:
I looked at your project and found new problems:
1.You enabled IsTrigger on your "Fireball" GameObject. Please uncheck the IsTrigger on the collider.
2.You just want to shoot a bullet. The force should be added once only not every FixedUpdate. Add the force in the Start function instead of the FixedUpdate function. Use Rigidbody.velocity instead of Rigidbody.MovePosition.
Remove the FixedUpdate function and the code inside it.
This should be your new Start function:
void Start()
{
rb = GetComponent<Rigidbody>();
Vector3 pos = transform.forward * speed * Time.deltaTime;
rb.velocity = pos;
}