How to raycasting to player facing direction? - c#

in my 2D topdown game, I'm trying to cast a ray from my player to the direction he is facing. all going great yet the problem is the moment the player stop moving the raycast stopped(which makes sense I'm using Input.GetAxis which returns value of 0 when stop moving). I've been trying using Transform.right / TransformDirection its just makes the ray to stick at the same position and not where the player facing. is there any way for me to keep the ray cast keep going even if my player is stop moving?
public Collider2D GetBarrleCollider()
{
Physics2D.queriesStartInColliders = false;
RaycastHit2D hit = Physics2D.Raycast(this.transform.position, movement, playerDistance);
if (hit.collider != null && hit.collider.gameObject.CompareTag("Pickup"))
{
Debug.Log("Return NOT null" + " " + hit.collider.tag);
return hit.collider;
}
else
{
Debug.Log("Return null");
return null;
}
}
public void MovementHandler()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
rb2D.MovePosition(rb2D.position + movement * getPlayerMovementSpeed * Time.deltaTime);
}

You would store the direction as a variable. Lets call it lookDir. Then you should check if the movement vector is not zero:
Vector2 lookDir;
public void MovementHandler()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
rb2D.MovePosition(rb2D.position + movement * getPlayerMovementSpeed * Time.deltaTime);
if (movement != Vector2.zero)
{
lookDir = movement;
}
}
Then you could raycast from this vector. This way, you only change it if the player is moving.
Something like this should work:
public void MovementHandler()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
rb2D.MovePosition(rb2D.position + movement * getPlayerMovementSpeed * Time.deltaTime);
if (movement != Vector2.zero)
{
lookDir = movement;
}
}
public Collider2D GetBarrleCollider()
{
Physics2D.queriesStartInColliders = false;
//this line was changed \/
RaycastHit2D hit = Physics2D.Raycast(this.transform.position, lookDir, playerDistance);
//
if (hit.collider != null && hit.collider.gameObject.CompareTag("Pickup"))
{
Debug.Log("Return NOT null" + " " + hit.collider.tag);
return hit.collider;
}
else
{
Debug.Log("Return null");
return null;
}
}
Let me know in the comments if this doesn't work.

Related

If two WASD keys are pressed, the character moves in the direction of the first keypress - but continues in that direction once it has been lifted

I am new to Unity so please be kind
So I want my character to move in the direction of the last key pressed. If I press W (up) and D (right) at the same time, the player will move in the direction of what came first. If I release that key and continue to hold the key that was pressed 2nd, the character doesn't change direction until you release said key and repress. This is ruining the feel of my game and I would like some help fixing this problem:)
Here is my character controller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator anim;
Vector2 movement;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal"); //gets axis as vector2
movement.y = Input.GetAxisRaw("Vertical");
anim.SetFloat("Horizontal", movement.x);
anim.SetFloat("Vertical", movement.y); //sets animation parameters
anim.SetFloat("Speed", movement.sqrMagnitude);
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
//If statement to set the correct idle animation (idle right, left, down) based off last direction.
{
anim.SetFloat("LastHorizontal", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("LastVertical", Input.GetAxisRaw("Vertical"));
}
}
void FixedUpdate()
{
if (Mathf.Abs(movement.x) > Mathf.Abs(movement.y)) //if statement disables diagonal movement
{
movement.y = 0;
}
else
{
movement.x = 0;
}
rb.MovePosition(rb.position + movement.normalized * moveSpeed * Time.fixedDeltaTime); //applies movement to player
}
}
Instead of changing movement only on one axis in yout fixed update, I'd try forcing the direction vector for each key. So your code would look like this:
void FixedUpdate()
{
Vector3 movement = Vector3.zero
if (Input.GetKey(KeyCode.W))
{
movement.y = 1;
}
else if (Input.GetKey(KeyCode.S))
{
movement.y = -1;
}
else if (Input.GetKey(KeyCode.A))
{
movement.x = -1;
}
else if (Input.GetKey(KeyCode.D))
{
movement.x = 1;
}
else
{
movement = Vector3.zero
}
rb.MovePosition(rb.position + movement.normalized * moveSpeed * Time.fixedDeltaTime); //applies movement to player
}
The result of this should be your character not being able to move diagonaly

