Hi there!
I’m making a platformer in C# I know very little about coding, so some explanation or example codes would really be helpful!
When pressing Up or Down, my character is moving a certain amount in the z axis. this is done with this code:
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (keyHasBeenPressed == false)
{
// transform.position = new Vector3(target.position.x, transform.position.y, target.position.z+3f);
// transform.position += Vector3.forward * 3.0f;
rigidbody.velocity = transform.InverseTransformDirection(Vector3.forward) * 30;
keyHasBeenPressed = true;
}
}
if (Input.GetKeyUp(KeyCode.UpArrow))
{
keyHasBeenPressed = false;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
if (keyHasBeenPressed == false)
{
//transform.position += Vector3.back * 3.0f;
rigidbody.velocity = transform.InverseTransformDirection(Vector3.back) * 30;
keyHasBeenPressed = true;
}
}
if (Input.GetKeyUp(KeyCode.DownArrow))
{
keyHasBeenPressed = false;
}
I’m trying the same with an attack function, where the player is supposed to move forward in the x axis. This looks like this:
if (shockwave.Check())
{
attackTime = 0.6f;
anim.SetBool("Attack", true);
Instantiate(shockwavePrefab, new Vector3(target.position.x,transform.position.y,target.position.z), transform.rotation);
StartCoroutine(waitToShockwaveAgain());
Debug.Log("SHOCKWAVE");
}
This is all working really well. The problem though, is that as soon as I turn around and move towards the left, these functions works in reverse. Now pressing Down will send the character up instead of down the z axis and vice verca. The attack function still sends the player to the right.
How do I make these functions register the direction my Player is facing?
Heres’s the full playerscript:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public GameManager manager;
public float moveSpeed;
private bool canMove = true;
public GameObject deathParticles;
public bool usesManager = true;
//ATTACKS
float attackTime = 0.0f;
private bool ReadyToSpecialAttack = true;
private bool ReadyToMelee = true;
//COMBOS
private KeyCombo sprintAttack= new KeyCombo(new string[] {"Powers", "Horizontal","Horizontal"});
private KeyCombo shockwave= new KeyCombo(new string[] {"Powers", "down","Horizontal"});
private float maxSpeed = 5f;
private Vector3 input;
private Vector3 spawn;
private bool keyHasBeenPressed = false;
public AudioClip[] audioClip;
Animator anim;
public GameObject shockwavePrefab;
public GameObject meleeAttack1Prefab;
private Transform target;
void Start () {
target = GameObject.FindGameObjectWithTag("Player").transform;
anim = GetComponent<Animator>();
if (usesManager)
{
manager = manager.GetComponent<GameManager>();
}
spawn = transform.position;
}
void Update ()
{
//Attack funktionen
if (attackTime > 0)
{
attackTime -= Time.deltaTime;
if(attackTime <= 0)
anim.SetBool("Attack", false);
anim.SetBool("Melee1", false);
}
if (canMove) {
anim.SetFloat("speed", Mathf.Abs(Input.GetAxis("Horizontal")));
input = new Vector3 (Input.GetAxisRaw("Horizontal"), 0, 0);
if(rigidbody.velocity.magnitude < maxSpeed)
{
rigidbody.AddForce(input * moveSpeed);
}
if(Input.GetAxisRaw("Horizontal") > 0)
{
transform.eulerAngles = new Vector2 (0, 0);
}
if(Input.GetAxisRaw("Horizontal") < 0)
{
transform.eulerAngles = new Vector2 (0, 180);
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (keyHasBeenPressed == false)
{
// transform.position = new Vector3(target.position.x, transform.position.y, target.position.z+3f);
// transform.position += Vector3.forward * 3.0f;
rigidbody.velocity = transform.InverseTransformDirection(Vector3.forward) * 30;
keyHasBeenPressed = true;
}
}
if (Input.GetKeyUp(KeyCode.UpArrow))
{
keyHasBeenPressed = false;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
if (keyHasBeenPressed == false)
{
//transform.position += Vector3.back * 3.0f;
rigidbody.velocity = transform.InverseTransformDirection(Vector3.back) * 30;
keyHasBeenPressed = true;
}
}
if (Input.GetKeyUp(KeyCode.DownArrow))
{
keyHasBeenPressed = false;
}
/* if (Input.GetButton("Jump"))
{
anim.SetBool("Jump", true);
}
else
{
anim.SetBool("Jump", false);
}
*/
}
//SPECIAL ATTACKS GOES HERE
if(ReadyToSpecialAttack == true)
{
if (sprintAttack.Check())
{
transform.position += new Vector3(transform.position.x,transform.position.y,target.position.z);
Debug.Log("SPRINT ATTACK");
}
if (shockwave.Check())
{
attackTime = 0.6f;
anim.SetBool("Attack", true);
Instantiate(shockwavePrefab, new Vector3(target.position.x,transform.position.y,target.position.z), transform.rotation);
StartCoroutine(waitToShockwaveAgain());
Debug.Log("SHOCKWAVE");
}
/*
if ( //Holdding still by pressing D, then presing an Arrowkey
(Input.GetKey(KeyCode.D) && (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.LeftArrow))) ||
//Holding down an Arrowkey, then pressing D
(Input.GetKeyDown(KeyCode.D) && (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow)))
)
{
attackTime = 0.6f;
anim.SetBool("Attack", true);
Instantiate(shockwavePrefab, new Vector3(target.position.x,transform.position.y,target.position.z), transform.rotation);
StartCoroutine(waitToShockwaveAgain());
}
*/
}
//NORMAL ATTACKS
if(ReadyToMelee == true)
{
if (Input.GetKeyDown(KeyCode.A))
{
attackTime = 1f;
anim.SetBool("Melee1", true);
rigidbody.velocity = transform.InverseTransformDirection(Vector3.right) * 30;
StartCoroutine(waitToMeleeAgain());
}
//MIDLERTIDIG LØSNING FOR AT LØBE OG SLÅ. ANIMATION VIRKER IKKE, LØBE ANIMATION OVERRULER!
if (Input.GetKeyDown(KeyCode.A) && (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow)))
{
attackTime = 1f;
anim.SetBool("Melee1", true);
Instantiate(meleeAttack1Prefab, transform.position+(transform.right*0.8f), transform.rotation);
StartCoroutine(waitToMeleeAgain());
}
}
if(Input.GetKeyDown(KeyCode.D))
{
canMove = ! canMove;
}
if(Input.GetKeyUp(KeyCode.D))
{
canMove = true;
}
//Dø hvis Players transform y er lavere end -2 (altså hvis han falder udover banens kant)
if (transform.position.y < -2)
{
Die();
}
Physics.gravity = Physics.Raycast(transform.position, Vector3.down, .8f) ? Vector3.zero : new Vector3(0,-60f,0);
}
void OnCollisionEnter(Collision other)
{
if(other.collider.tag == "Enemy")
{
Die();
}
}
void OnTriggerEnter(Collider other)
{
if(other.collider.tag == "Enemy")
{
Die();
}
if(other.collider.tag == "Traps" )
{
Die();
}
if(other.collider.tag == "Token")
{
if(usesManager)
{
manager.tokenCount += 1;
}
PlaySound(0);
Destroy(other.gameObject);
}
if(other.collider.tag == "Goal")
{
PlaySound(1);
Time.timeScale = 0f;
manager.CompleteLevel();
}
}
void PlaySound(int clip)
{
audio.clip = audioClip[clip];
audio.Play();
}
void Die()
{
Instantiate(deathParticles, transform.position, Quaternion.Euler(270,0,0));
transform.position = spawn;
PlaySound(2);
}
IEnumerator waitToShockwaveAgain()
{
ReadyToSpecialAttack = false;
yield return new WaitForSeconds(2f);
ReadyToSpecialAttack = true;
}
IEnumerator waitToMeleeAgain()
{
ReadyToMelee = false;
yield return new WaitForSeconds(0.3f);
ReadyToMelee = true;
}
}
Any help is appreciated! Thanks