I have error in collision on bullet shoot - c#

I have a problem on the bullet collision the enemy
I am using the OnCollisionEnter() method in the Bullet Script
Bullet Script :
public class BulletScript : MonoBehaviour
{
public float TimeForDestory = 10f;
public float Speed = 0.5f;
// Use this for initialization
void Start () {
}
public void OnCollisionEnter(Collision col)
{
Debug.Log("Collision");
if (col.gameObject.tag == "Enemy")
{
Debug.Log("Collision");
}
}
// Update is called once per frame
void Update () {
transform.Translate(0, -Speed, 0);
if (TimeForDestory < 0f)
{
Destroy(gameObject);
}else
{
TimeForDestory -= 0.1f;
}
}
}
The Bullet Not Collision Anything
the bullet is out of the objects because I am using Method Instantiate() in the Player Script
Player Script :
public class Player : MonoBehaviour
{
//Player
//Move
public float defualtSpdPlayer = 1f;
public float plsSpd = 0.1f;
public float maxPlayer = 2f;
private float speed = 0;
//Bullet
public Transform bulletSpawn;
public GameObject bullet;
public Texture ImgBackground;
public Texture ImgWhiteGround;
public Texture ImgReloading;
public float bulletDamge = 1f;
public float bulletSpeed = 0.1f;
public float ReloadTime = 0.5f;
public float ReloadFillAmontX = 2f;
private float reload = 0;
private float reloadFillAmont = 0;
private bool readyToShoot = true;
private string status = "Ready";
//GUI
[HideInInspector]
public bool guiShow = true;
void Start () {
MoveStart();
BulletStart();
}
private void MoveStart()
{
speed = defualtSpdPlayer;
}
private void BulletStart()
{
if(ReloadTime > 1)
{
Debug.LogError("The Reload Time Is Bigger 1");
}
}
void OnGUI()
{
//Verables
float cvReloadingWidth = 150;
float cvReloadingHeight = 150;
float cvReloadngY = Screen.height - cvReloadingHeight;
float cvReloadngX = Screen.width / 2 - 70;
//Rects
Rect cvReloadingImages = new Rect(cvReloadngX, cvReloadngY, cvReloadingWidth, cvReloadingHeight);
Rect cvReloadinglalReloadTime = new Rect(cvReloadngX + 65, cvReloadngY + 75, cvReloadingWidth, cvReloadingHeight);
Rect cvReloadinglalStatus = new Rect(cvReloadngX + 40, cvReloadngY + 50, cvReloadingWidth, cvReloadingHeight);
//Texts
//Values
//Texture
GUI.DrawTexture(cvReloadingImages, ImgBackground);
GUI.DrawTexture(cvReloadingImages, ImgWhiteGround);
//GUI.DrawTexture(cvReloadingImages, ImgReloading);
if (reloadFillAmont <= 0)
{
GUI.skin.label.normal.textColor = Color.green;
GUI.skin.label.fontSize = 25;
GUI.skin.label.fontStyle = FontStyle.Bold;
GUI.Label(cvReloadinglalStatus, status);
GUI.skin.label.fontSize = 15;
GUI.skin.label.fontStyle = FontStyle.Bold;
GUI.Label(cvReloadinglalReloadTime, ReloadTime.ToString());
}
else
{
GUI.skin.label.normal.textColor = Color.red;
GUI.Label(cvReloadinglalStatus, status);
GUI.Label(cvReloadinglalReloadTime, reloadFillAmont.ToString());
}
//GUI
}
void Update () {
Move();
Shoot();
}
private void Move()
{
//Move
if (transform.rotation.y < 180)
{
plsSpeed();
transform.Translate(Input.GetAxis("BW") * speed * Time.deltaTime, 0, Input.GetAxis("FW") * speed * Time.deltaTime);
}
if (transform.rotation.y >= 180)
{
plsSpeed();
transform.Translate(-Input.GetAxis("BW") * speed * Time.deltaTime, 0, -Input.GetAxis("FW") * speed * Time.deltaTime);
}
}
private void plsSpeed()
{
if (speed > maxPlayer)
{
speed = defualtSpdPlayer;
}
else
speed += plsSpd;
}
//Gun Shoot
private void Shoot()
{
if (readyToShoot)
{
if (Input.GetMouseButton(0))
{
Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
readyToShoot = false;
reload = ReloadTime;
status = "Reloading";
}
status = "Ready";
}
else
{
reloadFillAmont = reload * ReloadFillAmontX;
if (reload < 0)
{
readyToShoot = true;
}else
{
reload -= Time.deltaTime;
status = "Reloading";
}
}
}
}
What's the problem in the collision ?

