How to make the enemy look at the player if the player is not hiding behind a wall? - unity3d

The enemy must look at the player if the player comes into view. Below is the code how I did it. The problem is that the enemy sees the player through the wall. How can I fix the code so that the enemy does not see the player through obstacles?
I came up with this solution - to filter out all hits to the player, and then additionally filter out those that do not pass through the barrier. However, I don't know how to implement it.
RaycastHit[] hitsInfo = Physics.SphereCastAll(head.position, sightDistance, transform.forward, sightDistance);
for (int i = 0; i < hitsInfo.Length; i++)
if (hitsInfo[i].transform.gameObject.tag == "Player") {
transform.LookAt(hitsInfo[i].transform.position);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
Debug.DrawLine(head.position, hitsInfo[i].transform.position, Color.red, 0.05f, true);
break;
}
Below is an illustration: the player is white, the enemy is blue, the red is a raycast visualization.

You can use math to check whether the player in sight or not and than use raycast to filter all obstacles.
Here is an example:
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] private Transform _objectOfInterest;
[SerializeField] private float _sightAngle = 60f;
[SerializeField] private float _maxDistance = 10f;
private void Update()
{
if (InSight(_objectOfInterest))
{
Debug.Log("InSight");
if (BehindObstacle((_objectOfInterest)))
{
Debug.Log("BehindObstacle");
}
}
}
private bool InSight(Transform objectOfInterest)
{
var forwardDirection = transform.forward;
var directionToTarget = objectOfInterest.position - transform.position;
var angle = Mathf.Acos(Vector3.Dot(forwardDirection.normalized, directionToTarget.normalized));
if (angle * 100 <= _sightAngle) return true;
return false;
}
private bool BehindObstacle(Transform objectOfInterest)
{
var direction = objectOfInterest.position - transform.position;
var raycastDistance = direction.magnitude < _maxDistance ? direction.magnitude : _maxDistance;
var ray = new Ray(transform.position, direction.normalized);
var hits = new RaycastHit[16];
var hitsAmount = Physics.RaycastNonAlloc(ray, hits, raycastDistance);
Debug.DrawRay(transform.position, direction.normalized * raycastDistance);
if (hitsAmount == 0) return true;
foreach (var hit in hits)
{
if (hit.transform.TryGetComponent<Player>(out Player player) == false) // Player is empty MonoBehaviour in my case
{
return true;
}
return false;
}
return false;
}
}
All the math are in InSight method. forwardDirection is the direction where an enemy is looking. When we use "targetposition" - "myposition" in directionToTarget we getting vector that pointing from our position to target. Than we use simple function to get the angle at which the player is in relation to the enemy. The function is - angle = Acos(Dot(A, B)) where A and B are normalized vectors.
In BehindObstacle we just making raycast to player position and if there are any obstacle returning true. You can set sight angle and max distance on your opinion.
And don't use this in Update method (i'm using it only for test) or very often, it can cause performance issues.

Related

Integrate RigidBody kinematic After Press GetAxis Unity