How to make a wallrun in unity

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.

Face the player towards a pushable when raycast hits with an angle

I'm currently working on a pushing system and I'm having some issues. The player uses a CharacterController which can interact with Rigidbodies in the scene. Currently this works as follows: When a Raycast hits a pushable object in front of the player and presses the push button he will push the object, which all works fine. The issue occurs when the Player is slightly turned away from the pushable and starts pushing. I.e. when the character is off by like 10 degrees, but the ray still hits. The concept I had in mind was calculating the angle of the raycast that hits the pushable cube and rotating the character controller towards the pushable by that angle. The problem is I have no clue how to achieve this. I hope someone can help me with this concept. If anyone has any other concepts or ideas feel free to share them :)
Edit: I've added some scripts
The Character controller part:
if (inputDir != Vector2.zero)
{
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
}
float targetSpeed = ((running) ? runSpeed : movementSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
velocityY += Time.deltaTime * gravity;
Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
The Raycast setup and pushstates as off now:
if (Physics.Raycast(middle, (forward), out hit, distanceForPush))
{
if (Input.GetButton("Push") && hit.collider.tag == "PushableLight")
{
anim.SetBool("isPushing", true);
pushForce = playerPushForceLight;
movementSpeed = pushSpeedLight;
hit.transform.SendMessage("HitByPlayer", null, SendMessageOptions.DontRequireReceiver);
if (controller.velocity == Vector3.zero)
{
anim.SetFloat("pushSpeedAnim", 0f);
}
else
{
anim.SetFloat("pushSpeedAnim", 1f);
}
}
else if (Input.GetButton("Push") && hit.collider.tag == "PushableHeavy")
{
anim.SetBool("isPushing", true);
pushForce = playerPushForceHeavy;
movementSpeed = pushSpeedHeavy;
hit.transform.SendMessage("HitByPlayer", null, SendMessageOptions.DontRequireReceiver);
if (controller.velocity == Vector3.zero)
{
anim.SetFloat("pushSpeedAnim", 0f);
}
else
{
anim.SetFloat("pushSpeedAnim", 1f);
}
}
else
{
anim.SetBool("isPushing", false);
pushForce = 0f;
movementSpeed = walkSpeed;
hit.transform.SendMessage("HitStopped", null, SendMessageOptions.DontRequireReceiver);
}
}
else
{
anim.SetBool("isPushing", false);
}
AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
if (stateInfo.fullPathHash == pushStateHash)
{
turnSmoothTime = maxTurnSmoothTimePushing;
}
else
{
turnSmoothTime = 0.1f;
}
}
I would try to align the player with the hit's collision normal.
transform.rotation = Quaternion.LookRotation(-hit.normal, Vector3.up);
This will probably only work well for boxes, though.

Unity - jerky motion when going down the slope