You are using transform.Translate(0, -Speed, 0); to move the bullet, when you use this the bullet is going to move regardless any obstacle/force outside of it. To fix this add a Rigidbody to the bullet and change that line to GetComponent<Rigidbody>().MovePosition(transform.position - Vector3.up * Speed);
Because its a bullet it might be a fast moving object so put the Collision Detection to Continuous on the Rigidbody.

Related

player collision does not work with objects

I'm making an endless runner game and i have a question about my player colliding with so,e obstacles, I used Raycast but when i try to debug this collision doesn't occur.
Here my player Code.
public class Player : MonoBehaviour
{
private CharacterController controller;
public float speed;
public float jumpHeight;
private float jumpVelocity;
public float gravity;
public float rayRadius;
public LayerMask layer;
public float horizontalSpeed;
private bool isMovingLeft;
private bool isMovingRight;
private bool isDead;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Vector3 direction = Vector3.forward * speed;
if(controller.isGrounded)
{
if(Input.GetKeyDown(KeyCode.Space))
{
jumpVelocity = jumpHeight;
}
if(Input.GetKeyDown(KeyCode.RightArrow)&& transform.position.x < 3.58f && !isMovingRight)
{
isMovingRight = true;
StartCoroutine(RightMove());
}
if(Input.GetKeyDown(KeyCode.LeftArrow)&& transform.position.x > -3.58f && !isMovingLeft)
{
isMovingLeft = true;
StartCoroutine(LeftMove());
}
}
else
{
jumpVelocity -= gravity;
}
OnCollision();
direction.y = jumpVelocity;
controller.Move(direction * Time.deltaTime);
}
IEnumerator LeftMove()
{
for(float i = 0; i < 10; i += 0.1f)
{
controller.Move(Vector3.left * Time.deltaTime * horizontalSpeed);
yield return null;
}
isMovingLeft = false;
}
IEnumerator RightMove()
{
for (float i = 0; i < 10; i += 0.1f)
{
controller.Move(Vector3.right * Time.deltaTime * horizontalSpeed);
yield return null;
}
isMovingRight = false;
}
void OnCollision()
//The player will collide with obstacles that have a specific type of layer and dead.
{
RaycastHit hit;
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, rayRadius, layer) && !isDead)
{
Debug.Log("GameOver!");
speed = 0;
jumpHeight = 0;
isDead = true;
}
}
}
Instead of using raycast inside OnCollision function. You can do this with very much by following these steps:
Add a tag to your obstacles let say its "Obstacle"
Add collider to them
Check when you collide with someone that if it is obstacle or not if yes then you can call your dead function.
void OnCollisionEnter(Collision collision)
{
if(collision.transform.CompareTag("Obstacle")){
Debug.Log("GameOver!");
speed = 0;
jumpHeight = 0;
isDead = true;
}
}
In this way you can detect collision with super ease instead of using raycast.

OnPointerEnter and OnPointerExit not being triggered Unity

