Camera does not contain a definition for ViewportPointToRay - c#

I'm trying to make a script that can shoot stuff but this line isn't working. I've googled the issue and even unity says that this is correct
using UnityEngine;
using TMPro;
public class ShootGun_Script : MonoBehaviour
{
public GameObject bullet;
public float shootForce, upwardForce;
public float timeBetweenShooting, spread, reloadTime, timeBetweenShots;
public int magazineSize, bulletsPerTap;
public bool allowButtonHold;
int bulletsLeft, bulletsShot;
public Rigidbody playerRb;
public float recoilForce;
bool shooting, readyToShoot, reloading;
public Camera fpsCam;
public Transform attackPoint;
public GameObject muzzleFlash;
public TextMeshProUGUI ammunitionDisplay;
public bool allowInvoke = true;
private void Awake()
{
bulletsLeft = magazineSize;
readyToShoot = true;
}
private void Update()
{
MyInput();
if (ammunitionDisplay != null)
ammunitionDisplay.SetText(bulletsLeft / bulletsPerTap + " / " + magazineSize / bulletsPerTap);
}
private void MyInput()
{
if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
else shooting = Input.GetKeyDown(KeyCode.Mouse0);
if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
if (readyToShoot && shooting && !reloading && bulletsLeft <= 0) Reload();
if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
{
bulletsShot = 0;
Shoot();
}
}
private void Shoot()
{
readyToShoot = false;
** Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));**
RaycastHit hit;
Vector3 targetPoint;
if (Physics.Raycast(ray, out hit))
targetPoint = hit.point;
else
targetPoint = ray.GetPoint(75);
Vector3 directionWithoutSpread = targetPoint - attackPoint.position;
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
Vector3 directionWithSpread = directionWithoutSpread + new Vector3(x, y, 0);
GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity);
currentBullet.transform.forward = directionWithSpread.normalized;
currentBullet.GetComponent<Rigidbody>().AddForce(directionWithSpread.normalized * shootForce, ForceMode.Impulse);
currentBullet.GetComponent<Rigidbody>().AddForce(fpsCam.transform.up * upwardForce, ForceMode.Impulse);
if (muzzleFlash != null)
Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);
bulletsLeft--;
bulletsShot++;
if (allowInvoke)
{
Invoke("ResetShot", timeBetweenShooting);
allowInvoke = false;
playerRb.AddForce(-directionWithSpread.normalized * recoilForce, ForceMode.Impulse);
}
if (bulletsShot < bulletsPerTap && bulletsLeft > 0)
Invoke("Shoot", timeBetweenShots);
}
private void ResetShot()
{
readyToShoot = true;
allowInvoke = true;
}
private void Reload()
{
reloading = true;
Invoke("ReloadFinished", reloadTime);
}
private void ReloadFinished()
{
bulletsLeft = magazineSize;
reloading = false;
}
}

Related

How can i make object to rotate smooth towards a target with raycast?