Im trying to make simple 3rd person character controller using unitys character controller component instead of rigidbody. I have problem when making my character sliding down the slope, the motion is jerky, just as if the character was going down stairs.
I move my character using normal to the ground by reversing its y axis, then i apply some additional gravity and put this vector to charactercontroller.move() function.
Here is some of the code where i apply slide and gravity:
void ProcessMotion(){
MoveVector = transform.TransformDirection (MoveVector);
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize (MoveVector);
ApplySlide ();
MoveVector *= MoveSpeed;
MoveVector = new Vector3 (MoveVector.x, VerticalVel, MoveVector.z);
ApplyGravity ();
TP_Controller.CharacterController.Move (MoveVector*Time.deltaTime);
}
public void Jump(){
if (TP_Controller.CharacterController.isGrounded) {
VerticalVel=jumpSpeed;
}
}
void SnapAlignCharacterWithCamera(){
if (MoveVector.x != 0 || MoveVector.z != 0) {
transform.rotation = Quaternion.Euler(transform.eulerAngles.x, Camera.main.transform.eulerAngles.y, transform.eulerAngles.z);
}
}
void ApplyGravity(){
if (MoveVector.y > -TermVel) {
MoveVector = new Vector3 (MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
}
if (TP_Controller.CharacterController.isGrounded && MoveVector.y < - 1) {
MoveVector = new Vector3 (MoveVector.x, -1, MoveVector.z);
}
}
void ApplySlide(){
if (!TP_Controller.CharacterController.isGrounded) {
return;
}
SlideDirection = Vector3.zero;
RaycastHit hitInfo;
if (Physics.Raycast (transform.position , Vector3.down, out hitInfo)) {
if(hitInfo.normal.y < SlideTreshold){
SlideDirection = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z)*10;
}
}
if (SlideDirection.magnitude < MaxMagnitude) {
MoveVector += SlideDirection;
//Debug.DrawLine (transform.position,transform.position + new Vector3(hitInfo.normal.x*0.5f,-hitInfo.normal.y,hitInfo.normal.z*0.5f), Color.red,1.0f);
}else {
MoveVector = SlideDirection;
}
}
And here are screens with gizmos that show path of the object:
Sliding slowly
Sliding 10xfaster
In advance thanks for your help!
I'm not sure the scale of your objects, but it looks like your raycast is coming from inside of your capsule, and possibly colliding with the bottom. The resulting RaycastHit would report that information, which is a normal that points downward, so when you reverse it, you are heading upwards, creating the sawtooth effect.

Multiple raycast with restrictions