Alright so basically the issue that I've been having is that for some reason a GameObject is interfering with the OnPointerEnter function. I'm pretty sure that OnPointerEnter detects only UI. So that's why I'm extremely confused when seeing that a specific GameObject in this case the PlayerLogic GameObject (which you can see in the screenshot) is for some reason interfering with the detection of UI elements. The reason I believe it is this specific GameObject is because once I do PlayerLogic.SetActive(false); OnPointerEnter starts to work again, and I'm also sure that it isn't any of the children of PlayerLogic because I've tried turning them off specifically and it still didn't work.
Inspector of the PlayerLogic object
Hierarchy
The code I'm using to test OnPointerEnter
After some testing I've realized that its the specific issue lies within the Player script on the PlayerLogic GameObject. Now what confuses me is that once I turn off the Player component OnPointer doesn't work, but if I were to remove the Player component completely from the PlayerLogic GameObject OnPointerEnter works.
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;
using System.Collections.Generic;
public class Player : MonoBehaviour, TakeDamage {
[SerializeField] private Animator playerAnimator;
[SerializeField] private Transform mainCameraTransform;
private bool isRunning = false;
[SerializeField] private CharacterController controller;
public float speed = 10f;
[SerializeField] private float jumpForce = 3f;
[SerializeField] private float gravity = -10000.81f;
Vector3 velocity;
Vector3 desiredMoveDirection;
private float dashSpeed = 30f;
private float mouseX;
private float mouseY;
[SerializeField]
private Transform Target;
[SerializeField]
private Transform player;
private float turnSmoothVelocity;
private float time = 0f;
public bool playerIsAttacking = false;
[SerializeField] private Slider playerHealth, playerMana;
[SerializeField] private TextMeshProUGUI healthText, manaText;
private Vector3 originalSpawnPos;
private bool playerIsDead = false;
[SerializeField] private LayerMask enemyLayerMask;
[SerializeField] private Transform playerLook;
private ShowHPBar obj;
private bool HPBarShown = false;
private bool unshowingHPBar = false;
public bool lookingAtEnemy = false;
public RaycastHit hit;
[SerializeField] private Canvas abilityCanvas;
[SerializeField] private Slider CD1;
[SerializeField] private Slider CD2;
[SerializeField] private Slider CD3;
public List<Ability> currentlyEquippedAbilites = new List<Ability>();
public List<string> abilityTexts = new List<string>();
public float[] abilityCooldowns = new float[3];
private float manaRegenTime;
//public List<Image> abilityImages = new List<Image>();
private void Awake() {
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
private void Start() {
playerHealth.onValueChanged.AddListener(delegate {OnValueChangedHealth(); });
playerMana.onValueChanged.AddListener(delegate {OnValueChangedMana(); });
originalSpawnPos = transform.position;
}
private void Update() {
if (!playerIsDead) {
PlayerMovementAndRotation();
}
PlayerDash();
PlayerRun();
PlayerSeeEnemyHealth();
PlayerActivateAbility();
if (manaRegenTime > 0.5f) {
playerMana.value += playerMana.maxValue/100;
manaRegenTime = 0;
}
playerLook.rotation = mainCameraTransform.rotation;
time += Time.deltaTime;
manaRegenTime += Time.deltaTime;
#region Ability Cooldowns
if (currentlyEquippedAbilites.Count > 0) {
if (currentlyEquippedAbilites[0].cooldown <= abilityCooldowns[0])
{
currentlyEquippedAbilites[0].isOnCooldown = false;
abilityCooldowns[0] = 0;
CD1.value = 0;
}
else if (currentlyEquippedAbilites[0].isOnCooldown) {
abilityCooldowns[0] += Time.deltaTime;
CD1.value = currentlyEquippedAbilites[0].cooldown - abilityCooldowns[0];
}
}
if (currentlyEquippedAbilites.Count > 1) {
if (currentlyEquippedAbilites[1].cooldown <= abilityCooldowns[1])
{
currentlyEquippedAbilites[1].isOnCooldown = false;
abilityCooldowns[1] = 0;
CD2.value = 0;
}
else if (currentlyEquippedAbilites[1].isOnCooldown) {
abilityCooldowns[1] += Time.deltaTime;
CD2.value = currentlyEquippedAbilites[1].cooldown - abilityCooldowns[1];
}
}
if (currentlyEquippedAbilites.Count > 2) {
if (currentlyEquippedAbilites[2].cooldown <= abilityCooldowns[2])
{
currentlyEquippedAbilites[2].isOnCooldown = false;
abilityCooldowns[2] = 0;
CD3.value = 0;
}
else if (currentlyEquippedAbilites[2].isOnCooldown) {
abilityCooldowns[2] += Time.deltaTime;
CD3.value = currentlyEquippedAbilites[2].cooldown - abilityCooldowns[2];
}
}
#endregion
}
private void PlayerRun() {
if (Input.GetKey(KeybindsScript.RunKey) && (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)) {
playerAnimator.SetInteger("isRunning", 1);
playerAnimator.SetInteger("isIdle", 0);
playerAnimator.SetInteger("isWalking", 0);
speed = 15f;
}
else if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) {
playerAnimator.SetInteger("isWalking", 1);
playerAnimator.SetInteger("isIdle", 0);
playerAnimator.SetInteger("isRunning", 0);
speed = 10f;
}
else {
playerAnimator.SetInteger("isRunning", 0);
playerAnimator.SetInteger("isWalking", 0);
playerAnimator.SetInteger("isIdle", 1);
speed = 10f;
}
}
private void PlayerMovementAndRotation() {
bool isGrounded = controller.isGrounded;
if (isGrounded && velocity.y < 0) {
velocity.y = -2f;
}
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 moveDir = (transform.right*horizontal+transform.forward*vertical).normalized;
controller.Move(moveDir*Time.deltaTime*speed);
transform.eulerAngles = new Vector3(0f, mainCameraTransform.eulerAngles.y, 0f);
if (Input.GetKeyDown(KeybindsScript.JumpKey) && isGrounded) {
velocity.y = Mathf.Sqrt(jumpForce * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private void PlayerDash() {
if (Input.GetKeyDown(KeybindsScript.DashKeybind) && isRunning == false) {
if (Input.GetKey(KeybindsScript.MovementKeyBackward)) {
StartCoroutine(PlayerDashTiming(2));
}
else if (Input.GetKey(KeybindsScript.MovementKeyRight)) {
StartCoroutine(PlayerDashTiming(3));
}
else if (Input.GetKey(KeybindsScript.MovementKeyLeft)) {
StartCoroutine(PlayerDashTiming(4));
}
else {
StartCoroutine(PlayerDashTiming(1));
}
}
}
private void PlayerSeeEnemyHealth() {
if (Physics.Raycast(playerLook.position, playerLook.forward, out hit, 1000f, enemyLayerMask)) {
obj = hit.transform.gameObject.GetComponent<ShowHPBar>();
if (obj != null && !HPBarShown && !unshowingHPBar) {obj.ShowHPBarFunction(); HPBarShown = true;}
lookingAtEnemy = true;
}
else {
if (obj != null && HPBarShown) {StartCoroutine(UnShowHPBar(obj)); HPBarShown = false;}
}
}
public void PlayerEquipAbility(Ability ability, int place) {
if (currentlyEquippedAbilites.Count < place) {currentlyEquippedAbilites.Add(ability);}
else {currentlyEquippedAbilites[place-1] = ability;}
//if (abilityImages.Count < place) {abilityImages.Add(ability.icon);}
//else {abilityImages.Add(ability.icon);}
if (abilityTexts.Count < place) {abilityTexts.Add(ability.name);}
else {abilityTexts[place-1] = ability.name;}
for (int i=0;i < abilityTexts.Count;++i) {
abilityCanvas.transform.GetChild(i).GetChild(0).GetComponent<TextMeshProUGUI>().text = abilityTexts[i];
abilityCanvas.transform.GetChild(i).GetChild(1).GetComponent<Slider>().maxValue = ability.cooldown;
}
}
private void PlayerActivateAbility() {
if (Input.GetKeyDown(KeyCode.Alpha1)) {
if (currentlyEquippedAbilites[0] != null) {
if (currentlyEquippedAbilites[0].manaCost <= playerMana.value && !currentlyEquippedAbilites[0].isOnCooldown) {
ParticleEffect pe = currentlyEquippedAbilites[0].script.gameObject.GetComponent<ParticleEffect>();
playerMana.value -= currentlyEquippedAbilites[0].manaCost;
currentlyEquippedAbilites[0].isOnCooldown = true;
if (pe != null) {pe.PlayAnimation();}
}
}
}
}
public void OnValueChangedHealth() {
healthText.text = playerHealth.value + "/" + PlayerStats.HealthPoints;
}
public void OnValueChangedMana() {
manaText.text = playerMana.value + "/" + PlayerStats.ManaAmount;
}
public void TakeDamageFunction(float damage) {
playerHealth.value -= damage;
if (playerHealth.value <= 0) {
StartCoroutine(PlayerDeath());
}
}
IEnumerator PlayerDashTiming(int x) {
isRunning = true;
float time = 0f;
Vector3 savedVector = Vector3.zero;
switch (x) {
case 1:
savedVector = transform.forward;
break;
case 2:
savedVector = -transform.forward;
break;
case 3:
savedVector = transform.right;
break;
case 4:
savedVector = -transform.right;
break;
}
while(time < .3f)
{
time += Time.deltaTime;
controller.Move(savedVector * dashSpeed * Time.deltaTime);
yield return null;
}
yield return new WaitForSeconds(1.5f);
isRunning = false;
}
IEnumerator PlayerDeath() {
//Respawn
playerIsDead = true;
playerAnimator.enabled = false;
yield return new WaitForSeconds(1f);
playerHealth.value = 100;
transform.position = originalSpawnPos;
yield return new WaitForSeconds(0.1f);
playerIsDead = false;
playerAnimator.enabled = true;
}
IEnumerator UnShowHPBar(ShowHPBar obj) {
unshowingHPBar = true;
yield return new WaitForSeconds(1.5f);
obj.ShowHPBarFunction();
unshowingHPBar = false;
}
}
This is the Player.cs script.
I'm pretty sure that OnPointerEnter detects only UI.
No.
It works "out of the box" on UI because by default every Canvas contains a GraphicsRaycaster component which is then used and handled by the EventSystem.
For non-UI 3D objects you have to make sure
your objects have 3D Colliders
the Colliders are on the same object as the IPointerXY interfaces
on your Camera there is a PhysicsRayster component
For non-UI 2D objects quite similar
your objects have a 2D Collider
the colliders are on the same object as the IPointerXY interfaces
the Camera has a Physics2DRaycaster component
Note that it is possible that any other collider or in general raycast blocking object is between your input and the target object which would also prevent the OnPointerXY to be triggered on your objects.
The CharacterController
is simply a capsule shaped Collider
which can be told to move in some direction from a script.
which is probably blocking the input.
Now with your Player code:
You do
Cursor.lockState = CursorLockMode.Locked;
in Awake so even if you turn it off afterwards this already took effect.

how can I make the game object disappear?

when I was watching on YouTube, tutorials about endless runner on part 2, the game object wouldn't disappear when it hits the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obstacle : MonoBehaviour
{
public int damage = 1;
public float speed;
private void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
void onTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("rocket"))
{
//rocket takes damage 1
other.GetComponent<rocket>().health -= damage;
Debug.Log(other.GetComponent<rocket>().health);
Destroy(gameObject);
}
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class rocket : MonoBehaviour
{
private Vector2 targetPos;
public float Yincrement;
public float speed;
public float maxHeight;
public float minHeight;
public int health = 3;
void Update()
{
if (health <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.y < maxHeight)
{
targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement);
}
else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y > minHeight)
{
targetPos = new Vector2(transform.position.x, transform.position.y - Yincrement);
}
}
}
from this one https://www.youtube.com/watch?v=FVCW5189evI and I'm confused why it didn't work, can someone tell me what is wrong?
if you want to destroy use this, Correct Method to get the game object when collide is: other.gameObject
void onTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("rocket"))
{
//rocket takes damage 1
other.gameObject.GetComponent<rocket>().health -= damage;
Debug.Log(other.gameObject.GetComponent<rocket>().health);
Destory(other.gameObject);
}
}
public class megaStar : MonoBehaviour{
private Rigidbody2D rb;
private Animator _ani;
public bool canAttack = true;
[SerializeField] private Attack _attackObject;
private AudioSource _as;
public checkpoint lastCheckpoint;
public bool isDead = false;
private float _timer = 1.0f;
public float attackTimer = 2.0f;
public GameObject projectile;
public float projectileSpeed = 18.0f;
public Transform projectileAttackPoint;
public float timeDelayForNextShoot = 0.1f;
private float CONST_timeDelayForNextShoot;
// Start is called before the first frame update
void Start(){
CONST_timeDelayForNextShoot = timeDelayForNextShoot;
rb = GetComponent<Rigidbody2D>();
_as = GetComponent<AudioSource>();
_ani = GetComponent<Animator>();
_timer = attackTimer;
}
void Update(){
if (Input.GetKey(KeyCode.W))
{
rb.velocity = new Vector2(0f, 10f);
}
else if (Input.GetKey(KeyCode.S))
{
rb.velocity = new Vector2(0f, -10f);
}
else
{
rb.velocity = new Vector2(0f, 0f);
}
timeDelayForNextShoot -= Time.deltaTime;
if (Input.GetKeyDown(KeyCode.K) && timeDelayForNextShoot <= 0f){
_ani.SetBool("projectileAttack", true);
GameObject go = Instantiate(projectile, projectileAttackPoint.position, projectileAttackPoint.rotation) as GameObject;
go.GetComponent<Rigidbody2D>().velocity = new Vector2(-projectileSpeed, 0.0f);
Destroy(go, 2.0f);
timeDelayForNextShoot = CONST_timeDelayForNextShoot;
canAttack = false;
// new WaitForSeconds(1);
return;
}
}
void FixedUpdate()
{
if (!isDead)
{
Update();
}
else{
rb.velocity = Vector2.zero;
RigidbodyConstraints2D newRB2D = RigidbodyConstraints2D.FreezePositionY;
rb.constraints = newRB2D;
}
}
private void OnCollisionEnter2D(Collision2D collision){
if (collision.gameObject.CompareTag("Enemy"))
{
GetComponent<HitPoints>().TakeDamage(1);
}
}
}
Use SetActive instead of Destroy Function.
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("rocket"))
{
//rocket takes damage 1
other.gameObject.GetComponent<rocket>().health -= damage;
Debug.Log(other.gameObject.GetComponent<rocket>().health);
gameObject.SetActive(false);// it'll Hide GameObject instead of Destroying
// gameObject.SetActive(true);// to show again
}
}

