so this issue is fixed, the problem was that the U in Update wasnt capitalised
me and a few friends started making a FPS a few weeks ago and I'm now trying to get the AI to just walk towards the player but the enemy just stands still like a random object.
I have tried to rewrite the code, redo the NavMesh, redo the player and enemy components (with help from my brother who is an indie dev), I followed unity's documentation, even tried to do the AI in a different project but it just doesn't work.
I don't get any errors, so I don't know what I'm doing wrong, been at this AI for a day or two now
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerNavMesh : MonoBehaviour{
private NavMeshAgent agent;
private GameObject target;
private void Start()
{
agent = GetComponent<NavMeshAgent>();
}
private void update() {
target = GameObject.FindGameObjectWithTag("Player");
if(target != null)
{
agent.destination = target.transform.position;
}
else
{
Debug.Log("No target found");
}
}
}
Update() should be capitalized. Also, why are you setting target every frame?
Related
I am trying to create an interaction system in Unity using Natty Creations tutorial: https://www.youtube.com/watch?v=gPPGnpV1Y1c&t=866s
Although I am having a bit of trouble as the script cant find the Action that I am trying to reference.
I am still a beginner so sorry if this is a stupid question with a simple fix.
Here is the script that I am stuck with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInteract : MonoBehaviour
{
private Camera cam;
[SerializeField]
private float distance = 3f;
[SerializeField]
private LayerMask mask;
private PlayerUI playerUI;
private InputManager inputManager;
// Start is called before the first frame update
void Start()
{
cam = GetComponent<PlayerLook>().cam;
playerUI = GetComponent<PlayerUI>();
inputManager = GetComponent<InputManager>();
}
// Update is called once per frame
void Update()
{
playerUI.UpdateText(string.Empty);
Ray ray = new Ray(cam.transform.position, cam.transform.forward);
Debug.DrawRay(ray.origin, ray.direction * distance);
RaycastHit hitInfo; // var to store collision info
if (Physics.Raycast(ray, out hitInfo, distance, mask))
{
if (hitInfo.collider.GetComponent<Interactible>() != null)
{
Interactible interactible = hitInfo.collider.GetComponent<Interactible>();
playerUI.UpdateText(interactible.promptMessage);
if (inputManager.OnFoot.Interact.triggered)
{
interactible.BaseInteract();
}
}
}
}
And my Input Actions: Unity Input Actions
Any help would be greatly appreciated!
Thanks!
I had the SAMMMMEEE problem but I know the fix know (Thanks to a friend) all you have to do is to go into your InputManager and turn onFoot to public. In your Player Interact in the very last if function chane the Uppercase Onfoot to Lowercase onFoot. That's what works for me!
When you are in Input Actions you have to press Save Asset at the top of the window.
I don't know the problem but it worked for me when I did change "OnFoot" to "onFoot"(as Naeto3452 said) and if change "private" to "public" in InputManager.cs "private PlayerInput.OnFootActions onFoot;" it might help
I am currently working on the Flooded Grounds Unity Asset, I'm implementing an Alien NPC, with a script that tells it to follow the player.
The problem that I'm encountering is that the Alien turns his back to the player and I can't figure out why. I also tried rotating it by 180° on the Y axis (both in the transform and inside of the script using transform.rotate.y - when using the transform doesn't make any difference while using it in the update function just makes it constantly snap back and forth).
Here's a video of the issue:
https://drive.google.com/file/d/1tC9zJWKXXDuNZNZJbl2htBUgTPSTB4IG/view?usp=sharing
And here's the script that I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyFollow : MonoBehaviour
{
public NavMeshAgent enemy;
public Transform Player;
private Rigidbody alienRb;
private Animator alienAnim;
// Start is called before the first frame update
void Start()
{
alienRb = GetComponent<Rigidbody>();
alienAnim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
enemy.SetDestination(Player.position);
if(enemy.velocity.magnitude > 0)
{
alienAnim.SetTrigger("Walk");
}
}
}
Any help would be very appreciated!
Hello Mazza ^^ First of all, I have to say that your alien monster is really cute :D Anyway, did you try transform.LookAt(Player.transform.position)? Also, transform.rotation is might not be affective, maybe you can try alien.transform.Rotate(0.0f, 90.0f, 0.0f, Space.Self); or transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y - 180f, transform.rotation.z)
I am trying to practice creating a clone of Galaga following the same ruleset as the original. I am currently stuck trying to attempt a limit on the amount of cloned prefabs that can be in the scene at any one time, in the same way that Galaga's projectiles are limited to 2 on screen at any time. I want to make it so the player can shoot up to two projectiles, which destroy after 2 seconds or when they collide (this part is functioning), followed by not being able to shoot if two projectile clones are active and not yet destroyed in the hierarchy (Not working as I can instantiate projectiles over the limit of 2).
I have combed through Google for about 3 hours with no solutions that have worked for me, at least in the ways that I had attempted to implement them.
Thank y'all so much for the help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour
{
public float moveSpeed = 1.0f;
public playerProjectile projectile;
public Transform launchOffset;
public int maxBullets = 0;
private GameObject cloneProjectile;
public Rigidbody2D player;
// Start is called before the first frame update
void Start()
{
player = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
MovePlayer();
PlayerShoot();
}
public void MovePlayer()
{
player.velocity = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")) * moveSpeed;
}
public void PlayerShoot()
{
if (Input.GetKeyDown(KeyCode.Z))
{
var cloneProjectile = Instantiate(projectile, launchOffset.position, launchOffset.rotation);
maxBullets++;
if (maxBullets >= 3)
{
Destroy(cloneProjectile, 0.1f);
maxBullets --;
return;
}
}
}
}
You could change the logic up a bit. An instance of the playerController class is active as long as the game is active, so it will know and retain the value of 'maxBullets' until you die or exit the program.
So instead, every time you click "z", the first thing you should do is run the check. If the current amount of live projectiles equals the maximum, have the logic 'return' and exit out of the method.
I am relatively new to Unity and have a recurring error. I am creating a simple 2D game in which the player can shoot these bullets at enemies, which are prefabs. In Unity itself, everything works fine but when I try to make a build in WebGL to play in my browser, and error keeps popping up saying my memory is out of bounds or index is out of range.
I have tried doing this on chrome, firefox, and explorer but the same thing keeps happening. I have tried searching the internet but have found no answers on how to fix this problem so far. Here is the code for my bullet script, if it helps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WebShooter : MonoBehaviour {
public Transform firePoint;
public GameObject bulletPrefab;
void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
Shoot();
}
}
void Shoot() {
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
private void OnTriggerEnter2D(Collider2D hitInfo) {
Destroy(gameObject);
}
}
https://imgur.com/G9ysLFM
Could it have something to do with the fact that you are not destroying the bullets that don't hit the gameobject? do you have any code to destroy them once they go off the screen?
If(Mathf.Abs(bulletPrefab.position) - Mathf.Abs(firePoint.position) > 10)
{
Destroy(gameObject);
}
I'm just thinking that you might be overloading the memory with lots of undestroyed bullets flying off into infinity
I am very new at C# (around 1 week in), so I have been following a lot of tutorials. I am at the moment trying to edit a piece of code (unity tutorial) to only have the enemy follow the player when in a certain range of the player (10 foot), but unfortunately without changing the code completely, I cannot find a solution. At the moment the enemy will only follow the player when the player is alive (which i also want).
I've tried googling what I want, but I don't want to change the code from what is it too much. I am very new at c# and and currently learning bit by bit, other methods I have found require me changing the code a lot. As far as I understand, the main focus of the code below is for the enemy to be completely controlled by the Nav Mesh Agent, is it possible for me to keep the same technique?
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour
{
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
UnityEngine.AI.NavMeshAgent nav;
void Awake()
{
// references
player = GameObject.FindGameObjectWithTag("Player").transform;
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
void Update()
{
if (enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
nav.SetDestination(player.position);
transform.LookAt(player);
}
else
{
nav.enabled = false;
}
}
}
If possible, I would like to add 2 if functions. 1 being the check if the player is alive, and the other to check if the player is within distance. I don't know much about raycast at the moment, so will this be the next step for me to learn to get this to work how i want?
Thankyou for your time.
Dean.
You can calculate distance by doing:
float distance = Vector3.Distance(player.transform.position,transform.position);
You can do a check if it's no larger than some amount with:
bool playerIsCloseEnough = distance <= amount;
And you can check if the player is alive with:
bool playerIsAlive = playerHealth.currentHealth > 0;
Then, you can do things if they're both true with:
if (playerIsAlive && playerIsCloseEnough)
{
// ...
}
And as you mentioned in the comment, you'll need to set nav.enabled=true; in void Awake or void Start to make sure the nav mesh is turned on :)