I have this problem which I have been trying to figure out for quite some time already. The code I have below is almost complete. I just need to add this additional feature it should work how I want it to work.
So what I want to implement into the code is the make alertedLock false when !withinRange.
But for some reason no matter how i do it. It doesnt work. Because the problem i have is that, when i implement some kind of code to do that, everything goes back to normal.
Thanks in advance.
Edit
The script should be doing this:
If player !withinRange && !withinAngle of enemy then, enemy.color.blue;
If player is not within Range for enemy to detect and player is not within certain Angle for enemy to detect. It means, player is not detected by enemy. So we use Color.Blue to represent that.
If player !withinRange && withinAngle of enemy then, enemy.color.blue;
If player is not within Range for enemy to detect and player is within Angle for enemy to detect. It means, player is still not detect by enemy. So we use Color.Blue to represent that.
If player withinRange && !withinAngle of enemy then, enemy.color.red;
If player is within Range for enemy to detect and player is not within Angle for enemy to detect player. It means, enemy has detected something within Range but doesnt meet all the requirements, that is, withinAngle = true. So in this case we use Color.Red to represent that.
If player withinRange && withinAngle of enemy then, enemy.color.green;
If player is within Range for enemy to detect and player is within Angle for enemy to detect player. It means, player has successfully killed the the enemy. Therefore, using Color.Green to represent that.
If player withinRange && touchRestrictedRaycast of enemy then, enemy.color.magenta (forever) unless !withinRange && !withinAnge
If player is within Range for enemy to detect and player is not within Angle for enemy to detect player. But however then hits the raycast, touchRestrictedRaycast. This will make a lock. So the player cant kill the enemy from a invalid position. So when this happens, the player can no longer kill the enemy. Unless the player is totally out of Range of the enemies detection.
The problem is 5. I dont know how to code 5.
Edit
When i tried to code it outside the forloop , that is,
if (alertedLock && !withinRange) {
alertedLock = false;
}
Does doesnt solve the problem. It returns the solution to where alertedLock is always false
Even when i try to apply it inside the for loop. Such like,
if(withinRange) {
// Inside
if(alertedLock) {
gameObject.renderer.material.color = Color.magenta;
}
if(!alertedLock) {
if(enemyAngleTLUP || enemyAngleTLLEFT) {
alertedLock = true;
}
if(withinAngle) {
gameObject.renderer.material.color = Color.green;
}
if(!withinAngle) {
gameObject.renderer.material.color = Color.red;
}
}
}
if (!withinRange){
if(alteredLock) {
alertedLock = false;
}
}
There is a problem when i do this, its because once it detects the first raycast detection. It ignores the rest, and so it has this color state problem.
I found out this problem on my earlier questions, here:
https://gamedev.stackexchange.com/questions/90329/raycast-flashing-problem
Edit
alertedLock is just a bool which determines when the player has touched the restricted raycast. Due to the fact the player can only kill the enemy from a certain range and angle (withinRange && withinAngle). This is why we have alertedLock.
However, when alertedLock is true. It can only be turned off when the player isnt within the kill range of the enemy (!withinRange)
using UnityEngine;
using System.Collections;
public class Script_v2 : MonoBehaviour {
// Player Properties
private GameObject player;
public Vector3 playerSize;
private Vector3 playerTransform;
public Vector3 playerTransformTL;
public Vector3 playerTransformTR;
public Vector3 playerTransformBL;
public Vector3 playerTransformBR;
private Vector3 newPlayerTransformTL;
private Vector3 newPlayerTransformTR;
private Vector3[] playerRaycastPoints;
// Enemy Properties
private Vector3 enemyTransformTL;
private Vector3 enemyTransformTR;
private Vector3 enemyTransformBL;
private Vector3 enemyTransformBR;
public float distance;
public Vector3 enemySize;
// Detection Alerts
public bool outOfVision;
public bool alerted;
public bool alertedLock;
public bool withinRange;
public bool withinAngle;
public bool dead;
Ray ray;
RaycastHit hit;
// Use this for initialization
void Start () {
playerRaycastPoints = new Vector3[4];
distance = 3f;
player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update () {
enemyTransformTL = new Vector3 (transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z);
enemyTransformTR = new Vector3 (transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z);
enemyTransform_TL_TR ();
Reference_Player_Transform_Points ();
Player_Transform_Points_Detection ();
Debug.Log (alerted + " " + alertedLock);
}
void OnDrawGizmos() {
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere (new Vector3(transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
//Gizmos.DrawWireSphere (new Vector3(transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
}
public void enemyTransform_TL_TR() {
if (alertedLock && !withinRange) {
alertedLock = false;
}
if (!alertedLock) {
gameObject.renderer.material.color = Color.blue;
}
for (int i = 0; i < playerRaycastPoints.Length; i++) {
double enemyAngleTL = Mathf.Atan2(playerRaycastPoints[i].y - ( transform.position.y + 0.5f ),
playerRaycastPoints[i].x - ( transform.position.x - 0.5f )) * 180f / 3.14159265f;
//Debug.Log (enemyAngleTL);
double enemyAngleTR = Mathf.Atan2 (playerRaycastPoints[i].y - (transform.position.y + 0.5f),
playerRaycastPoints[i].x - (transform.position.x + 0.5f)) * 180f / 3.14159265f;
Vector3 directionTL = (playerRaycastPoints[i] - enemyTransformTL).normalized;
Ray rayTL = new Ray(enemyTransformTL, directionTL);
RaycastHit hitTL;
Vector3 directionTR = (playerRaycastPoints[i] - enemyTransformTR).normalized;
Ray rayTR = new Ray (enemyTransformTR, directionTR);
RaycastHit hitTR;
withinRange = Physics.Raycast (rayTL, out hitTL, distance);
withinAngle = enemyAngleTL > 90 && enemyAngleTL < 180;
RaycastHit hitTPUP;
RaycastHit hitTPLEFT;
bool enemyAngleTLUP = Physics.Raycast(enemyTransformTL, Vector3.up, out hitTPUP, distance);
bool enemyAngleTLLEFT = Physics.Raycast(enemyTransformTL, Vector3.left, out hitTPLEFT, distance);
Debug.DrawRay(enemyTransformTL, Vector3.up * distance);
Debug.DrawRay(enemyTransformTL, Vector3.left * distance);
if(withinRange) {
// Inside
if(alertedLock) {
gameObject.renderer.material.color = Color.magenta;
}
if(!alertedLock) {
if(enemyAngleTLUP || enemyAngleTLLEFT) {
alertedLock = true;
}
if(withinAngle) {
gameObject.renderer.material.color = Color.green;
}
if(!withinAngle) {
gameObject.renderer.material.color = Color.red;
}
}
}
}
}
private void Reference_Player_Transform_Points() {
playerSize = player.transform.localScale;
playerTransformTL = new Vector3(player.transform.position.x - (playerSize.x / 2),
player.transform.position.y + playerSize.y / 2,
player.transform.position.z);
playerTransformTR = new Vector3(player.transform.position.x + (playerSize.x / 2),
player.transform.position.y + playerSize.y / 2,
player.transform.position.z);
playerTransformBL = new Vector3(player.transform.position.x - (playerSize.x / 2),
player.transform.position.y - playerSize.y / 2,
player.transform.position.z);
playerTransformBR = new Vector3(player.transform.position.x + (playerSize.x / 2),
player.transform.position.y - playerSize.y / 2,
player.transform.position.z);
playerRaycastPoints [0] = playerTransformTL;
playerRaycastPoints [1] = playerTransformTR;
playerRaycastPoints [2] = playerTransformBL;
playerRaycastPoints [3] = playerTransformBR;
/*
Debug.Log (playerTransformTL);
Debug.Log (playerTransformTR);
Debug.Log (playerTransformBL);
Debug.Log (playerTransformBR);
*/
}
private void Player_Transform_Points_Detection() {
float eTLpTL = Vector3.Distance (enemyTransformTL, playerTransformTL);
float eTLpTR = Vector3.Distance (enemyTransformTL, playerTransformTR);
float eTLpBL = Vector3.Distance (enemyTransformTL, playerTransformBL);
float eTLpBR = Vector3.Distance (enemyTransformTL, playerTransformBR);
float eTRpTL = Vector3.Distance (enemyTransformTR, playerTransformTL);
float eTRpTR = Vector3.Distance (enemyTransformTR, playerTransformTR);
float eTRpBL = Vector3.Distance (enemyTransformTR, playerTransformBL);
float eTRpBR = Vector3.Distance (enemyTransformTR, playerTransformBR);
float eTLMin = Mathf.Min (eTLpTL, eTLpTR, eTLpBL, eTLpBR);
if (eTLMin == eTLpTL) {
newPlayerTransformTL = playerTransformTL;
// Debug.Log("eTLpTL");
}
else if(eTLMin == eTLpTR) {
newPlayerTransformTL = playerTransformTR;
// Debug.Log("eTLpTR");
}
else if(eTLMin == eTLpBL) {
newPlayerTransformTL = playerTransformBL;
// Debug.Log("eTLpBL");
}
else if(eTLMin == eTLpBR) {
newPlayerTransformTL = playerTransformBR;
// Debug.Log("eTLpBR");
}
float eTRMin = Mathf.Min (eTRpTL, eTRpTR, eTRpBL, eTRpBR);
if(eTRMin == eTRpTL) {
newPlayerTransformTR = playerTransformTL;
// Debug.Log("eTRpTL");
}
else if(eTRMin == eTRpTR) {
newPlayerTransformTR = playerTransformTR;
// Debug.Log("eTRpTR");
}
else if(eTRMin == eTRpBL) {
newPlayerTransformTR = playerTransformBL;
// Debug.Log("eTRpBL");
}
else if(eTRMin == eTRpBR) {
newPlayerTransformTR = playerTransformBR;
// Debug.Log("eTRpBR");
}
}
}

Categories

Resources