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.
Related
Newbie to unity and vr here.
I recently got started with Valem's unity tutorial for VR, but I've hit a snag:
In his part 5 video where he explains how to pick up and interact with objects, the code he uses for the gun results in the bullets being very inaccurate when I used it.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.XR;
public class Gun : MonoBehaviour
{
public float speed = 40;
public GameObject bullet;
public Transform barrel;
public AudioSource audioSource;
public AudioClip audioClip;
public void Fire()
{
GameObject spawnedBullet = Instantiate(bullet, barrel.position, barrel.rotation);
spawnedBullet.GetComponent<Rigidbody>().AddForce(transform.forward * speed);// = speed * barrel.forward;
audioSource.PlayOneShot(audioClip);
Destroy(spawnedBullet, 2);
}
}
In his video his gun fires accurately, but for me it randomly shoots in a different direction (usually forward but rarely straight).
Really need help, I've been hitting my head against this one all day.
I tried make a Ragdoll game with my brother but
the problem is that we try make a Ragdoll movement and we can make a movement but
when we try this movement on the Ragdoll It just goes pretty normal. we know we need bones and things like that but we don't know how can we make A script like that. we don't want see tutorials on YouTube we want to make a script that how We want to be.
Anyone have any idea how we can do that?
Oh and I know when I wrote this there were some spelling errors in the above code so sorry for that.
here's our script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
// Start is called before the first frame update
public float force;
public Rigidbody2D body;
public Leg[] legs;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.D))
{
Vector2 vec = Vector2.right * force;
body.AddForce(vec);
foreach (Leg leg in legs)
{
leg.Walk();
}
}
}
}
[System.Serializable]
public class Leg
{
public Rigidbody2D bone;
public float walkRotation;
public float force;
public void Walk()
{
bone.MoveRotation(Mathf.LerpAngle(bone.rotation, walkRotation, force * Time.deltaTime));
}
}
Use animations for different movements it will save you a lot of time. If you want the Rag doll effect enable it after the Player dies. So, in animations you should check blend trees if you want different animations for different speeds or locations etc. All game characters move as one Object and the rest is done in Animations. It is much simpler and efficient.
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;
}
}
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;
}
}
I am trying to change mass of a 3D object programmatically. But the object doesn't get the calculated mass but a 0 value initially. When the prefab of the object is created it gets the calculated mass of the previous object not the current mass. And the scenario repeats for all the prefabs created henceforth. How can I get around this problem? Any help is greatly appreciated.
You're probably missing a get component call:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float mass;
public Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
rb.mass = 0.5f;
}
}
To give your rigidbody a mass can use the following script wise
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Example() {
rigidbody.mass = 0.5F;
}
}
Next time do some more research.
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-mass.html
Google first hit.