Unity 3D NavMesh Agent when hit breaks - unity3d

When I hit the enemy(which has the navMeshAgent) with my player the enemy is sent to the corner of the map and fidgets. It is sent in the direction that the player pushes it in. I just want it to go to the player for now.
The code
{
GameObject playerPosition;
NavMeshAgent navMesh;
// Start is called before the first frame update
void Start()
{
playerPosition = GameObject.FindGameObjectWithTag("Player");
navMesh = GetComponent<NavMeshAgent>();
if(navMesh == null)
{
Debug.Log("Nav Mesh for enemy brock");
}
}
private void Update()
{
Vector3 dir = playerPosition.transform.position;
navMesh.destination = dir;
}
}
This is what it looks like

Related

Unity - How to jump using a NavMeshAgent and click to move logic

I am building a game where the player can be controlled using the mouse input, using a click to move logic via a navmesh agent.
In order to let the player jump, I started using a CharacterController as well which should help managing the player. My issue is that I can't figure out where to put the jump logic. All references I found are related using the character controller without the navmesh agent.
I can get rid of the CharacterController if needed, but the NavMeshAgent has to stay.
Here it is a working code which allows to walk. Can you please help me with the jumping logic?
private NavMeshAgent _agent;
private CharacterController _characterController;
private Vector3 _desVelocity;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hitInfo))
{
_agent.destination = hitInfo.point;
}
}
var currMovementDirection = _desVelocity.normalized * currentSpeed;
if (_agent.remainingDistance > _agent.stoppingDistance)
{
_desVelocity = _agent.desiredVelocity;
_characterController.Move(currMovementDirection * Time.deltaTime);
}
}
You can achieve this using a Rigidbody instead of a CharacterController. The trick is that you need to disable the NavMeshAgent in order to jump.
Optionally, you set the destination to where you are at the time of the jump, so that the agent doesn't continue the simulation while the jump is happening.
Using collision detection, you turn the NavMeshAgent back on again once you land.
public class PlayerMovement : MonoBehaviour
{
private Camera cam;
private NavMeshAgent agent;
private Rigidbody rigidbody;
public bool grounded = true;
void Start()
{
cam = Camera.main;
agent = GetComponent<NavMeshAgent>();
rigidbody = GetComponent<Rigidbody>();
}
void Update()
{
// clicking on the nav mesh, sets the destination of the agent and off he goes
if (Input.GetMouseButtonDown(0) && (!agent.isStopped))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
agent.SetDestination(hit.point);
}
}
// when you want to jump
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
grounded = false;
if (agent.enabled)
{
// set the agents target to where you are before the jump
// this stops her before she jumps. Alternatively, you could
// cache this value, and set it again once the jump is complete
// to continue the original move
agent.SetDestination(transform.position);
// disable the agent
agent.updatePosition = false;
agent.updateRotation = false;
agent.isStopped = true;
}
// make the jump
rigidbody.isKinematic = false;
rigidbody.useGravity = true;
rigidbody.AddRelativeForce(new Vector3(0, 5f, 0), ForceMode.Impulse);
}
}
/// <summary>
/// Check for collision back to the ground, and re-enable the NavMeshAgent
/// </summary>
private void OnCollisionEnter(Collision collision)
{
if (collision.collider != null && collision.collider.tag == "Ground")
{
if (!grounded)
{
if (agent.enabled)
{
agent.updatePosition = true;
agent.updateRotation = true;
agent.isStopped = false;
}
rigidbody.isKinematic = true;
rigidbody.useGravity = false;
grounded = true;
}
}
}
}
The jump logic should be inside the Update() method since we want the height to be calculated every frame.
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hitInfo))
{
_agent.destination = hitInfo.point;
}
}
var currMovementDirection = _desVelocity.normalized * currentSpeed;
groundedPlayer = _characterController.isGrounded;
if (groundedPlayer && currMovementDirection.y < 0)
{
currMovementDirection.y = 0f;
}
// Changes the height position of the player..
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
currMovementDirection.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
currMovementDirection.y += gravityValue * Time.deltaTime;
if (_agent.remainingDistance > _agent.stoppingDistance)
{
_desVelocity = _agent.desiredVelocity;
_characterController.Move(currMovementDirection * Time.deltaTime);
}
}
Please see the official docs here

How to move gun backward when near to wall in unity?

I am making a FPS game in unity. I have wrote a script for gun to move back ward when it is close to a object, And also for enabling gun scope. I have used animation for gun to move back(gunBackward) and to enable scope(Scoper) and also for gun regular position(idleGun). But now in unity when I play the game I am not able to enable scope when Ray hits a collider, The gun starts vibrating. I am only able to enable scope when I look at the sky box.
public float distance;
public GameObject origin;
public bool scop;
public Animator anim;
public GameObject CrossHair;
public bool scoped;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire2")) {
scoped = true;
scop = false;
}
if (Input.GetButtonUp ("Fire2"))
{
scoped = false;
scop = true;
}
if (scoped == true) {
anim.Play ("Scoper");
}
if (scoped == false)
{
anim.Play("idleGun");
}
Vector3 cartPos = new Vector3 (0.0f, 0.0f, 0.0f);
RaycastHit hit;
if (Physics.Raycast (cartPos+origin.transform.position, transform.TransformDirection (Vector3.forward), out hit))
{
distance = hit.distance;
}
if (hit.transform == null) {
distance = 2.1f;
return;
}
if (distance < 2)
{
anim.Play ("gunBackward");
}
if (distance > 2)
{
anim.Play("idleGun");
}
}
}

