Hello everyone!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Animator animator;
public GameObject crossHair;
public GameObject ArrowPrefab;
// Update is called once per frame
void Update ()
{
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Magnitude", movement.magnitude);
AimAndShoot();
transform.position = transform.position + movement * Time.deltaTime;
}
private void AimAndShoot()
{
Vector3 aim = new Vector3(Input.GetAxis("AimHorizontal"), Input.GetAxis("AimVertical"), 0.0f);
Vector2 shootingdirection = new Vector2(Input.GetAxis("AimHorizontal"), Input.GetAxis("AimVertical"));
if (aim.magnitude > 0.0f)
{
aim.Normalize();
aim *= 0.4f;
crossHair.transform.localPosition = aim;
crossHair.SetActive(true);
shootingdirection.Normalize();
if (Input.GetButtonDown("Cancel"))
{
GameObject Arrow = Instantiate(ArrowPrefab, transform.localPosition , Quaternion.identity);
Arrow.GetComponent<Rigidbody2D>().velocity = shootingdirection * 3.0f;
Arrow.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingdirection.x, shootingdirection.y) + Mathf.Rad2Deg);
Destroy(Arrow, 1.0f);
}
}
else
{
crossHair.SetActive(false);
}
}
}
So what’s the problem with my code
I want to rotate my arrow where my aim is facing but it doesn’t work:(
I am just new to Unity…
Here is the image: