- Home /
Vector doesn't get calculated
I'm writing a script for knockback to the player, but for some reason the difference vector dosen't get caculated. This line is the exact same as the enemy knockback which works fine so I don't see the issue. The calculation lines are at 46.
using System.Collections; using System.Collections.Generic; using UnityEngine;
class AttacksEnemyBasic : MonoBehaviour {
//Player Variables
public GameObject Player;
public float PlayerX;
public float PlayerY;
public Vector2 PlayerVec;
//Enemy Variable
public GameObject Enemy;
public float EnemyX;
public float EnemyY;
public Vector2 EnemyVec;
//Weapon Variables
public GameObject EnemyWeapon;
public float EnemyWeaponZ;
public float AttackAngle;
private float EnemySwingTime;
public float EnemySwingTimer;
public float EnemySwingSpeed;
private float EnemyHitCD;
public float EnemyHitCDTime;
public bool SwingLeft;
public bool SwingRight;
//Spatial Difference Variables
public float DiffX;
public float DiffY;
public Vector2 DiffVec;
public float DiffAngle;
public float AttackDistance;
void Update(){
EnemyX = Enemy.transform.position.x;
EnemyY = Enemy.transform.position.y;
EnemyVec = Enemy.transform.position;
PlayerX = Enemy.transform.position.x;
PlayerY = Enemy.transform.position.y;
PlayerVec = Enemy.transform.position;
DiffX = EnemyX - PlayerX; //These are the lines that don't get calculated.
DiffY = EnemyY - PlayerY;
DiffVec = EnemyVec - PlayerVec;
DiffAngle = Vector2.Angle(EnemyVec, PlayerVec);
if(EnemyHitCD > 0) {
EnemyHitCD -= Time.deltaTime;
}
if(EnemyHitCD < 0) {
EnemyHitCD = 0;
}
if(EnemySwingTime > 0) {
EnemySwingTime -= Time.deltaTime;
}
if(EnemySwingTime < 0) {
EnemySwingTime = 0;
}
if(DiffX < AttackDistance && DiffY < AttackDistance){
if(EnemyHitCD == 0) {
Attack();
EnemyHitCD = EnemyHitCDTime;
}
}
}
Answer by Bunny83 · Apr 01, 2018 at 02:20 AM
Check your lines 33 up to 38. You always use the Enemy object while i guess you wanted to use the Player object for the player variables. Why do you actually have seperate X and Y variables for each position? Why don't use use the vector variables you already have?
Apologies about that! I must've forgotten to change the lines after copying and pasting them.
Your answer
Follow this Question
Related Questions
Neural Network type conversion problem 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Create custom constructors for certain Value types/Objects (Vector3, Transform, etc...) 2 Answers
Random.Range issue 1 Answer