The question is pretty straight forward, i’m a beginner tho so i’m having infinite trouble.
I want to make my character look to where i’m shooting like in Binding of isaac! where the character can move left facing right while shooting.
This is my actual movement code. I NEED A LIGHT!!!
VIDEO TO HELP: https://www.youtube.com/watch?v=buuw_9KgHSs
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator animator;
public GameObject bulletPreFab;
public float bulletSpeed;
private float lastFire;
public float fireDelay;
Vector2 movement;
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
float shootHor = Input.GetAxisRaw("ShootHorizontal");
float shootVert = Input.GetAxisRaw("ShootVertical");
if((shootHor != 0 || shootVert != 0) && Time.time > lastFire + fireDelay) {
Shoot(shootHor, shootVert);
lastFire = Time.time;
}
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
if(Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1) {
animator.SetFloat("lastMoveX", Input.GetAxisRaw("Horizontal"));
animator.SetFloat("lastMoveY", Input.GetAxisRaw("Vertical"));
}
}
void Shoot(float x, float y) {
GameObject bullet = Instantiate(bulletPreFab, transform.position, transform.rotation) as GameObject;
bullet.AddComponent<Rigidbody2D>().gravityScale = 0;
bullet.GetComponent<Rigidbody2D>().linearVelocity = new Vector3(
(x < 0) ? Mathf.Floor(x) * bulletSpeed : Mathf.Ceil(x) * bulletSpeed,
(y < 0) ? Mathf.Floor(y) * bulletSpeed : Mathf.Ceil(y) * bulletSpeed,
0
);
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}