I have object that move around in fixed radius around another object with random height.
In this script i want the transform(turret) to rotate facing the target(target is the object that move with the random height) and that the laser that shoot will hit all the time the moving around target.
In the original i used the mouse to rotate the transform to be facing objects but now i want that the transform will rotate automatic to the moving target with the laser.
I added this part to the script and the laser start when running the game but the transform is not rotating facing the target. and i still can rotate the transform with the mouse and in this case i don't want the mouse to rotate the transform but that the transform will be rotating automatic.
if (startLaser)
{
Destroy(Instance);
Instance = Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
Instance.transform.parent = transform;
LaserScript = Instance.GetComponent<Hovl_Laser>();
LaserScript2 = Instance.GetComponent<Hovl_Laser2>();
startLaser = false;
}
if (Cam != null)
{
RaycastHit hit;
RayMouse = Cam.ScreenPointToRay(target.position);
if(Physics.Raycast(RayMouse.origin, RayMouse.direction, out hit, MaxLength))
{
RotateToMouseDirection(gameObject, hit.point);
}
else
{
var pos = RayMouse.GetPoint(MaxLength);
RotateToMouseDirection(gameObject, pos);
}
}
but it's not working the transform is not rotating automatic and the mouse still controlling the rotating.
This is the full script :
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System;
using UnityEngine;
public class Hovl_DemoLasers : MonoBehaviour
{
public Transform target;
public GameObject FirePoint;
public Camera Cam;
public float MaxLength;
public GameObject[] Prefabs;
private Ray RayMouse;
private Vector3 direction;
private Quaternion rotation;
[Header("GUI")]
private float windowDpi;
private int Prefab;
private GameObject Instance;
private Hovl_Laser LaserScript;
private Hovl_Laser2 LaserScript2;
private bool rotateMouse = true;
private bool startLaser = true;
//Double-click protection
private float buttonSaver = 0f;
void Start ()
{
if (Screen.dpi < 1) windowDpi = 1;
if (Screen.dpi < 200) windowDpi = 1;
else windowDpi = Screen.dpi / 200f;
Counter(0);
}
void Update()
{
//Enable lazer
if (Input.GetMouseButtonDown(0))
{
Destroy(Instance);
Instance = Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
Instance.transform.parent = transform;
LaserScript = Instance.GetComponent<Hovl_Laser>();
LaserScript2 = Instance.GetComponent<Hovl_Laser2>();
rotateMouse = true;
}
if (Input.GetMouseButtonDown(1))
{
rotateMouse = false;
}
//To change lazers
if ((Input.GetKey(KeyCode.A) || Input.GetAxis("Horizontal") < 0) && buttonSaver >= 0.4f)// left button
{
buttonSaver = 0f;
Counter(-1);
}
if ((Input.GetKey(KeyCode.D) || Input.GetAxis("Horizontal") > 0) && buttonSaver >= 0.4f)// right button
{
buttonSaver = 0f;
Counter(+1);
}
buttonSaver += Time.deltaTime;
if (startLaser)
{
Destroy(Instance);
Instance = Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
Instance.transform.parent = transform;
LaserScript = Instance.GetComponent<Hovl_Laser>();
LaserScript2 = Instance.GetComponent<Hovl_Laser2>();
startLaser = false;
}
if (Cam != null)
{
RaycastHit hit;
RayMouse = Cam.ScreenPointToRay(target.position);
if(Physics.Raycast(RayMouse.origin, RayMouse.direction, out hit, MaxLength))
{
RotateToMouseDirection(gameObject, hit.point);
}
else
{
var pos = RayMouse.GetPoint(MaxLength);
RotateToMouseDirection(gameObject, pos);
}
}
//Current fire point
if (Cam != null && rotateMouse)
{
RaycastHit hit;
var mousePos = Input.mousePosition;
RayMouse = Cam.ScreenPointToRay(mousePos);
if (Physics.Raycast(RayMouse.origin, RayMouse.direction, out hit, MaxLength))
{
RotateToMouseDirection(gameObject, hit.point);
}
else
{
var pos = RayMouse.GetPoint(MaxLength);
RotateToMouseDirection(gameObject, pos);
}
}
else
{
Debug.Log("No camera");
}
}
//GUI Text
void OnGUI()
{
GUI.Label(new Rect(10 * windowDpi, 5 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use the keyboard buttons A/<- and D/-> to change lazers!");
GUI.Label(new Rect(10 * windowDpi, 20 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use left mouse button for shooting!");
}
//To change prefabs (count - prefab number)
void Counter(int count)
{
Prefab += count;
if (Prefab > Prefabs.Length - 1)
{
Prefab = 0;
}
else if (Prefab < 0)
{
Prefab = Prefabs.Length - 1;
}
}
//To rotate fire point
void RotateToMouseDirection (GameObject obj, Vector3 destination)
{
direction = destination - obj.transform.position;
rotation = Quaternion.LookRotation(direction);
obj.transform.localRotation = Quaternion.Lerp(obj.transform.rotation, rotation, 1);
}
}
This is working fine : I'm not sure if i need to use the out hit variable in the Automatic aiming part ? do i need to use the hit variable and if so how ?
// Automatic aiming part
if (Cam != null)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, MaxLength))
{
RotateToMouseDirection(gameObject, target.position);
}
}
Anyway it's not working as i wanted.
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System;
using UnityEngine;
public class Hovl_DemoLasers : MonoBehaviour
{
public Transform target;
public GameObject FirePoint;
public Camera Cam;
public float MaxLength;
public GameObject[] Prefabs;
private Ray RayMouse;
private Ray AutomaticRay;
private Vector3 direction;
private Quaternion rotation;
[Header("GUI")]
private float windowDpi;
private int Prefab;
private GameObject Instance;
private Hovl_Laser LaserScript;
private Hovl_Laser2 LaserScript2;
private bool rotateMouse = true;
private bool startLaser = true;
//Double-click protection
private float buttonSaver = 0f;
void Start ()
{
//LaserEndPoint = new Vector3(0, 0, 0);
if (Screen.dpi < 1) windowDpi = 1;
if (Screen.dpi < 200) windowDpi = 1;
else windowDpi = Screen.dpi / 200f;
Counter(0);
}
void Update()
{
//Enable lazer
if (Input.GetMouseButtonDown(0))
{
Destroy(Instance);
Instance = Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
Instance.transform.parent = transform;
LaserScript = Instance.GetComponent<Hovl_Laser>();
LaserScript2 = Instance.GetComponent<Hovl_Laser2>();
rotateMouse = true;
}
if (Input.GetMouseButtonDown(1))
{
rotateMouse = false;
}
//Disable lazer prefab
if (Input.GetMouseButtonUp(0))
{
/*if (LaserScript) LaserScript.DisablePrepare();
if (LaserScript2) LaserScript2.DisablePrepare();
Destroy(Instance,1);*/
}
//To change lazers
if ((Input.GetKey(KeyCode.A) || Input.GetAxis("Horizontal") < 0) && buttonSaver >= 0.4f)// left button
{
buttonSaver = 0f;
Counter(-1);
}
if ((Input.GetKey(KeyCode.D) || Input.GetAxis("Horizontal") > 0) && buttonSaver >= 0.4f)// right button
{
buttonSaver = 0f;
Counter(+1);
}
buttonSaver += Time.deltaTime;
if (startLaser)
{
rotateMouse = false;
Destroy(Instance);
Instance = Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
Instance.transform.parent = transform;
LaserScript = Instance.GetComponent<Hovl_Laser>();
LaserScript2 = Instance.GetComponent<Hovl_Laser2>();
startLaser = false;
}
// Automatic aiming part
if (Cam != null)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, MaxLength))
{
RotateToMouseDirection(gameObject, target.position);
}
}
//Current fire point
if (Cam != null && rotateMouse)
{
RaycastHit hit; //DELATE THIS IF YOU WANT TO USE LASERS IN 2D
var mousePos = Input.mousePosition;
RayMouse = Cam.ScreenPointToRay(mousePos);
//ADD THIS IF YOU WANT TO USE LASERS IN 2D: RaycastHit2D hit = Physics2D.Raycast(RayMouse.origin, RayMouse.direction, MaxLength);
if (Physics.Raycast(RayMouse.origin, RayMouse.direction, out hit, MaxLength)) //CHANGE THIS IF YOU WANT TO USE LASERRS IN 2D: if (hit.collider != null)
{
RotateToMouseDirection(gameObject, hit.point);
}
else
{
var pos = RayMouse.GetPoint(MaxLength);
RotateToMouseDirection(gameObject, pos);
}
}
else
{
Debug.Log("No camera");
}
}
//GUI Text
void OnGUI()
{
GUI.Label(new Rect(10 * windowDpi, 5 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use the keyboard buttons A/<- and D/-> to change lazers!");
GUI.Label(new Rect(10 * windowDpi, 20 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use left mouse button for shooting!");
}
//To change prefabs (count - prefab number)
void Counter(int count)
{
Prefab += count;
if (Prefab > Prefabs.Length - 1)
{
Prefab = 0;
}
else if (Prefab < 0)
{
Prefab = Prefabs.Length - 1;
}
}
//To rotate fire point
void RotateToMouseDirection (GameObject obj, Vector3 destination)
{
direction = destination - obj.transform.position;
rotation = Quaternion.LookRotation(direction);
obj.transform.localRotation = Quaternion.Lerp(obj.transform.rotation, rotation, 1);
}
}

I have a blank Unity object nested under the player. It's supposed to be tied to the front of the sprite but does not flip with the sprite

I've got my player sprite that has a blank "Front Check" object nested underneath it. The nested item is used to check if the front of the sprite is touching a wall. the problem I'm having is the Front Check object does not flip with the sprite when I move in the opposite direction. it was working fine earlier today and then out of the blue just quit. This is my first unity project and I'm just not quite sure what's missing. Any help is much appreciated. here is an image of the project and my movement script.
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
private BoxCollider2D coll;
private SpriteRenderer sprite;
private Animator anim;
[SerializeField] private LayerMask jumpableGround;
public float checkRadius;
bool isTouchingFront;
public Transform frontCheck;
public float wallSlidingSpeed;
bool wallSliding;
bool wallJumping;
public float xWallForce;
public float yWallForce;
public float wallJumpTime;
private float dirX = 0f;
[SerializeField] private float moveSpeed = 7f;
[SerializeField] private float jumpForce = 14f;
[SerializeField] private float airControl;
private enum MovementState { idle, running, jumping, falling }
[SerializeField] private AudioSource JumpSoundEffect;
// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody2D>();
coll = GetComponent<BoxCollider2D>();
sprite = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
private void Update()
{
dirX = Input.GetAxisRaw("Horizontal");
if (dirX < 0 && IsGrounded())
{
rb.velocity = new Vector2(moveSpeed * -1, rb.velocity.y);
}
else if (dirX > 0 && IsGrounded())
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
}
else if (dirX == 0 && IsGrounded())
{
rb.velocity = new Vector2(0, rb.velocity.y);
}
if (Input.GetButtonDown("Jump") && IsGrounded())
{
JumpSoundEffect.Play();
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
UpdateAnimationState();
}
private void UpdateAnimationState()
{
MovementState state;
if (dirX > 0f && IsGrounded())
{
state = MovementState.running;
}
else if (dirX < 0f && IsGrounded())
{
state = MovementState.running;
}
else
{
state = MovementState.idle;
}
if (rb.velocity.y > .1f)
{
state = MovementState.jumping;
}
else if (rb.velocity.y < -.1f)
{
state = MovementState.falling;
}
//Wall Jump
isTouchingFront = Physics2D.OverlapCircle(frontCheck.position, checkRadius, jumpableGround);
if (isTouchingFront == true && IsGrounded() == false && dirX != 0)
{
wallSliding = true;
}
else
{
wallSliding = false;
}
if (wallSliding)
{
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
if (Input.GetButtonDown("Jump") && wallSliding == true)
{
wallJumping = true;
Invoke("SetWallJumpingToFalse", wallJumpTime);
}
if (wallJumping == true)
{
rb.velocity = new Vector2(xWallForce * -Input.GetAxisRaw("Horizontal"), yWallForce);
}
anim.SetInteger("state", (int)state);
}
void SetWallJumpingToFalse()
{
wallJumping = false;
}
public bool IsGrounded()
{
return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}
public void FixedUpdate()
{
if (dirX > 0f)
{
sprite.flipX = false;
}
else if (dirX < 0f)
{
sprite.flipX = true;
}
}
}
sprite.flipX only flips the rendering not the actual object scaling.
You rather want to use e.g.
var scale = transform.localScale;
scale.x = Mathf.Sign(dirX);
transform.localScale = scale;
Note that it needs to be UnityEngine.Mathf.Sign and not System.Math.Sign since the latter returns 0 for parameter 0 which would totally break your colliders etc. while Mathf.Sign returns only -1 or 1.

Unity2D - Verify if isGround is correctly

I have a script that detect when player is touching a layer named "Ground", and it works pretty well, but my problem is when I use the "Platform Effector 2D" that allows me jump through the plataform. So, when my character is passsing through the plataform, my bool "isGrounded" gets true, but he is actually not in ground, what happens is that the point that I use to check is "colliding" with the object that countains the layer Ground.
I don't think I can explain it very well, so I upload a not listed video on Youtube to show
www.youtube.com/watch?v=EQpF4iSNC0Yt
here's the code
[Header("Components")]
[HideInInspector]public Rigidbody2D _rb;
[Header("Layer Masks")]
[SerializeField] private LayerMask groundLayer;
[Header("Movement Variables")]
[SerializeField] private float _movementAcceleration;
[SerializeField] private float _maxMoveSpeed;
[SerializeField] private float _linearDrag;
[SerializeField] private float dashPower;
private float horizontalDirection;
private bool changingDirection => (_rb.velocity.x > 0f && horizontalDirection < 0f) || (_rb.velocity.x < 0f && horizontalDirection > 0f);
public bool isFalling => _rb.velocity.y < 0.0f;
private bool isRunning => horizontalDirection != 0;
[Header("Jump Variables")]
[SerializeField] private float jumpForce = 12f;
[SerializeField] private float airLinearDrag = 2.5f;
[SerializeField] private float fallMultiplier = 8f;
[SerializeField] private float lowJumpFallMultiplier = 5f;
private bool canJump => Input.GetButtonDown("Jump") && isGround;
[Header("Ground Colission Variables")]
[SerializeField] private float groundRayCastLenght;
private bool isGround;
private bool jumped;
private bool facinRight = true;
private Animator animator;
private SpriteRenderer sprite;
GameController controller;
public Transform groundCheck ;
public bool canPassThrough = false;
void Start()
{
sprite = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
_rb = GetComponent<Rigidbody2D>();
controller = FindObjectOfType<GameController>();
}
// Update is called once per frame
void Update()
{
Debug.Log(_rb.velocity.y);
isGround = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
horizontalDirection = GetInput().x;
if(canJump && !controller.gamePaused){
Jump();
}
if(Input.GetKeyDown(KeyCode.LeftShift)){
ApplyDash();
}
setAnimations();
}
void FixedUpdate()
{
//CheckCollisions();
if(!controller.gamePaused){
//checkDirection();
MoveCharacter();
}
if(isGround){
jumped = false;
ApplyLinearDrag();
}else{
ApplyAirLinearDrag();
FallMultiplier();
}
}
private Vector2 GetInput()
{
return new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
}
private void MoveCharacter(){
if(horizontalDirection < 0 && facinRight || horizontalDirection > 0 && !facinRight){
checkDirection();
}
Vector3 v = _rb.velocity;
_rb.AddForce(new Vector2(horizontalDirection* _movementAcceleration, 0f) );
if(Mathf.Abs(_rb.velocity.x) > _maxMoveSpeed){
v.x = Mathf.Sign(_rb.velocity.x) * _maxMoveSpeed;
_rb.velocity = v;
//_rb.velocity = new Vector2(Mathf.Sign(_rb.velocity.x) * _maxMoveSpeed, _rb.velocity.y );
}
}
private void CheckCollisions(){
isGround = Physics2D.Raycast(transform.position * groundRayCastLenght, Vector2.down, groundRayCastLenght, groundLayer);
}
private void ApplyLinearDrag(){
if(Mathf.Abs(horizontalDirection) < 0.4f || changingDirection){
_rb.drag = _linearDrag;
}else{
_rb.drag = 0f;
}
}
private void ApplyDash(){
_rb.AddForce(new Vector2(horizontalDirection * dashPower, _rb.velocity.y));
}
void ApplyAirLinearDrag(){
_rb.drag = airLinearDrag;
}
void Jump(){
jumped = true;
if(horizontalDirection != 0){
_rb.velocity = new Vector2(_rb.velocity.x, 0f);
_rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}else{
_rb.velocity = new Vector2(_rb.velocity.x, 0f);
_rb.AddForce(Vector2.up * jumpForce * 2f, ForceMode2D.Impulse);
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, transform.position + Vector3.down * groundRayCastLenght);
}
void FallMultiplier(){
if(_rb.velocity.y < 0){
_rb.gravityScale = fallMultiplier;
}else if(_rb.velocity.y > 0 && !Input.GetButtonDown("Jump")){
_rb.gravityScale = lowJumpFallMultiplier;
}else{
_rb.gravityScale = 1f;
}
}
void setAnimations(){
animator.SetBool("Run", isRunning && isGround);
animator.SetBool("isGround", isGround);
animator.SetBool("Jump", jumped);
animator.SetBool("isFalling", isFalling);
animator.SetFloat("eixoY", Mathf.Abs(_rb.velocity.y));
}
void checkDirection(){
facinRight = !facinRight;
transform.localScale = new Vector3( - transform.localScale.x, transform.localScale.y, transform.localScale.z);
}
void OnCollisionEnter2D(Collision2D other)
{
if(LayerMask.LayerToName(other.gameObject.layer) == "Ground") //check layer is ground
{
if (gameObject.GetComponent<Rigidbody2D>().velocity.y <= 0) //check velocity.y is 0
{
Debug.Log("in ground"); //debug console
canPassThrough = false;
}else{
Debug.Log("Not in ground");
canPassThrough = true;
}
}
}
If we want to perform the debug operation when we touch it, we can perform the operation successfully by checking that the velocity.y is 0 at the moment it touches.
like this:
private void OnCollisionEnter2D(Collision2D collision)
{
if(LayerMask.LayerToName(collision.gameObject.layer) == "Ground") //check layer is ground
{
if (gameObject.GetComponent<Rigidbody2D>().velocity.y <= 0) //check velocity.y is 0
{
Debug.Log("in ground"); //debug console
}
}
}

I'm using a VEctor2 to do a dash move but it won'tmove the character

I created a script to make the character dash to the left or to the right but no matter what I do the character won't get pushed horizontally, when I switch it so that the character is pushed upwards the vector does work.
at the vectors there are two different ways of using methods because i tried both to see if either would work but they didn't.
here's the script.
public float speed;
public float jumpForce;
public float inputHorizontal;
public Rigidbody2D rb;
public float checkRadius;
bool isGrounded;
public Transform groundCheck;
public LayerMask whatIsGround;
public float dashSpeed;
public float dashTimeValue;
private float dashTime;
int extraJumps;
public int extraJumpsValue;
private bool facingRight = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
dashTime = dashTimeValue;
}
void Update()
{
if(isGrounded == true)
{
extraJumps = extraJumpsValue;
}
if(Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
{
rb.velocity = new Vector2(0f, jumpForce);
extraJumps = extraJumps - 1;
}
if(Input.GetKeyDown(KeyCode.F))
{
if(dashTime == dashTimeValue && facingRight == true)
{
rb.velocity = new Vector2(dashSpeed, 1f);
Debug.Log("eh");
dashTime -= Time.deltaTime;
}else if(dashTime == dashTimeValue && facingRight == false)
{
rb.velocity = Vector2.left * dashSpeed;
Debug.Log("eh");
dashTime -= Time.deltaTime;
}
dashTime = dashTimeValue;
}
}
void FixedUpdate()
{
inputHorizontal = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(inputHorizontal * speed, rb.velocity.y);
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
if(facingRight == false && inputHorizontal > 0){
Flip();
}else if(facingRight == true && inputHorizontal < 0){
Flip();
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}

Why won't my Skeleton deal damage to my player?

So, every time the skeleton hit the player/character. It won't show the word "HIT!". What did I do wrong?
THE PLAYER IMAGE INSPECTOR
THE SKELETON IMAGE INSPECTOR
THE HIERARCHY IMAGE
SKELETON ENEMY SCRIPT
private Rigidbody2D myBody;
[Header("Movement")]
public float moveSpeed;
private float minX, maxX;
public float distance;
public int direction;
private bool patrol, detect;
private Transform playerPos;
private Animator anim;
[Header("Attack")]
public Transform attackPos;
public float attackRange;
public LayerMask playerLayer;
public int damage;
//sound
VOID AWAKE
void Awake()
{
anim = GetComponent<Animator>();
playerPos = GameObject.Find("George").transform;
myBody = GetComponent<Rigidbody2D>();
}
VOID START
private void Start()
{
maxX = transform.position.x + (distance);
minX = maxX - distance;
//if (Random.value > 0.5) direction = 1;
//else direction = -1;
}
VOID UPDATE
void Update()
{
if (Vector3.Distance(transform.position, playerPos.position) <= 4.0f) patrol = false;
else patrol = true;
}
VOID FIXED UPDATE
private void FixedUpdate()
{
if (anim.GetBool("Death"))
{
myBody.velocity = Vector2.zero;
GetComponent<Collider2D>().enabled = false;
myBody.isKinematic = true;
anim.SetBool("Attack", false);
return;
}
if (myBody.velocity.x > 0)
{
transform.localScale = new Vector2(1f, transform.localScale.y);
anim.SetBool("Attack", false);
}
else if
(myBody.velocity.x < 0) transform.localScale = new Vector2(-1f, transform.localScale.y);
if (patrol)
{
detect = false;
switch (direction)
{
case -1:
if (transform.position.x > minX)
myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
else
direction = 1;
break;
case 1:
if (transform.position.x < maxX)
myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
else
direction = -1;
break;
}
}
else
{
if (Vector2.Distance(playerPos.position, transform.position) >= 1.0f)
{
if (!detect)
{
detect = true;
anim.SetTrigger("Detect");
myBody.velocity = new Vector2(0, myBody.velocity.y);
}
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Detect")) return;
Vector3 playerDir = (playerPos.position - transform.position).normalized;
if (playerDir.x > 0)
myBody.velocity = new Vector2(moveSpeed + 0.4f, myBody.velocity.y);
else
myBody.velocity = new Vector2(-(moveSpeed + 0.4f), myBody.velocity.y);
}
else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
anim.SetBool("Attack", true);
}
}
}
VOID ATTACK
public void Attack()
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
Collider2D attackPlayer = Physics2D.OverlapCircle(attackPos.position, attackRange, playerLayer);
if (attackPlayer == null)
{
if(attackPlayer.tag == "Player")
{
print("Hit!");
attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPos.position, attackRange);
}
PLAYER HEALTH SCRIPT
VOID AWAKE
public int health = 100;
void Awake()
{
}
VOID UPDATE
void Update()
{
if (health < 1)
{
print("Dead");
}
}
VOID TAKE DAMAGE
public void TakeDamage(int damage)
{
FindObjectOfType<CameraShake>().ShakeItMedium();
health -= damage;
}
private void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Fireball")
{
TakeDamage(25);
}
}
You dont seem to call the "Attack" method in your script.
else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
// calling the attack method, so the physics cast is being made
Attack();
anim.SetBool("Attack", true);
}
And dont forget to change this, you are checking if the cast hit nothing instead of something.
// if the cast hit something
if (attackPlayer != null)
{
if(attackPlayer.tag == "Player")
{
print("Hit!");
attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
}
}
While there maybe many other things wrong with that long pile of code:
if (attackPlayer == null)
{
if(attackPlayer.tag == "Player")
{
print("Hit!");
attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
}
}
You check if attackPlayer is null and then try do work on it... Im pretty you sure you meant if (attackPlayer != null)...

Categories

Resources