Click on active surface to place GameObject with 8th Wall XR?

With 8th Wall XR is it possible to click on a surface and make it the active surface and place a gameobject on the position of the click? Kinda like ARKit which only augments the gameobject after clicking on it.
Something like this should do the trick. You'll need a GameObject (i.e. a Plane) with an XRSurfaceController attached, and put it on a Layer called "Surface":
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaceObject : MonoBehaviour {
// Adjust this if the transform isn't at the bottom edge of the object
public float heightAdjustment = 0.0f;
// Prefab to instantiate. If null, the script will instantiate a Cube
public GameObject prefab;
// Scale factor for instantiated GameObject
public float objectScale = 1.0f;
private GameObject myObj;
void Update() {
// Tap to place
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began ) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
// The "Surface" GameObject with an XRSurfaceController attached should be on layer "Surface"
// If tap hits surface, place object on surface
if(Physics.Raycast(ray, out hit, 100.0f, LayerMask.GetMask("Surface"))) {
CreateObject(new Vector3(hit.point.x, hit.point.y + heightAdjustment, hit.point.z));
}
}
}
void CreateObject(Vector3 v) {
// If prefab is specified, Instantiate() it, otherwise, place a Cube
if (prefab) {
myObj = GameObject.Instantiate(prefab);
} else {
myObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
}
myObj.transform.position = v;
myObj.transform.localScale = new Vector3(objectScale, objectScale, objectScale);
}
}

How to make an enemy stop when he enter the required distance to shoot

The problem that i have is that when the enemy enters the range to shoot it stops and never follows the player again even if the player gets out of the shooting range. To detect if the player entered the shooting range I have made a sphere collider for the enemy.
using UnityEngine;
using System.Collections;
public class EnemyWithRifleMovement : MonoBehaviour {
private GameObject player;
private NavMeshAgent nav;
private bool playerInRange = false;
private Rigidbody rb;
void Awake()
{
rb = GetComponent<Rigidbody>();
player = GameObject.FindGameObjectWithTag("Player").gameObject;
nav = GetComponent<NavMeshAgent>();
}
void Update()
{
if(playerInRange)
{
nav.SetDestination(transform.position);
}
else if(playerInRange == false)
nav.SetDestination(player.transform.position);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
playerInRange = true;
}
}
void OnTriggetExit(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
playerInRange = false;
}
}
}
Your code looks all fine, except there is one typo. Replace OnTriggetExit with OnTriggerExit, and it should work!
Also, in my opinion it is easier to get the distance between the enemy and the player by doing playerInRange = (Vector3.Distance(transform.position, player) <= range.

Stop Player/User from multi jump in my Unity game

Hello and thanks for reading this post.
I'm new to Unity but regardless of this I managed to make a small 2d game. But I ran into a little problem with the jump function.
The player / user shouldn't be able to multi jump in the game.
This is the C# script that controls the player.
using UnityEngine;
using System.Collections;
public class RobotController : MonoBehaviour {
//This will be our maximum speed as we will always be multiplying by 1
public float maxSpeed = 2f;
public GameObject player;
//a boolean value to represent whether we are facing left or not
bool facingLeft = true;
//a value to represent our Animator
Animator anim;
//to check ground and to have a jumpforce we can change in the editor
bool grounded = true;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
// Use this for initialization
void Start () {
//set anim to our animator
anim = GetComponent <Animator>();
}
void FixedUpdate () {
//set our vSpeed
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
//set our grounded bool
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
//set ground in our Animator to match grounded
anim.SetBool ("Ground", grounded);
float move = Input.GetAxis ("Horizontal");//Gives us of one if we are moving via the arrow keys
//move our Players rigidbody
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y);
//set our speed
anim.SetFloat ("Speed",Mathf.Abs (move));
//if we are moving left but not facing left flip, and vice versa
if (move > 0 && !facingLeft) {
Flip ();
} else if (move < 0 && facingLeft) {
Flip ();
}
}
void Update(){
//if we are on the ground and the space bar was pressed, change our ground state and add an upward force
if(grounded && Input.GetKeyDown (KeyCode.UpArrow)){
anim.SetBool("Ground",false);
rigidbody2D.AddForce (new Vector2(0,jumpForce));
}
}
//flip if needed
void Flip(){
facingLeft = !facingLeft;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
And here is the Player object and the GroundCheck Object.
How can I stop the player from being able to multijump. So if he press the upArrow key, He will jump and not be able to jump again before he lands.
Thanks for your time and help
Update
If its hard to see the Images here are the Image on Imgur:
http://imgur.com/GKf4bgi,2i7A0AU#0
You can add another Collider to your player GameObject and make it a trigger with Is Trigger option. Use this code to change a flag variable telling if player is on ground:
private bool isOnGround = false;
void OnCollisionEnter2D(Collision2D collision) {
isOnGround = true;
}
void OnCollisionExit2D(Collision2D collision) {
isOnGround = false;
}
Then you can allow jumping only when isOnGround is true.
You could make little game object called groundController, place it under player.
You're setting there bool value for grounded and in code your checking if your controller overlaps with ground.
watch it here for more info: http://youtu.be/Xnyb2f6Qqzg?t=45m22s