Grenade spawns in the wrong location

In my game I want to have a floating monster that's attack throws a grenade at the player. My problem is that the grenade only spawns in 0, 0, 0. In my script I make it so that the zombies spawns in on its own location but for some reason that doesn't work. I tried making it spawn by having the spawn location equal new Vector3(100, 100, 100) but it still spawned at 0, 0, 0. I know that the co-routine runs because I put a Debug.Log. Thanks for the help!
Edit #2: I can't have a rigidbody on the script. I have edited the movement script and I have found that no mater what if a rigidbody is added then it will go to 0, 0, 0.
Edit #3: I updated the scripts
Here is my script: (Sorry if the code is bad)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZKAttack_lvl3 : MonoBehaviour
{
public Transform Player;
public float MoveSpeed = 2.0f;
public float InRadius = 10.0f;
public float AttackRange = 15.0f;
private Coroutine hasCourutineRunYet;
public GameObject grenade;
public GameObject FloatingMonster;
private Vector3 FloatingMon;
private Animator anim;
private Rigidbody rigid;
private void Start()
{
anim = GetComponent<Animator>();
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
rigid = GetComponent<Rigidbody>();
}
void Update()
{
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
transform.LookAt(Player);
float dstSqr = (Player.position - transform.position).sqrMagnitude;
bool inRadius = (dstSqr <= InRadius * InRadius);
bool inAttackRange = (dstSqr <= AttackRange * AttackRange);
anim.SetBool("AttackingPlayer", inAttackRange);
if (inRadius)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
}
rigid.AddForce(1, 10, 1);
if (inAttackRange)
{
if (hasCourutineRunYet == null)
{
hasCourutineRunYet = StartCoroutine(GrenadeAttack());
}
}
}
IEnumerator GrenadeAttack()
{
FloatingMon = FloatingMonster.transform.position;
GameObject bulletObject = Instantiate(grenade, FloatingMonster.transform.position, Quaternion.identity);
yield return new WaitForSeconds(2.0f);
}
}
Edit: This is the code for the movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrenadeMovement : MonoBehaviour
{
public float speed = 10f;
public float lifeDuration = 4.0f;
private float lifeTimer;
private Coroutine hasCourutineRunYet;
private Transform Player;
public SphereCollider sphereCollider;
// Use this for initialization
void Start()
{
lifeTimer = lifeDuration;
sphereCollider.enabled = false;
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
}
// Update is called once per frame
void Update()
{
lifeTimer -= Time.deltaTime;
if (lifeTimer >= 3f)
{
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
transform.LookAt(Player);
transform.position += transform.forward * speed * Time.deltaTime;
}
if (lifeTimer >= 0f)
{
transform.position = speed * transform.up * Time.deltaTime;
transform.position = speed * transform.forward * Time.deltaTime;
}
if (lifeTimer <= 0f)
{
if (hasCourutineRunYet == null)
{
hasCourutineRunYet = StartCoroutine(GrenadeExplosion());
}
}
}
private void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.tag == "Player")
{
if (hasCourutineRunYet == null)
{
hasCourutineRunYet = StartCoroutine(GrenadeExplosion());
}
}
}
IEnumerator GrenadeExplosion()
{
sphereCollider.enabled = true;
yield return new WaitForSeconds(1.0f);
Destroy(gameObject);
}
}
With the help of #ken I figured out that I couldn't use a rigidbody so I changed the insantiation to this: GameObject bulletObject = Instantiate(grenade, FloatingMonster.transform.position, Quaternion.identity);. I then changed the movement script for it to:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrenadeMovement : MonoBehaviour
{
public float speed = 10f;
public float lifeDuration = 4.0f;
private float lifeTimer;
private Coroutine hasCourutineRunYet;
private Transform Player;
public SphereCollider sphereCollider;
public Vector3 velocity;
// Use this for initialization
void Start()
{
lifeTimer = lifeDuration;
sphereCollider.enabled = false;
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
}
// Update is called once per frame
void Update()
{
lifeTimer -= Time.deltaTime;
if (lifeTimer >= 2f)
{
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
transform.LookAt(Player);
transform.position += transform.forward * 5 * Time.deltaTime;
}
if (lifeTimer >= 0f && lifeTimer <= 2f)
{
transform.position = 9.18f * transform.up * Time.deltaTime;
transform.position = 9.18f * transform.forward * Time.deltaTime;
}
if (lifeTimer <= 0f)
{
if (hasCourutineRunYet == null)
{
hasCourutineRunYet = StartCoroutine(GrenadeExplosion());
}
}
}
private void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.tag == "Player" || coll.gameObject.tag == "Terrain")
{
if (hasCourutineRunYet == null)
{
hasCourutineRunYet = StartCoroutine(GrenadeExplosion());
}
}
}
IEnumerator GrenadeExplosion()
{
sphereCollider.enabled = true;
yield return new WaitForSeconds(1.0f);
Destroy(gameObject);
}
}
Thank you for all your help, I have been trying to fix this all week.
You could set its position in the Instantiate line. Instantiate has several arguments. You can set its position in Instantiate, as well as its rotation and parent.
Set it to this:
IEnumerator GrenadeAttack()
{
GameObject bulletObject = Instantiate(grenade, FloatingMonster.transform.position, Quaternion.identity);
yield return new WaitForSeconds(2.0f);
}

