I've been looking at this AI Navigation script for three days (I'm a beginner) . Can someone tell me when (m_PlayerNear) is actually set to true. The only thing I can think of is that its set to true when the if condition is not met in Chasing() to set m_PlayerNear to false. I hope that made sense.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AIController : MonoBehaviour
{
public NavMeshAgent navMeshAgent; // Nav mesh agent component
public float startWaitTime = 4; // Wait time of every action
public float timeToRotate = 2; // Wait time when the enemy detect near the player without seeing
public float speedWalk = 6; // Walking speed, speed in the nav mesh agent
public float speedRun = 9; // Running speed
public float viewRadius = 15; // Radius of the enemy view
public float viewAngle = 90; // Angle of the enemy view
public LayerMask playerMask; // To detect the player with the raycast
public LayerMask obstacleMask; // To detect the obstacules with the raycast
public float meshResolution = 1.0f; // How many rays will cast per degree
public int edgeIterations = 4; // Number of iterations to get a better performance of the mesh filter when the raycast hit an obstacule
public float edgeDistance = 0.5f; // Max distance to calcule the a minumun and a maximum raycast when hits something
public Transform[] waypoints; // All the waypoints where the enemy patrols
int m_CurrentWaypointIndex; // Current waypoint where the enemy is going to
Vector3 playerLastPosition = Vector3.zero; // Last position of the player when was near the enemy
Vector3 m_PlayerPosition; // Last position of the player when the player is seen by the enemy
float m_WaitTime; // Variable of the wait time that makes the delay
float m_TimeToRotate; // Variable of the wait time to rotate when the player is near that makes the delay
bool m_playerInRange; // If the player is in range of vision, state of chasing
bool m_PlayerNear; // If the player is near, state of hearing
bool m_IsPatrol; // If the enemy is patrol, state of patroling
bool m_CaughtPlayer; // if the enemy has caught the player
void Start()
{
m_PlayerPosition = Vector3.zero;
m_IsPatrol = true;
m_CaughtPlayer = false;
m_playerInRange = false;
m_PlayerNear = false;
m_WaitTime = startWaitTime; // Set the wait time variable that will change
m_TimeToRotate = timeToRotate;
m_CurrentWaypointIndex = 0; // Set the initial waypoint
navMeshAgent = GetComponent<NavMeshAgent>();
navMeshAgent.isStopped = false;
navMeshAgent.speed = speedWalk; // Set the navemesh speed with the normal speed of the enemy
navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position); // Set the destination to the first waypoint
}
private void Update()
{
EnviromentView(); // Check whether or not the player is in the enemy's field of vision
if (!m_IsPatrol)
{
Chasing();
}
else
{
Patroling();
}
}
private void Chasing()
{
// The enemy is chasing the player
m_PlayerNear = false; // Set false that hte player is near beacause the enemy already sees the player
playerLastPosition = Vector3.zero; // Reset the player near position
if (!m_CaughtPlayer)
{
Move(speedRun);
navMeshAgent.SetDestination(m_PlayerPosition); // set the destination of the enemy to the player location
}
if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance) // Control if the enemy arrive to the player location
{
if (m_WaitTime <= 0 && !m_CaughtPlayer && Vector3.Distance(transform.position, GameObject.FindGameObjectWithTag("Player").transform.position) >= 6f)
{
// Check if the enemy is not near to the player, returns to patrol after the wait time delay
m_IsPatrol = true;
m_PlayerNear = false;
Move(speedWalk);
m_TimeToRotate = timeToRotate;
m_WaitTime = startWaitTime;
navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);
}
else
{
if (Vector3.Distance(transform.position, GameObject.FindGameObjectWithTag("Player").transform.position) >= 2.5f) /////
// Wait if the current position is not the player position
Stop();
m_WaitTime -= Time.deltaTime;
}
}
}
private void Patroling()/////
{
if (m_PlayerNear)
{
// Check if the enemy detect near the player, so the enemy will move to that position
if (m_TimeToRotate <= 0)
{
Move(speedWalk);
LookingPlayer(playerLastPosition);
}
else
{
// The enemy wait for a moment and then go to the last player position
Stop();
m_TimeToRotate -= Time.deltaTime;
}
}
else
{
m_PlayerNear = false; // The player is no near when the enemy is platroling
playerLastPosition = Vector3.zero;
navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position); // Set the enemy destination to the next waypoint
if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
{
// If the enemy arrives to the waypoint position then wait for a moment and go to the next
if (m_WaitTime <= 0)
{
NextPoint();
Move(speedWalk);
m_WaitTime = startWaitTime;
}
else
{
Stop();
m_WaitTime -= Time.deltaTime;
}
}
}
}
private void OnAnimatorMove()
{
}
public void NextPoint()
{
m_CurrentWaypointIndex = (m_CurrentWaypointIndex + 1) % waypoints.Length;
navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);
}
void Stop()
{
navMeshAgent.isStopped = true;
navMeshAgent.speed = 0;
}
void Move(float speed)
{
navMeshAgent.isStopped = false;
navMeshAgent.speed = speed;
}
void CaughtPlayer()
{
m_CaughtPlayer = true;
}
void LookingPlayer(Vector3 player)
{
navMeshAgent.SetDestination(player);
if (Vector3.Distance(transform.position, player) <= 0.3)
{
if (m_WaitTime <= 0)
{
m_PlayerNear = false;
Move(speedWalk);
navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);
m_WaitTime = startWaitTime;
m_TimeToRotate = timeToRotate;
}
else
{
Stop();
m_WaitTime -= Time.deltaTime;
}
}
}
void EnviromentView()
{
Collider[] playerInRange = Physics.OverlapSphere(transform.position, viewRadius, playerMask); // Make an overlap sphere around the enemy to detect the playermask in the view radius
for (int i = 0; i < playerInRange.Length; i++)
{
Transform player = playerInRange[i].transform;
Vector3 dirToPlayer = (player.position - transform.position).normalized;
if (Vector3.Angle(transform.forward, dirToPlayer) < viewAngle / 2)
{
float dstToPlayer = Vector3.Distance(transform.position, player.position); // Distance of the enmy and the player
if (!Physics.Raycast(transform.position, dirToPlayer, dstToPlayer, obstacleMask))
{
m_playerInRange = true; // The player has been seeing by the enemy and then the nemy starts to chasing the player
m_IsPatrol = false; // Change the state to chasing the player
}
else
{
/*
* If the player is behind a obstacle the player position will not be registered
* */
m_playerInRange = false;
}
}
if (Vector3.Distance(transform.position, player.position) > viewRadius)
{
/*
* If the player is further than the view radius, then the enemy will no longer keep the player's current position.
* Or the enemy is a safe zone, the enemy will no chase
* */
m_playerInRange = false; // Change the sate of chasing
}
if (m_playerInRange)
{
/*
* If the enemy no longer sees the player, then the enemy will go to the last position that has been registered
* */
m_PlayerPosition = player.transform.position; // Save the player's current position if the player is in range of vision
}
}
}
}
Related
I`m trying to make a wallrun in unity like the wallrun of the Prince of Persia.
The idea is that when you are touching the wall and you press "left shift" the player would run some seconds without falling.
I would like to add that if you press "enter" the character would jump to the side using the Wall as support for impulse.
You can watch this in the first minutes of the video:
https://www.youtube.com/watch?v=zsnB7HEiLr0
I have made this script for the character controller:
public Transform playerPosition;
//controls the x movement. (right/left)
public float horizontalmove;
//controls the y movement. (forward/back)
public float verticalmove;
//controls the movement direction.
private Vector3 playerInput;
//Here I store my character controller.
public CharacterController player;
//controls the player speed.
public float playerSpeed;
//controls de movement direction according to camera
public Vector3 movePlayer;
//controls the last movement
public Vector3 lastMovePlayer;
public float gravity = 9.8f;
public float fallVelocity;
public float jumpForce = 5.0f;
public float verticalSpeed;
private RaycastHit HitR;
private RaycastHit HitL;
//Here I store the main camera
public Camera mainCamera;
//It stores the camera direction when the player is looking forward.
private Vector3 camForward;
//It stores the camera direction when the player is looking right.
private Vector3 camRight;
//Checks
//The meaning of Caida is fall.
public bool Caida;
//The meaning of salto is jump.
public bool Salto;
public bool Wallrun;
public bool WallrunCount;
// Start is called befoe the first frame update
void Start()
{
//i store the character controler.
player = GetComponent<CharacterController>();
Caida = true;
}
// Update is called once per frame
void Update()
{
if (Wallrun == true)
{
Caida = false;
}
if (Salto == true)
{
fallVelocity -= gravity * Time.deltaTime;
movePlayer = lastMovePlayer;
}
if (Caida == true)
{
fallVelocity -= gravity * Time.deltaTime;
}
if (player.isGrounded && Wallrun == false)
{
Caida = false;
Salto = false;
WallrunCount = false;
//I assign the horizontal move to the w and s keys.
horizontalmove = Input.GetAxis("Horizontal");
//I assign the vertical move to the a and d keys.)
verticalmove = Input.GetAxis("Vertical");
//controls the movement direction
playerInput = new Vector3(horizontalmove, 0, verticalmove);
//limits the player speed. With this method if teh player waalks in diagonal doesn´t
//exceed the speed limit.
playerInput = Vector3.ClampMagnitude(playerInput, 1);
// It calls the function that give the camera look direction.
camDirection();
//Here, It`s calculates the player movement considering the camera point and the movement
//we have assing to teh player earlier
//With this method the player always moves looking to the camera
movePlayer = playerInput.x * camRight + playerInput.z * camForward;
//The movement * the speed we want.
movePlayer = movePlayer * playerSpeed;
//we are going to say to the player where is looking at.
player.transform.LookAt(player.transform.position + movePlayer);
//It gives the gravity to the player.
fallVelocity = -gravity * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space))
{
Salto = true;
fallVelocity = jumpForce;
}
}
else if (!player.isGrounded && Salto == false && Wallrun == false)
{
Caida = true;
}
movePlayer.y = fallVelocity;
//we give the movement to th eplayer.
player.Move(movePlayer * Time.deltaTime);
lastMovePlayer = movePlayer;
}
private void OnTriggerStay(Collider other)
{
if (Input.GetKeyDown(KeyCode.LeftShift) && Wallrun == false && WallrunCount == false)
{
if (Input.GetKey("w"))
{
Wallrun = true;
WallrunCount = true;
fallVelocity = 5f;
movePlayer.y = fallVelocity;
movePlayer.z = movePlayer.z * 1.6f;
if (Physics.Raycast(transform.position, transform.right, out HitR, 1))
{
movePlayer.x = 1.6f;
}
else if (Physics.Raycast(transform.position, -transform.right, out HitL, 1))
{
movePlayer.x = -1.6f;
}
StartCoroutine(wallrunTime());
}
}
}
void camDirection()
{
//we store the forward and right directions here.
camForward = mainCamera.transform.forward;
camRight = mainCamera.transform.right;
//we block the direction and the camera direction because we are not going to use it.
camForward.y = 0;
camRight.y = 0;
//It gives as the normalized vectors.
camForward = camForward.normalized;
camRight = camRight.normalized;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (!player.isGrounded && hit.normal.y < 0.1f)
{
if (Input.GetKeyDown(KeyCode.Space))
{
fallVelocity = jumpForce;
movePlayer = hit.normal * 7;
player.transform.LookAt(player.transform.position + movePlayer);
}
}
}
IEnumerator wallrunTime()
{
yield return new WaitForSeconds(1);
Wallrun = false;
}
As you can see, when the player enters the leftshift it checks to what direction is moving the character, if this is front (w) the script make the z movement * 1.6 (to make the character run a bit in the wall) and the character go a bit up bit the y axis.Then, the script checks if the Wall it i son the right or on the left and, depending on where the wall is, sticks the character to that wall.
private void OnTriggerStay(Collider other)
{
if (Input.GetKeyDown(KeyCode.LeftShift) && Wallrun == false && WallrunCount == false)
{
if (Input.GetKey("w"))
{
Wallrun = true;
WallrunCount = true;
fallVelocity = 5f;
movePlayer.y = fallVelocity;
movePlayer.z = movePlayer.z * 1.6f;
if (Physics.Raycast(transform.position, transform.right, out HitR, 1))
{
movePlayer.x = 1.6f;
}
else if (Physics.Raycast(transform.position, -transform.right, out HitL, 1))
{
movePlayer.x = -1.6f;
}
StartCoroutine(wallrunTime());
}
}
}
With this method, I can make the character jump when the player enters space because the character is hitting the wall (a condition required to bounce in the wall).
And after some seconds, the character falls simulating a wallrun.
IEnumerator wallrunTime()
{
yield return new WaitForSeconds(1);
Wallrun = false;
}
The problem is that this works perfectly if the character makes the wallrun when is looking in the same direction as the axes of the environment.
When the character z axis is looking at the same direction of the environment z axes it works perfectly. But when the axes are not looking at the same direction is a disaster. I show you a video I have recorded.
https://youtu.be/KH7rE9kh5d0
The problem, I suppose, is that with the code I have written, I'm moving the character according to the axes of the environment, and not its axes, so I have to tell him to move according to his own.
My teacher says I might have to change the way I make the character move. I would like to know if you can tell me a way to fix this without changing the movement method or if it is not possible, how can I change that movement.
Thank you in advance.
Hey Guys I've been having a problem lately that I cant seem to solve.
A sprite is supposed to roam around (as it does) while nothing is inside its radius, however if the player moves close to it the sprite should theoretically move towards it and stop roaming.
The sprite doesn't follow the player and cant even see its tag since I cant even see the contents of the "Collider2D[] hits".
using System.Collections.Generic;
using UnityEngine;
public class FireCultist : MonoBehaviour
{
public float moveTimeSeconds; //Time it will take object to move, in seconds.
private float xMax = 10.0f; // The boundaries of the spawn area
private float yMax = 10.0f;
private float xMin = -10.0f; // The boundaries of the spawn area
private float yMin = -10.0f;
public int xDistanceRange; // The max distance you can move at one time
public int yDistanceRange;
private BoxCollider2D boxCollider; //The BoxCollider2D component attached to this object.
private Rigidbody2D rb2D; //The Rigidbody2D component attached to this object.
private float inverseMoveTime; //Used to make movement more efficient.
public Vector2 start;
public Vector2 end;
public bool roam = true;
public Collider2D[] hits;
void Start()
{
boxCollider = GetComponent<BoxCollider2D>();
rb2D = GetComponent<Rigidbody2D>();
inverseMoveTime = 1f / moveTimeSeconds;
InvokeRepeating("RandomMove", 0.0f, 5.0f);
}
void Update()
{
Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, 10); // returns all colliders within radius of enemy
int i = 0;
while(hits.Length > i)
{
Debug.Log("Sees");
Debug.Log(hits[i]);
i++;
}
followPlayer();
if (roam)
{
Debug.Log("Roam");
transform.position = Vector2.MoveTowards(transform.position, end, inverseMoveTime * Time.deltaTime); //Random move to new position
}
}
public void followPlayer()
{
foreach (Collider2D hit in hits)
{
if (hit.tag == "Player") // if the player is within a radius
{
Debug.Log("Chasing Player");
transform.position = Vector2.MoveTowards(transform.position, GameObject.Find("Prefabs/Player").GetComponent<Transform>().position, inverseMoveTime * Time.deltaTime); // chase player
roam = false;
}
else
{
Debug.Log("Continues");
roam = true; // Continue RandomMove()
}
}
}
public void RandomMove() // gets random coordinates near enemy and moves there
{
float xDistance = Random.Range(-xDistanceRange, xDistanceRange); // check
float yDistance = Random.Range(-yDistanceRange, yDistanceRange);// check
if (xDistance < xMax && xDistance > xMin && yDistance < yMax && yDistance > yMin && roam == true) // If within boundaries of walking space
{
start = transform.position;
end = start + new Vector2(xDistance, yDistance);
Debug.Log("Roaming From : " + start + " To : " + end);
}
}
}
The roaming algorithm works however not too sure about the tag detection.
The script belongs to this enemy object
Player Object Properties
It looks like you are declaring a new hits variable during every update instead of using your class-level variable. This means the variable inside of followPlayer() will never be instantiated, and the information will not be passed between the two methods.
Try this:
void Update()
{
hits = Physics2D.OverlapCircleAll(transform.position, 10);
//...
}
You might also consider attaching a trigger collider to the enemy which sends a notification when the player enters/exits the trigger instead of relying on OverlapCircleAll()
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "Player"){
roam = false;
}
}
void OnTriggerExit2D(Collider2D col)
{
if(col.gameObject.tag == "Player"){
roam = true;
}
}
So, when I start the game, my character can jump on the first platform (because that is the manually placed platform), but I cannot jump on the spawned floors. BTW I am able to run on the floors and I know my jump works correctly.
I have tried so many ways of collider detection I am going crazy and I know its a simple fix that I just can't figure out.
I expected my character to be able to jump on the duplicated platforms but the character just doesn't do anything at all.
If anyone is willing to take a look that would be very helpful. - Nick
P.S I know my code is messy.
CODE:
#Code that is on my player script#
using System;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using TouchControlsKit;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Text;
using System.IO;
public class Attack : MonoBehaviour
{
const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
[SerializeField] private LayerMask m_WhatIsGround;
[SerializeField] private Transform m_GroundCheck;
private bool m_Grounded;
public Collider2D objectCollider;
public Collider2D anotherCollider;
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;
private Timer t;
private Timer a;
private float timeStamp;
private float die = 0;
public GameObject bullet;
private bool m_FacingRight = true;
public float move;
private Vector3 velocity = Vector3.zero;
public GameObject idle_0;
public playscript play;
public Transform player;
private Rigidbody2D m_Rigidbody2D;
[SerializeField] private float m_JumpForce = 200f;
bool swing = false;
bool isgrounded = false;
public bool canJump = false;
bool slide = false;
public Transform groundLayer; // Insert the layer here.
public Vector2 jumpHeight;
private Vector2 touchOrigin = -Vector2.one;
public Vector2 moveSpeed;
public bool run;
Collider2D m_Collider;
// variable to hold a reference to our SpriteRenderer component
private SpriteRenderer mySpriteRenderer;
// This function is called just one time by Unity the moment the component loads
private void Awake()
{
// get a reference to the SpriteRenderer component on this gameObject
mySpriteRenderer = GetComponent<SpriteRenderer>();
animator.SetBool("death", false);
}
public Animator animator;
Animator anim;
int swingHash = Animator.StringToHash("swing");
// Use this for initialization
void Start()
{
timeStamp = Time.time + 5;
m_Collider = GetComponent<Collider2D>();
run = false;
m_Rigidbody2D = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
animator.SetBool("isgrounded", false);
isgrounded = false;
canJump = false;
animator.SetBool("swing", false);
}
private void FixedUpdate()
{
m_Grounded = false;
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
// This can be done using layers instead but Sample Assets will not overwrite your project settings.
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
animator.SetBool("isgrounded", true);
m_Grounded = true;
}
}
// Update is called once per frame
void Update()
{
anotherCollider = GameObject.FindGameObjectWithTag("Ground").GetComponent<BoxCollider2D>();
objectCollider = GameObject.FindGameObjectWithTag("Player").GetComponent<CapsuleCollider2D>();
Vector3 targetVelocity = new Vector2(move * 2f, m_Rigidbody2D.velocity.y);
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref velocity, m_MovementSmoothing);
animator.SetBool("run", true);
if (move > 0 && !m_FacingRight)
{
// ... flip the player.
Flip();
}
// Otherwise if the input is moving the player left and the player is facing right...
else if (move < 0 && m_FacingRight)
{
// ... flip the player.
Flip();
}
int horizontal = 0; //Used to store the horizontal move direction.
int vertical = 0; //Used to store the vertical move direction.
#if UNITY_STANDALONE || UNITY_WEBPLAYER
//Check if we are running on iOS, Android, Windows Phone 8 or Unity iPhone
#elif UNITY_IOS || UNITY_ANDROID || UNITY_WP8 || UNITY_IPHONE
//Check if Input has registered more than zero touches
if (Input.touchCount > 0)
{
//Store the first touch detected.
Touch myTouch = Input.touches[0];
//Check if the phase of that touch equals Began
if (myTouch.phase == TouchPhase.Began)
{
//If so, set touchOrigin to the position of that touch
touchOrigin = myTouch.position;
}
//If the touch phase is not Began, and instead is equal to Ended and the x of touchOrigin is greater or equal to zero:
else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
{
//Set touchEnd to equal the position of this touch
Vector2 touchEnd = myTouch.position;
//Calculate the difference between the beginning and end of the touch on the x axis.
float x = touchEnd.x - touchOrigin.x;
//Calculate the difference between the beginning and end of the touch on the y axis.
float y = touchEnd.y - touchOrigin.y;
//Set touchOrigin.x to -1 so that our else if statement will evaluate false and not repeat immediately.
touchOrigin.x = -1;
//Check if the difference along the x axis is greater than the difference along the y axis.
if (Mathf.Abs(x) > Mathf.Abs(y))
//If x is greater than zero, set horizontal to 1, otherwise set it to -1
horizontal = x > 0 ? 1 : -1;
else
//If y is greater than zero, set horizontal to 1, otherwise set it to -1
vertical = y > 0 ? 1 : -1;
}
}
#endif
if (TCKInput.GetAction("jumpBtn", EActionEvent.Up))
{
animator.SetBool("jump", false);
}
if (TCKInput.GetAction("jumpBtn", EActionEvent.Down) && m_Grounded == true)
{
animator.SetBool("jump", true);
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
if (TCKInput.GetAction("fireBtn", EActionEvent.Down))
{
animator.SetBool("swing", true);
m_Collider.enabled = !m_Collider.enabled;
}
if (TCKInput.GetAction("fireBtn", EActionEvent.Up))
{
animator.SetBool("swing", false);
m_Collider.enabled = !m_Collider.enabled;
}
if (TCKInput.GetAction("slideBtn", EActionEvent.Down))
{
if (timeStamp <= Time.time)
{
animator.SetBool("slide", true);
GameObject b = (GameObject)(Instantiate(bullet, transform.position + transform.right * 1.5f, Quaternion.identity));
b.GetComponent<Rigidbody2D>().AddForce(transform.right * 1000);
timeStamp = Time.time + 5;
}
}
if (TCKInput.GetAction("slideBtn", EActionEvent.Up))
{
animator.SetBool("slide", false);
}
if (TCKInput.GetAction("right", EActionEvent.Press))
{
move = -1;
}
if (TCKInput.GetAction("right", EActionEvent.Up))
{
animator.SetBool("run", false);
}
if (TCKInput.GetAction("left", EActionEvent.Press))
{
move = 1;
}
if (TCKInput.GetAction("left", EActionEvent.Up))
{
animator.SetBool("run", false);
}
if (objectCollider.IsTouching(anotherCollider))
{
canJump = true;
}
else
{
canJump = false;
}
}
private void Flip()
{
// Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void Hurt()
{
move = 4;
SceneManager.LoadScene(0);
}
protected void OnCollisionEnter2D(Collision2D collision)
{
EnemyHealth3 enemy = collision.collider.GetComponent<EnemyHealth3>();
if (enemy != null)
{
move = 0;
animator.SetBool("death", true);
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
StartCoroutine(ExecuteAfterTime(.1));
}
}
IEnumerator ExecuteAfterTime(double time)
{
yield return new WaitForSeconds((float)time);
Hurt();
}
}
#Code that is on the floor spawner script#
using UnityEngine;
using System.Collections;
public class Floor_Spawn_Script : MonoBehaviour
{
public GameObject[] obj;
private float oldPosition;
private float currentPosition;
private float ctr = 0;
private float inte = 10.19f;
// Use this for initialization
private void Start()
{
oldPosition = transform.position.x;
AddRoom(ctr * inte);
ctr += 1;
AddRoom(ctr * inte);
ctr += 1;
AddRoom(ctr * inte);
}
// Update is called once per frame
void Update()
{
currentPosition = transform.position.x;
if ((currentPosition - oldPosition) <= 9.595f)
{
AddRoom(ctr * inte);
oldPosition = transform.position.x;
ctr += 1;
}
}
void AddRoom(float roomCenter)
{
GameObject room = (GameObject)Instantiate(obj[Random.Range(0, obj.Length)]);
room.transform.position = new Vector3(roomCenter, 0f, 10f);
}
}```
When the game starts, a random waypoint is selected from an array. The camera should then rotate to face the selected random waypoint and start moving towards it.
Once the camera has reached the waypoint, it should wait 3 seconds before rotating to face and move towards the next random waypoint.
The problem I have is in Start(). The camera does not rotate to face the first waypoint before it starts moving towards it. Instead, it moves towards the first waypoint backwards. Then, when it reaches the waypoint, it waits 3 seconds to rotate and move towards the next waypoint.
It's working fine except that the camera does not rotate to face the first selected random waypoint. It's moving to it backward without first rotating to face it.
Here's my code:
The waypoints script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public GameObject[] waypoints;
public GameObject player;
public float speed = 5;
public float WPradius = 1;
public LookAtCamera lookAtCam;
private int current = 0;
private bool rot = false;
public void Init()
{
waypoints = GameObject.FindGameObjectsWithTag("Target");
if(waypoints.Length > 0)
{
StartCoroutine(RotateFacingTarget(waypoints[UnityEngine.Random.Range(0, waypoints.Length)].transform));
}
}
void Update()
{
if (waypoints.Length > 0)
{
if (Vector3.Distance(waypoints[current].transform.position, transform.position) < WPradius)
{
current = UnityEngine.Random.Range(0, waypoints.Length);
rot = false;
StartCoroutine(RotateFacingTarget(waypoints[current].transform));
if (current >= waypoints.Length)
{
current = 0;
}
}
if (rot)
transform.position = Vector3.MoveTowards(transform.position, waypoints[current].transform.position, Time.deltaTime * speed);
}
}
IEnumerator RotateFacingTarget(Transform target)
{
yield return new WaitForSeconds(3);
lookAtCam.target = target;
rot = true;
}
}
The look at camera script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAtCamera : MonoBehaviour
{
//values that will be set in the Inspector
public Transform target;
public float RotationSpeed;
//values for internal use
private Quaternion _lookRotation;
private Vector3 _direction;
// Update is called once per frame
void Update()
{
//find the vector pointing from our position to the target
if (target)
{
_direction = (target.position - transform.position).normalized;
//create the rotation we need to be in to look at the target
_lookRotation = Quaternion.LookRotation(_direction);
//rotate us over time according to speed until we are in the required rotation
transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
}
}
}
How can I fix this?
Let's assume that Waypoints.Init() is being called and your waypoints variable has an array of 3.
Waypoints.Init() starts a coroutine
Your coroutine waits 3 seconds
After 3 seconds, you set your camera target which Slerps to face the position
Update on its first frame says waypoints.Length > 0 == true
It's not close to its target, and rot is false, so it does not move
Now, you're waiting 3 seconds, not rotating, and not moving.
Your coroutine's 3 second wait time is up and starts the rotation toward your target
rot is now true at the start of your rotation, so your Update method starts moving toward the target as well
It would seem that your logic is off in how the order of operations works. If it needs to act as you describe, I suggest that you operate on the target differently.
I've implemented the following using an enum:
Waypoints
public class Waypoints : MonoBehaviour
{
private GameObject[] waypoints;
private Transform currentWaypoint;
private enum CameraState
{
StartRotating,
Rotating,
Moving,
Waiting
}
private CameraState cameraState;
public GameObject player;
public float speed = 5;
public float WPradius = 1;
public LookAtCamera lookAtCam;
private int current = 0;
private bool isCameraRotating = false;
void Start()
{
cameraState = CameraState.StartRotating;
}
void Update()
{
switch (cameraState)
{
// This state is used as a trigger to set the camera target and start rotation
case CameraState.StartRotating:
{
// Sanity check in case the waypoint array was set to length == 0 between states
if (waypoints.Length == 0)
break;
// Tell the camera to start rotating
currentWaypoint = waypoints[UnityEngine.Random.Range(0, waypoints.Length)].transform;
lookAtCam.target = currentWaypoint;
cameraState = CameraState.Rotating;
break;
}
// This state only needs to detect when the camera has completed rotation to start movement
case CameraState.Rotating:
{
if (lookAtCam.IsFinishedRotating)
cameraState = CameraState.StartMoving;
break;
}
case CameraState.Moving:
{
// Move
transform.position = Vector3.MoveTowards(transform.position, currentWaypoint.position, Time.deltaTime * speed);
// Check for the Waiting state
if (Vector3.Distance(currentWaypoint.position, transform.position) < WPradius)
{
// Set to waiting state
cameraState = CameraState.Waiting;
// Call the coroutine to wait once and not in CameraState.Waiting
// Coroutine will set the next state
StartCoroutine(WaitForTimer(3));
}
break;
}
case CameraState.Waiting:
// Do nothing. Timer has already started
break;
}
}
IEnumerator WaitForTimer(float timer)
{
yield return new WaitForSeconds(timer);
cameraState = CameraState.StartRotating;
}
public void RefreshWaypoints()
{
waypoints = GameObject.FindGameObjectsWithTag("Target");
}
}
LookAtCamera
public class LookAtCamera : MonoBehaviour
{
// Values that will be set in the Inspector
public Transform target;
public float RotationSpeed;
private float timer = 0.0f;
public bool IsRotationFinished
{
get { return timer > 0.99f; }
}
// Update is called once per frame
void Update()
{
if (target != null && timer < 0.99f)
{
// Rotate us over time according to speed until we are in the required rotation
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation((target.position - transform.position).normalized),
timer);
timer += Time.deltaTime * RotationSpeed;
}
}
}
I have a player class in Unity which all works fine except for my ClosestTarget function. The function works how I wanted it to but now it only ever picks Cube 2 (The last element in the list) even if I'm closer to another cube.
The class is as shown:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Player : MonoBehaviour
{
public int health; //Current health
public int stamina; //Current stamina
public int maxHealth = 100; //Constant for max health
public int maxStamina = 500; //Constant for max stamina
protected CharacterController chCtrl; //Reference to the character controller
protected CharacterMotor chMotor; //Reference to the character motor
public float walkSpeed = 3; //Speed at which the player walks
public float runSpeed = 20; //Speed at which the player runs
public bool isWalking = false; //Check for whether the player is walking
public bool isRunning = false; //Check for whether the player is running
public bool isFatigued = false; //Check for whether the player is fatigued
public List<Transform> targets; //Create a list of transforms
public Transform currentTarget;
float distance = Mathf.Infinity;
// Use this for initialization
void Start ()
{
//Get the character controller assigned to the current game object
chCtrl = GetComponent<CharacterController> ();
//Get the character motor assigned to the current game object
chMotor = GetComponent<CharacterMotor> ();
//Set the stamina to the max stamina
stamina = maxStamina;
//Set the health to the max health
health = maxHealth;
//Initialise the targets list
targets = new List<Transform>();
//Call the function to retrieve all buttons in the scene
AddAllButtons();
}
// Update is called once per frame
void Update ()
{
//Call the function to set the speed of the player
SetSpeed ();
ClosestTarget();
}
public void SetSpeed ()
{
//Set the player to walking speed by default
float speed = walkSpeed;
//If the stamina is less than or equal to 0
if (stamina <= 0)
{
//Set the player as fatigued
isFatigued = true;
//Set the player to walking
speed = walkSpeed;
//Set stamina to 0
stamina = 0;
}
//If the stamina is greater than or equal to max stamina
if(stamina >= maxStamina)
{
//Set the stamina to the max stamina
stamina = maxStamina;
//Set the player as not fatigued
isFatigued = false;
}
//If the player is moving along either the x or y axis
if (Input.GetAxis("Horizontal") !=0 || Input.GetAxis("Vertical") !=0)
{
//If the player is fatigued
if(isFatigued == true)
{
//Set the player to walking speed
speed = walkSpeed;
//Player is not running
isRunning = false;
//Player is walking
isWalking = true;
//Start increasing stamina
stamina++;
}
//If the player is touching the ground and the user is either pressing left shift or right shift
else if (chCtrl.isGrounded && Input.GetKey ("left shift") || Input.GetKey ("right shift") && isFatigued == false )
{
//Set the player to running speed
speed = runSpeed;
//Player is running
isRunning = true;
//Player is not walking
isWalking = false;
//Start reducting stamina
stamina--;
}
else
{
//Set the player to walking speed
speed = walkSpeed;
//Player is not running
isRunning = false;
//Player is walking
isWalking = true;
//Start increasing stamina
stamina++;
}
}
else
{
//Player is not running
isRunning = false;
//Player is not walking
isWalking = false;
//Start increasing stamina
stamina++;
}
//Set the players speed to either walkSpeed or runSpeed
chMotor.movement.maxForwardSpeed = speed;
}
void AddAllButtons()
{
//Create an array that contains all game objects tagged with 'button'
GameObject[] buttons = GameObject.FindGameObjectsWithTag("Button");
//For each of the game objects in the array
foreach(GameObject button in buttons)
{
//Add the transform of the button
AddButton(button.transform);
}
}
void AddButton(Transform button)
{
//Add the transform of the button into the targets list
targets.Add(button);
}
void ButtonCheck(Transform button)
{
Vector3 dir = (button.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log(direction);
if(Input.GetKeyDown(KeyCode.E))
if(direction > 0.7F && Vector3.Distance(currentTarget.position, transform.position) < 2.0F)
{
print("Button has been clicked");
}
}
void ClosestTarget()
{
foreach (Transform button in targets)
{
Vector3 diff = (button.position - transform.position);
float curDistance = Vector3.Distance(transform.position, button.position );
Debug.Log(curDistance);
if (curDistance < distance )
{
currentTarget = button;
distance = curDistance;
}
}
}
}
As said, the problem is with the ClosestTargets function. It's finding the distance for every cube but it only ever selects the last element in the targets list.
It still shows the distance from every cube in the console.
My guess: distance is class member and correctly initialised to Mathf.Infinity but it's never reset. So on the first call to ClosestTarget () Cube2 might be the one closest to player as Player.Update might called later. So distance contains a value too tiny (0?) for other objects to have a chance.
I use this extension method to find the closest gameObject (warning it uses linq):
public static GameObject ReturnClosestObject(this GameObject go, float radius, LayerMask layerMask)
{
Collider[] closeObjects = Physics.OverlapSphere(go.transform.position, radius, layerMask);
closeObjects = closeObjects.OrderBy((collider) => Vector3.Distance(collider.gameObject.transform.position, go.transform.position)).ToArray();
GameObject returnedObject = null;
if(closeObjects.FirstOrDefault().Exist())
{
returnedObject = closeObjects.FirstOrDefault().gameObject;
}
return returnedObject;
}
The downside to this method is that it will only detect objects that overlapwith the projected sphere and miss objects that are inside it completly. So I will often compliment it with this recursive function that projects a finit amount of sphere at various distances.
Note while they have similier names, the above is a extension method and below is a instance method. They work together hand and hand.
public GameObject FindClosestGameObject(int startingLookUpDistance, int maxLookUpDistance, int numberOfSteps)
{
GameObject closestGameObject = gameObject.ReturnClosestObject(startingLookUpDistance, LayerMasks.gameObjects);
bool gameObjectNotFound = (closestGameObject.Equals(null));
if(gameObjectNotFound)
{
if(startingLookUpDistance <= maxLookUpDistance)
{
float stepDistance = maxLookUpDistance / numberOfSteps;
return FindClosestBuildingObject((int)(startingLookUpDistance + stepDistance), maxLookUpDistance, numberOfSteps);
}
return null;
}
else
{
return closestGameObject;
}
}
This top method works by specifying your starting distance and your max distance, you then specify how many iterations of the function you want to perform. Based on the number of iterations it will cast a n spheres equal distances apart in between your start and stop bounds.