NavMeshAgent is still moving after stopped in Unity3D - unity3d

I was making a simple 3D project with Unity3D. But when using NavMeshAgent, I got a problem that NavMeshAgent was still moving even though it had been stopped. Why this situation is happening, and how can I get rid of this?
Below is GIF:
And this is a picture of NavMeshAgent of that red cube, if it needs:
This is a source code attached to that:
using UnityEngine;
using UnityEngine.AI;
public class PathFinder : MonoBehaviour {
public GameObject target;
private NavMeshAgent nav;
void Start() {
nav = GetComponent<NavMeshAgent>();
if (target != null) nav.SetDestination(target.transform.position);
nav.isStopped = true;
}
}

Related

Shooting bullets in unity2D

I'm trying to shoot bullets in a 2D top-down-shooter game but they just stay rest and doesnt move
I tried to add "addforce" but they didnt move. I changed it with "new Vector2" but that time it doesnt follow my cursor. istead of it shoots in a different same direction.I want to shoot to my cursor. and if possible I want to add a fire delay between object and make it possible to attack repeatly while holding mouse left button.
`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 15f;
void Update()
{
if (Input.GetButtonDown("Fire1"))
Shoot();
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab,firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.right* bulletForce, ForceMode2D.Impulse);
}
}//class
`

Why my NavMesh agents does not work after build?

My agents do a great job in the editor for avoiding static and dynamic obstacles(carving) and finishing the platform but after I build it and they only stay where they are and they rotate some random locations different for each one.
EDIT: I added a lineRenderer to show the path and it does not show up in the build even though they are created in the editor. Why would they stop path finding?
Here is my OpponentController class that only sets the finishLine as destination on awake:
using UnityEngine;
using UnityEngine.AI;
public class OpponentController : Competitor
{
[SerializeField] private NavMeshAgent agent;
[SerializeField] private Animator anim;
[SerializeField] private float _endPositionX = -23.54f;
void Awake()
{
//agent.speed = Random.Range(1.0f, 2.0f);
_startPosition = transform.position;
Vector3 endPosition = new Vector3(GameManager.instance.finishLine.position.x, 0, Random.Range(_limitZ, _limitZ));
agent.SetDestination(endPosition);
}
}
and Competitor is just a MonoBehaviour that I created as a base class :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Competitor : MonoBehaviour
{
protected Vector3 _endPosition;
protected float _limitZ;
public Vector3 _startPosition;
public void StartOver()
{
transform.position = _startPosition;
}
}
How can I fix this? It was a case study for a job application and I got only one day to make it work.
SOLUTION: I enabled carving for dynamic obstacles and made the NavMesh Surface change every time obstacles move their positions. It is an expensive job to do under low performance. Also, I learned that there is a performance bottleneck between editor and build. So disabling carving for some of the obstacles and trusting on obstacle avoidance of agents solved this problem. This guide helped me solve the situation.

why does my raycast sometimes shoot in the wrong direction?

I'm trying to shoot a raycast from a gameobject at the front of a gun that is being carried by an XR hand. The gun shoots fine most of the time, but sometimes, for some reasons beyond my fragile little minds comprehension, it shoots in the wrong direction. It shoots in random directions, but never more than 90 degrees of where it should be. This is my code. [I have attached a video of it happening.][1] Every time I think I find a pattern, it changes. All I can say is that I have reason to believe it's related to mesh colliders. Only very vague reason though. Edit: https://youtu.be/JxC4wq9nVRw here's an updated video, it shows the problem more clearly. I also changed the redundant if statements.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class GunBasic : MonoBehaviour
{
public Transform BulletShootPoint;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public AudioSource audioSource;
public AudioClip audioClip;
public float Ammo;
public TextMeshPro text;
public void Fire()
{if (Ammo > 0)
{
muzzleFlash.Play();
audioSource.PlayOneShot(audioClip);
RaycastHit hit;
if (Physics.Raycast(BulletShootPoint.transform.position, BulletShootPoint.transform.forward, out hit) && Ammo > 0)
{
Debug.Log(hit.transform.name);
GameObject impactDestroy = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactDestroy, 2f);
}
Ammo -= 1f;
}
}
private void Update()
{
text.text = Ammo.ToString();
}
public void Reload()
{
Ammo = 10;
}
public void OnTriggerEnter(Collider other)
{
if(other.tag == "ReloadTag")
{
Ammo = 10f;
}
}
}
It was a bug in the xr interaction toolkit. It was fixed by an update. Sorry I didn’t try this sooner, thanks for all the help.

Unity: Distance between two game-objects in the scene view

I am new to Unity and as I am creating my interface, I am trying to find what's the distance between two game objects. I know how to do in a C#/javascript script, however I am strugling to find this information in the scene view. Any key I could press while moving an object to see the distance to its neighbours ?
In a script, you can use Vector3.Distance between vector3 to get the distance between two points. Every gameObject has a position that is represented in a Vector3.
Below is a script example that shows you how it works. You just have to drag the script onto a gameObject in your scene and drag in the inspector another gameobject in your scene. The script should run even when your not in play mode because of the [ExecuteInEditMode].This way you will see the distanceBetweenObjects update in real time without actually having to hit play.
using UnityEngine;
[ExecuteInEditMode]
public class DistanceBetweenTwoObjects : MonoBehaviour
{
public GameObject obj;
public float distanceBetweenObjects;
private void Update()
{
distanceBetweenObjects = Vector3.Distance(transform.position, obj.transform.position);
Debug.DrawLine(transform.position, obj.transform.position, Color.green);
}
private void OnDrawGizmos()
{
GUI.color = Color.black;
Handles.Label(transform.position - (transform.position -
obj.transform.position)/2, distanceBetweenObjects.ToString());
}
}
The method OnDrawGizmos will draw text in between the 2 objects showing the distance value to help make it more user friendly.
There isn't any built-in functionality for this, but it's fairly trivial to add such a readout into the display using [ExecuteinEditMode], which causes scripts to run even when the game is not in play mode. This script, for example, should readout the distances on different axes and in total:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class EditorDistanceDisplay : MonoBehaviour
{
public GameObject target1;
public GameObject target2;
public float distanceX;
public float distanceY;
public float distanceZ;
public float distanceTotal;
void Start()
{
}
void Update()
{
Vector3 delta = target2.transform.position - target1.transform.position;
distanceX = delta.x;
distanceY = delta.y;
distanceZ = delta.z;
distanceTotal = delta.magnitude;
}
}

NavMeshAgent in unity not working

I am making a game in unity and it is throwing the error "Failed to create agent because there is no valid NavMesh" and i don't know what the problem is.
using UnityEngine;
using System.Collections;
public class EnemyMotion : MonoBehaviour {
public NavMeshAgent agent;
public Rigidbody rb;
public GameObject otwt;
void Start () {
rb = GetComponent<Rigidbody>();
agent = GetComponent<NavMeshAgent>();
}
void Update () {
gameObject.transform.Rotate(270, 0, 0);
agent.SetDestination(otwt.transform.position);
}
}
Go to Window -> Navigation.
Click on the Bake tab.
Click on Bake in the bottom right corner.
This will bake the NavMesh and your NavMeshAgent will now work.
Right now you have no NavMesh, so your Agents don't know where they can move/walk.