I would like to integrate the rigidbody kinematic, when I finish pressing one of the axis, because in my code after my tank moves, it keeps moving for a while as if it were a force. I tried to add it, if it stopped pressing the buttons but it didn't work.
I tried to add them in the update, or in the form of a boolean but I think I have problems to set the logic of where it should be so that it works correctly.
What I would like is for the object's kinematic to activate when the axis is released.
Thank you very much for the help
public class TankController : MonoBehaviour
{
public int m_PlayerNumber = 1;
public float m_Speed = 12f;
public float m_TurnSpeed = 180f;
public AudioSource m_MovementAudio;
public float m_PitchRange = 0.2f;
private string m_MovementAxisName;
private string m_TurnAxisName;
private Rigidbody m_Rigidbody;
private float m_MovementInputValue;
private float m_TurnInputValue;
private float m_OriginalPitch;
private void Awake()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
private void OnEnable()
{
// When the tank is turned on, make sure it's not kinematic.
m_Rigidbody.isKinematic = false;
// Also reset the input values.
m_MovementInputValue = 0f;
m_TurnInputValue = 0f;
}
private void OnDisable()
{
// When the tank is turned off, set it to kinematic so it stops moving.
m_Rigidbody.isKinematic = true;
}
private void Start()
{
// The axes names are based on player number.
m_MovementAxisName = "CarroV" ;
m_TurnAxisName = "CarroH";
// Store the original pitch of the audio source.
}
private void Update()
{
// Store the value of both input axes.
m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
EngineAudio();
}
private void FixedUpdate()
{
// Adjust the rigidbodies position and orientation in FixedUpdate.
/* if (Input.GetKey(KeyCode.Keypad8) || !Input.GetKey(KeyCode.Keypad2) || !Input.GetKey(KeyCode.Keypad4) || !Input.GetKey(KeyCode.Keypad6))
{
m_Rigidbody.isKinematic = true;
}
*/
Move();
Turn();
}
private void Move()
{
//m_Rigidbody.isKinematic = false;
// Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
// Apply this movement to the rigidbody's position.
m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
}
private void Turn()
{
// m_Rigidbody.isKinematic = false;
// Determine the number of degrees to be turned based on the input, speed and time between frames.
float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
// Make this into a rotation in the y axis.
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
// Apply this rotation to the rigidbody's rotation.
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
/* else
{
m_Rigidbody.isKinematic = true;
}*/
}
}
`
When you set isKinematic, you're telling the physics engine to not apply forces to the game object.
However, your object still has velocity and you need to cancel that yourself.
if (Input.GetKey(KeyCode.Keypad8) || !Input.GetKey(KeyCode.Keypad2) || !Input.GetKey(KeyCode.Keypad4) || !Input.GetKey(KeyCode.Keypad6))
{
m_Rigidbody.isKinematic = true;
m_Rigidbody.velocity = Vector3.zero;
}
Additional consideration:
Is there a reason you're using the physics engine? If you want it to use physics then stopping should be either using "drag" in the RigidBody inspector or place a force in the opposite direction:
if (Input.GetKey(KeyCode.Keypad8) || !Input.GetKey(KeyCode.Keypad2) || !Input.GetKey(KeyCode.Keypad4) || !Input.GetKey(KeyCode.Keypad6))
{
m_Rigidbody.AddForce(-m_Rigidbody.velocity * someScalingValue); // where someScalingValue is a float you choose
}
If you're doing physics, you should try and keep isKinematic to false.
Alternatively don't use the physics engine, keep isKinematic set to true and move the transform instead.

Unity2D Enemy RigidBody Sticking To Player

Video example: https://imgur.com/a/d7MRjVG
Hello, my enemy object is getting stuck to my player object and dragged along. Normally, the enemy has a much lower movement speed than my player does. But when stuck to the player, the enemy essentially inherits my player's movement. I should note that this ONLY happens when the enemy is higher than my player ie has a greater Y position
Player is a rigidbody with Kinematic type
Enemy is a rigidbody with Dynamic type (I wanted this so I can push enemies aside)
Enemy is scripted not to attempt movement if too close to the player as well
The enemy has a pretty basic movement script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI_Common : MonoBehaviour
{
public float moveSpeed; // Movement speed
public float contactDamageDistance; // offset for contact damage and when to stop walking
public ContactFilter2D movementFilter; // Filter of object types to collide with
Vector2 proposedMovement; // movement to check for collisions before executing
List<RaycastHit2D> castCollisions = new List<RaycastHit2D>(); // Collision list
float myX; // Used for incrementing movement
float myY; // Used for incrementing movement
GameObject player;
Rigidbody2D rBody;
Animator animator;
SpriteRenderer spriteRenderer;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindWithTag("Player");
rBody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
}
// Once per physics frame
private void FixedUpdate()
{
animator.SetBool("IsWalking", false);
// Attempt to move toward player if far enough away
if (Vector2.Distance(player.transform.position, rBody.transform.position) < contactDamageDistance)
return;
bool moveSuccess = false;
myX = 0;
myY = 0;
if (player.transform.position.x > rBody.position.x)
{
myX = moveSpeed * Time.fixedDeltaTime;
} else if (player.transform.position.x < rBody.position.x)
{
myX = -moveSpeed * Time.fixedDeltaTime;
}
// Flip X if needed
if(player.transform.position.x - rBody.position.x < -contactDamageDistance)
{
spriteRenderer.flipX = true;
}
else if (player.transform.position.x - rBody.position.x < contactDamageDistance)
{
spriteRenderer.flipX = false;
}
if (player.transform.position.y > rBody.position.y)
{
myY = moveSpeed * Time.fixedDeltaTime;
}
else if (player.transform.position.y < rBody.position.y)
{
myY = -moveSpeed * Time.fixedDeltaTime;
}
print($"MyX:{rBody.transform.position.x}, MyY:{rBody.transform.position.y}, PlayerX{player.transform.position.x}, playerY{player.transform.position.y}");
print($"Distance:{Vector2.Distance(player.transform.position, rBody.transform.position)}");
moveSuccess = TryMove(new Vector2(myX, myY));
// If move failed, try X only
if (!moveSuccess && myX != 0)
{
moveSuccess = TryMove(new Vector2(myX, 0));
}
// If move failed, try Y only
if (!moveSuccess && myY != 0)
{
moveSuccess = TryMove(new Vector2(0, myY));
}
animator.SetBool("IsWalking", moveSuccess);
}
private bool TryMove(Vector2 moveDirection)
{
// Check for collisions
int count = rBody.Cast(
moveDirection, // X and Y values -1 to 1
movementFilter, // Specifies which objects are considered for collision
castCollisions, // List of collisions detected by cast
moveSpeed * Time.fixedDeltaTime + contactDamageDistance); // Length of cast equal to movement plus offset
// If no collisions found, move
if (count == 0)
{
rBody.MovePosition(rBody.position + moveDirection);
return true;
}
return false;
}
}

Unity 2D Platformer Jumping Issue

I'm able to get my sprite to jump using a Axis.RawInput. This input also serves as a parameter to trigger the jumping animation when the RawInput is greater than 0. This issue with this is when you release the key, the sprite instantly falls back down. How can I perform a fixed jump when the key is pressed once or held down and then have the sprite fall at a fixed rate while also having the animations trigger?
This is what I have in my PlayerMover script now.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMover : MonoBehaviour
{
public Animator anim;
public Vector3 velocity = Vector3.zero;
public float runSpeed = 0f;
public float jumpSpeed = 0f;
private SpriteRenderer sp;
public float maxJump = 4f;
private void Awake()
{
sp = GetComponent<SpriteRenderer>();
}
void Update()
{
runSpeed = Input.GetAxisRaw("Horizontal");
jumpSpeed = Input.GetAxisRaw("Vertical");
anim.SetFloat("Jump", jumpSpeed);
anim.SetFloat("Speed", Mathf.Abs(runSpeed));
}
private void FixedUpdate()
{
Move(runSpeed, jumpSpeed*4);
}
void Move(float horizontal, float vertical)
{
if(horizontal > 0 || horizontal > 0 && vertical >0)
{
anim.SetBool("Idle", false);
sp.flipX = false;
}
else if(horizontal < 0 || horizontal <0 && vertical >0)
{
anim.SetBool("Idle", false);
sp.flipX = true;
}
else
anim.SetBool("Idle", true);
velocity.x = horizontal;
velocity.y = vertical;
transform.position += velocity * Time.fixedDeltaTime;
}
}
I've read about using something like
if(Input.GetKeyDown(GetKeyCode("Space"){
rigidbody2D.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
}
In order to move but it allows additional jumps whenever Space is pressed.
If you want to use something like:
if(Input.GetKeyDown(GetKeyCode("Space"))){
rigidbody2D.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
}
but need to stop the player from jumping when they're already in the air, just only allow them to do this when they're already on the floor. So:
if(Input.GetKeyDown(GetKeyCode("Space")) && isOnFloor()){
rigidbody2D.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
}
Where isOnFloor() is some function that checks if your character is on the floor. Depending on your game implementation this could be done several ways, but the most common one is to check if the player character is colliding with anything, and if so, if that collision object is below them. This stackexchange thread gives some code samples for how to achieve this.
In order to use this
if (Input.GetKeyDown(GetKeyCode("Space"))
{
rigidbody2D.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
}
you need to check if your player is colliding with the ground, so instead you would have a bool like bool isGrounded to check if you are touching the ground, to do this you can do it in the OnCollisionStay() to confirm it is true when you are colliding with the ground. You can use tags to check if the collider you are colliding with is the ground. Then when you jump, you need to say isGrounded = false; then it will not be true untill you land on the ground again
if (Input.GetKeyDown(GetKeyCode("Space") && isGrounded == true)
{
rigidbody2D.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
isGrounded = false;
}

how to limit and clamp distance between two points in a Line renderer unity2d

I am making a game which let you click on a ball and drag to draw a line renderer with two points and point it to a specific direction and when release I add force to the ball,
for now, I just want to know how can I limit the distance between those two points like give it a radius.
You can simply clamp it using a Mathf.Min.
Since you didn't provide any example code unfortunately here is some example code I made up with a simple plane with a MeshCollider, a child object with the LineRenderer and a camera set to Orthographic. You probably would have to adopt it somehow.
public class Example : MonoBehaviour
{
// adjust in the inspector
public float maxRadius = 2;
private Vector3 startPosition;
[SerializeField] private LineRenderer line;
[SerializeField] private Collider collider;
[SerializeField] private Camera camera;
private void Awake()
{
line.positionCount = 0;
line = GetComponentInChildren<LineRenderer>();
collider = GetComponent<Collider>();
camera = Camera.main;
}
// wherever you dragging starts
private void OnMouseDown()
{
line.positionCount = 2;
startPosition = collider.ClosestPoint(camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z)));
var positions = new[] { startPosition, startPosition };
line.SetPositions(positions);
}
// while dragging
private void OnMouseDrag()
{
var currentPosition = GetComponent<Collider>().ClosestPoint(camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z)));
// get vector between positions
var difference = currentPosition - startPosition;
// normalize to only get a direction with magnitude = 1
var direction = difference.normalized;
// here you "clamp" use the smaller of either
// the max radius or the magnitude of the difference vector
var distance = Mathf.Min(maxRadius, difference.magnitude);
// and finally apply the end position
var endPosition = startPosition + direction * distance;
line.SetPosition(1, endPosition);
}
}
This is how it could look like
I've written the following pseudo code, which may help you
float rang ;
Bool drag=true;
GameObject ball;
OnMouseDrag () {
if(drag) {
//Put your dragging code here
}
if (ball.transform.position>range)
Drag=false;
else Drage=true;
}

Ball falls through platform collider

There are 2 GameObjects; a platform and a ball.
The ball is controlled via a custom controller, and the platform moves via an animation.
Components
platform
+ rigidbody
+ box collider
ball
+ rigidbody
+ sphere collider
When the ball comes in contact with the platform, the ball should stop its current velocity and attain the velocity of the platform it is in contact with. However currently, the ball just falls straight through the platform as if there is no colliders attached.
Code of Player:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
public Text winText;
public float speed;
public Text countText;
public GameObject light;
public GameObject player;
private Rigidbody rb;
private int count;
private int a = 0;
private int b = 0;
private int c = 0;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate()
{
if (player.transform.position.y < -15) {
transform.position = new Vector3(a, b, c);
}
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed );
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
if (other.gameObject.CompareTag ("Check point"))
{
other.gameObject.SetActive (false);
light.gameObject.SetActive (false);
a = 0;
b = -10;
c = 96;
}
}
void SetCountText ()
{
countText.text = "Score: " + count.ToString();
if (count >= 8)
{
winText.text = "You Win!";
}
}
}
You said you are using a custom controller. Please make sure that you are not using Transform() to change the ball's position manually and move your ball as this defies the physics laws in unity. Instead use Rigidbody.MovePosition().
More at Unity docs
Make sure you have all those game objects in same Z axis.
Throw a debug.log() message in your OnCollisionEnter2D() method to see if they are actually colliding.
Could you also check for the type of Colliders you are using.
More Details about Collision in unity :
https://docs.unity3d.com/Manual/CollidersOverview.html
Also if it a custom controller make sure that something is not changing the position of ball to go below the platform.
Fast moving objects need the dynamic ContinuousDynamic or Continuous mode to work reliable
Make sure both rigidbodies are kinetic and the colliders are not triggers. Thats what really worked for me.