NPC Follower - how to implement script

my goal is to have an NPC follow ThePlayer. I had a functioning NPCFollower script attached to a character with two animations, Idle and Walk.
I'm wondering if anyone knows how I can implement the NPCFollower script with the'SimpleCharacterControl'script which is attached to a character with an animator with multiple animations. I would like to call on two of the animations, walk and idle. I highly appreciate the help!
NPCFollow' script
public class NPCFollow : MonoBehaviour {
public GameObject ThePlayer;
public float TargetDistance;
public float AllowedDistance = 5;
public GameObject TheNPC;
public float FollowSpeed;
public RaycastHit Shot;
void Update () {
transform.LookAt(ThePlayer.transform);
if (Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward),out Shot))
{
TargetDistance = Shot.distance;
if(TargetDistance >= AllowedDistance)
{
FollowSpeed = 0.02f;
TheNPC.GetComponent<Animation>().Play("Walk");
}
else
{
FollowSpeed = 0;
TheNPC.GetComponent<Animation>().Play("Idle");
}
}
}
}
'SimpleCharacterControl'script
public class SimpleCharacterControl : MonoBehaviour {
private enum ControlMode
{
Tank,
Direct
}
[SerializeField] private float m_moveSpeed = 2;
[SerializeField] private float m_turnSpeed = 200;
[SerializeField] private float m_jumpForce = 4;
[SerializeField] private Animator m_animator;
[SerializeField] private Rigidbody m_rigidBody;
[SerializeField] private ControlMode m_controlMode = ControlMode.Direct;
private float m_currentV = 0;
private float m_currentH = 0;
private readonly float m_interpolation = 10;
private readonly float m_walkScale = 0.33f;
private readonly float m_backwardsWalkScale = 0.16f;
private readonly float m_backwardRunScale = 0.66f;
private bool m_wasGrounded;
private Vector3 m_currentDirection = Vector3.zero;
private float m_jumpTimeStamp = 0;
private float m_minJumpInterval = 0.25f;
private bool m_isGrounded;
private List<Collider> m_collisions = new List<Collider>();
private void OnCollisionEnter(Collision collision)
{
ContactPoint[] contactPoints = collision.contacts;
for(int i = 0; i < contactPoints.Length; i++)
{
if (Vector3.Dot(contactPoints[i].normal, Vector3.up) > 0.5f)
{
if (!m_collisions.Contains(collision.collider)) {
m_collisions.Add(collision.collider);
}
m_isGrounded = true;
}
}
}
private void OnCollisionStay(Collision collision)
{
ContactPoint[] contactPoints = collision.contacts;
bool validSurfaceNormal = false;
for (int i = 0; i < contactPoints.Length; i++)
{
if (Vector3.Dot(contactPoints[i].normal, Vector3.up) > 0.5f)
{
validSurfaceNormal = true; break;
}
}
if(validSurfaceNormal)
{
m_isGrounded = true;
if (!m_collisions.Contains(collision.collider))
{
m_collisions.Add(collision.collider);
}
} else
{
if (m_collisions.Contains(collision.collider))
{
m_collisions.Remove(collision.collider);
}
if (m_collisions.Count == 0) { m_isGrounded = false; }
}
}
private void OnCollisionExit(Collision collision)
{
if(m_collisions.Contains(collision.collider))
{
m_collisions.Remove(collision.collider);
}
if (m_collisions.Count == 0) { m_isGrounded = false; }
}
void Update () {
m_animator.SetBool("Grounded", m_isGrounded);
switch(m_controlMode)
{
case ControlMode.Direct:
DirectUpdate();
break;
case ControlMode.Tank:
TankUpdate();
break;
default:
Debug.LogError("Unsupported state");
break;
}
m_wasGrounded = m_isGrounded;
}
private void TankUpdate()
{
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
bool walk = Input.GetKey(KeyCode.LeftShift);
if (v < 0) {
if (walk) { v *= m_backwardsWalkScale; }
else { v *= m_backwardRunScale; }
} else if(walk)
{
v *= m_walkScale;
}
m_currentV = Mathf.Lerp(m_currentV, v, Time.deltaTime * m_interpolation);
m_currentH = Mathf.Lerp(m_currentH, h, Time.deltaTime * m_interpolation);
transform.position += transform.forward * m_currentV * m_moveSpeed * Time.deltaTime;
transform.Rotate(0, m_currentH * m_turnSpeed * Time.deltaTime, 0);
m_animator.SetFloat("MoveSpeed", m_currentV);
JumpingAndLanding();
}
private void DirectUpdate()
{
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
Transform camera = Camera.main.transform;
if (Input.GetKey(KeyCode.LeftShift))
{
v *= m_walkScale;
h *= m_walkScale;
}
m_currentV = Mathf.Lerp(m_currentV, v, Time.deltaTime * m_interpolation);
m_currentH = Mathf.Lerp(m_currentH, h, Time.deltaTime * m_interpolation);
Vector3 direction = camera.forward * m_currentV + camera.right * m_currentH;
float directionLength = direction.magnitude;
direction.y = 0;
direction = direction.normalized * directionLength;
if(direction != Vector3.zero)
{
m_currentDirection = Vector3.Slerp(m_currentDirection, direction, Time.deltaTime * m_interpolation);
transform.rotation = Quaternion.LookRotation(m_currentDirection);
transform.position += m_currentDirection * m_moveSpeed * Time.deltaTime;
m_animator.SetFloat("MoveSpeed", direction.magnitude);
}
JumpingAndLanding();
}
private void JumpingAndLanding()
{
bool jumpCooldownOver = (Time.time - m_jumpTimeStamp) >= m_minJumpInterval;
if (jumpCooldownOver && m_isGrounded && Input.GetKey(KeyCode.Space))
{
m_jumpTimeStamp = Time.time;
m_rigidBody.AddForce(Vector3.up * m_jumpForce, ForceMode.Impulse);
}
if (!m_wasGrounded && m_isGrounded)
{
m_animator.SetTrigger("Land");
}
if (!m_isGrounded && m_wasGrounded)
{
m_animator.SetTrigger("Jump");
}
}
}
1) Get a reference to ThePlayer(Any object you want other objects to follow).
2) Find direction of ThePlayer.
3) In update check for DistanceFromTarget
if (DistanceFromTarget <= distanceUwant)
{
setMovingSpeed=0;
playIdelAnimation();
}
else
{
setMovingSpeed= asmuchyouwant;
playWalkAnimation();
}

Categories