NavMeshAgent first rotate to target direction and than move to it? - c#

I'm making a small space game in 3D, but it looks awful when my ship is rotating in the middle of movement
So can someone please help me with code ? I need to rotate to that position and than move to it. (And i forgot to say, that rotation needs to be on Y axis)
Here is my code for that movement.
NavMeshAgent agent;
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if(Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.collider.tag == "Ground")
{
agent.SetDestination(hit.point);
}
}
}

You are supposed to stop the agents rotation.
this is the code:
NavMeshAgent agent;
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if(Input.GetMouseButton(0))
{
agent.updateRotation = false;
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.collider.tag == "Ground")
{
agent.SetDestination(hit.point);
}
}
}
And I also have a more efficient way for you to check if the player is on Ground.
Here is what you should do instead of
hit.collider.tag == "Ground
First, make a Layer named "Ground" (anything... depends on you).
Then, add a LayerMask in the script above the NavMeshAgent. public LayerMask groundLayer
And in the if statement
do this:
if (Physics.Raycast(ray, out hit, groundLayer))
{
agent.SetDestination(hit.point);
}
What this does is, it only considers the click on objects with the selected layer.The plus point here is that you can select multiple layers in the Inspector.
And do not forget to select the layer in the Inspector.
Thank you! <3

Related

Raycast goes through walls

so I'm trying to add a script to an enemy AI to detect the player as long as the player isn't obstruted by anything. I'm using a Raycast to achive this but my script isn't working at all. What am I doing wrong?
public Transform playerPos;
bool canSee = false;
private void Update()
{
RaycastHit hit;
var playerTarget = (transform.position - playerPos.position).normalized;
if (Physics.Raycast(transform.position, playerTarget, out hit))
{
canSee = true;
Debug.Log("HIT");
}
else
Debug.Log("NO HIT");
canSee = false;
}
When I hit play in unity Debug.Log reads "HIT" regardless if I'm obscured by a wall or not. Sorry guys, I'm an absolute beginner here so any help is appreciated.

Raycast not detecting colliders

So I'm working on a little top-down perspective game and wanted to add a combat system. I set it up so that it'll shoot a raycast and if it hits an enemy they'll take damage but the raycast is returning nothing.
Vector3 mouse_pos = Input.mousePosition;
mouse_pos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
mouse_pos.x = mouse_pos.x - objectPos.x
mouse_pos.y = mouse_pos.y - objectPos.y
if (Input.GetMouseButtonDown(0))
{
Raycast hit;
print(Physics.Raycast(transform.position, mouse_pos, out hit));
if (Physics.Raycast(transform.position, mouse_pos, out hit))
{
if (hit.collider.gameObject.name.Contains("enemy"))
{
hit.collider.gameObject.GetComponent<enemyMovement>().life -= 1;
}
}
}
The print returns false even when there's a gameobject there. It's probably a simple problem but I don't know what to do lol.
I changed the code for you, because I am not very familiar with the ray, thank you and hope it can help you.
void update(){
if (Input.GetMouseButtonDown(0))
{
// When the left mouse button is pressed, emit a ray to the screen
position where the mouse is located
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray,out hitInfo))
{
// When the ray collides with the object, draw the ray in the scene view
Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
if (hitInfo.collider.gameObject.name.Contains("enemy"))
{
hitInfo.collider.gameObject.GetComponent<enemyMovement>().life -= 1;
}
}
}
}

Detecting touch on gameobject

I want to detect a touch on a GameObject without sucess. My code copied from some examples is:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse Clicked!!");
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 worldPoint2D = new Vector2(worldPoint.x, worldPoint.y);
RaycastHit2D hit = Physics2D.Raycast(worldPoint2D, Vector2.zero);
Debug.Log(hit.collider);
}
}
The output is always null :(
The game object is not moving and is a simple Cube with a box colider.
Try switching your Vector3 worldPoint into Vector2. So like this:
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 worldPoint2D = new Vector2(worldPoint.x, worldPoint.y);
and then use worldPoint2D in the raycast.
Answering myself...I was not be able to resolve the question using Phisycs2D.Raycast method but Phisics.Raycast did the work:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
Debug.Log(hit.collider);
}
}
}

ARKit - How to move object along the recognised surface?

How can I make a 3D object (a car) to move along the surface which was recognized by ARKit (Unity, C#)?
So that instead of finger tap I could use UI to manipulate the car, while the car will use the surface, recognized by ARKit, for movement and collide with it in case of any rigid body effects.
Is it possible to make it also collide with other surfaces (for example the walls)?
Something like this will allow you to tap on your object and drag to move it along a surface. In regards to collisions, etc - make sure your objects have appropriate colliders and rigidbody's. ARKit currently only provides horizontal surfaces, so not sure how to deal with wall collisions...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragAlongSurface : MonoBehaviour {
private bool holding;
void Start () {
holding = false;
}
void Update() {
if (holding) {
Move();
}
// One finger
if (Input.touchCount == 1) {
// Tap on Object
if (Input.GetTouch(0).phase == TouchPhase.Began ) {
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100f)) {
if (hit.transform == transform) {
holding = true;
}
}
}
// Release
if (Input.GetTouch(0).phase == TouchPhase.Ended) {
holding = false;
}
}
}
void Move(){
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
// The GameObject this script attached should be on layer "Surface"
if(Physics.Raycast(ray, out hit, 30.0f, LayerMask.GetMask("Surface"))) {
transform.position = new Vector3(hit.point.x,
transform.position.y,
hit.point.z);
}
}
}

How can I raycast between two moving objects in Unity?

How can I raycast between two moving objects?
I want to raycast from a moving enemy to a moving player. I dont know how to actually code to make the direction work.
using UnityEngine;
using System.Collections;
public class Enemy_Manage_Two : MonoBehaviour {
public GameObject player;
// Use this for initialization
void Start () { }
// Update is called once per frame
void Update () {
player = GameObject.FindGameObjectWithTag ("Player");
//Debug.Log (player.transform.position + " " + transform.position);
Ray ray = new Ray (transform.position, player.transform.position);
RaycastHit hit;
Debug.DrawRay (transform.position, player.transform.position,
Color.red);
if(Physics.Raycast(ray, out hit)) {
gameObject.renderer.material.color = Color.blue;
} else {
gameObject.renderer.material.color = Color.white;
}
}
}
Currently I haven't Unity so treat my answer as proof of concept.
//1. You have to calculate vector between enemy and player:
Vector3 direction=player.transform.position - transform.position;
direction.Normalize(); //Normalize vector
//2. Now you can create Ray
Ray ray=new Ray(transform.position,direction);

Categories

Resources