Hello so I’m trying to get this throwing axe to be shot from the Firepoint object that is a child of my player. but currently its only shooting right when I press (E) please someone help me!
Here is my PlayerController script and the ThrowingAxe script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private float currentMoveSpeed;
public float diagonalMoveModifier;
private Animator anim;
private bool playerMoving;
private Vector2 lastMove;
private Rigidbody2D myRigidbody;
private static bool playerExists;
private bool attacking;
public float attackTime;
private float attackTimeCounter;
public string startPoint;
public Transform firePoint;
public GameObject throwingAxe;
// Use this for initialization
void Start () {
anim = GetComponent();
myRigidbody = GetComponent();
if(!playerExists)
{
playerExists = true;
DontDestroyOnLoad(transform.gameObject);
} else {
Destroy(gameObject);
}
}
// Update is called once per frame
void Update () {
playerMoving = false;
if (!attacking)
{
if (Input.GetAxisRaw(“Horizontal”) > 0.5f || Input.GetAxisRaw(“Horizontal”) < -0.5)
{
//transform.Translate(new Vector3(Input.GetAxisRaw(“Horizontal”) * moveSpeed * Time.deltaTime, 0f, 0f));
myRigidbody.velocity = new Vector2(Input.GetAxisRaw(“Horizontal”) * currentMoveSpeed, myRigidbody.velocity.y);
playerMoving = true;
lastMove = new Vector2(Input.GetAxisRaw(“Horizontal”), 0f);
}
if (Input.GetAxisRaw(“Vertical”) > 0.5f || Input.GetAxisRaw(“Vertical”) < -0.5)
{
//transform.Translate(new Vector3(0f,Input.GetAxisRaw(“Vertical”) * moveSpeed * Time.deltaTime, 0f));
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw(“Vertical”) * currentMoveSpeed);
playerMoving = true;
lastMove = new Vector2(0f, Input.GetAxisRaw(“Vertical”));
}
if (Input.GetAxisRaw(“Horizontal”) < 0.5f && Input.GetAxisRaw(“Horizontal”) > -0.5f)
{
myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
}
if (Input.GetAxisRaw(“Vertical”) < 0.5f && Input.GetAxisRaw(“Vertical”) > -0.5)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
}
if (Input.GetKeyDown(KeyCode.Q))
{
attackTimeCounter = attackTime;
attacking = true;
myRigidbody.velocity = Vector2.zero;
anim.SetBool(“Attack”, true);
}
if (Mathf.Abs (Input.GetAxisRaw(“Horizontal”)) > 0.5f && Mathf.Abs(Input.GetAxisRaw(“Vertical”)) > 0.5f)
{
currentMoveSpeed = moveSpeed * diagonalMoveModifier;
} else {
currentMoveSpeed = moveSpeed;
if(Input.GetKeyDown(KeyCode.E))
{
Instantiate(throwingAxe, firePoint.position, firePoint.rotation);
}
}
}
if(attackTimeCounter > 0)
{
attackTimeCounter -= Time.deltaTime;
}
if(attackTimeCounter <= 0)
{
attacking = false;
anim.SetBool(“Attack”, false);
}
anim.SetFloat(“MoveX”, Input.GetAxisRaw(“Horizontal”));
anim.SetFloat(“MoveY”, Input.GetAxisRaw(“Vertical”));
anim.SetBool(“PlayerMoving”, playerMoving);
anim.SetFloat(“LastMoveX”, lastMove.x);
anim.SetFloat(“LastMoveY”, lastMove.y);
}
}
And my throwing axe script is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowingAxe : MonoBehaviour {
public float speed;
public PlayerController player;
// Use this for initialization
void Start () {
player = FindObjectOfType();
if (player.transform.localScale.x < 0)
speed = -speed;
}
// Update is called once per frame
void Update () {
GetComponent().velocity = new Vector3(speed, GetComponent().velocity.y);
}
void OnTriggerEnter2D(Collider2D other)
{
Destroy(gameObject);
}
}
Please help, been stumped for days now. even watched videos and nothing has fixed the problem!