hi all im trying to add fire stick to my shooter..
for the movement im ok, i just need help for shooting.. it shoot in the direction with the stick with Time.deltaTime, but the player dont get the initial position when i go in the scene, he get stucked.. without Time.deltaTime the character dont aim with the stick but he get in the scene where i want it to be.. i dont know what to do, im new to this, please help..
im using a 2 stick left and right.. left for movement and right for aiming and shoot
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput; using CodeMonkey.Utils;
public class PlayerAimWeapon : MonoBehaviour {
public event EventHandler<OnShootEventArgs> OnShoot;
public class OnShootEventArgs : EventArgs {
public Vector3 gunEndPointPosition;
public Vector3 shootPosition;
public Vector3 shellPosition;
}
private Transform aimTransform;
private Transform aimGunEndPointTransform;
private Transform aimShellPositionTransform;
private Animator aimAnimator;
private void Awake() {
aimTransform = transform.Find("AK47");
aimAnimator = aimTransform.GetComponent<Animator>();
aimGunEndPointTransform = aimTransform.Find("GunEndPointPosition");
aimShellPositionTransform = aimTransform.Find("ShellPosition");
}
private void Update() {
HandleAiming();
HandleShooting();
}
private void HandleAiming() {
Vector3 LookDir = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal2"),CrossPlatformInputManager.GetAxis("Vertical2"));
transform.position = transform.position * Time.deltaTime;
}
Vector3 aimDirection = (mousePosition - aimTransform.position).normalized;
float angle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg;
aimTransform.eulerAngles = new Vector3(0, 0, angle);
Vector3 aimLocalScale = Vector3.one;
if (angle > 90 || angle < -90) {
aimLocalScale.y = -1f;
} else {
aimLocalScale.y = +1f;
}
aimTransform.localScale = aimLocalScale;
}
private void HandleShooting() {
if (Input.GetMouseButton(0)) {
Vector3 mousePosition = UtilsClass.GetMouseWorldPosition();
aimAnimator.SetTrigger("Shoot");
OnShoot?.Invoke(this, new OnShootEventArgs {
gunEndPointPosition = aimGunEndPointTransform.position,
shootPosition = mousePosition,
shellPosition = aimShellPositionTransform.position,
});
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612221811im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Android Controlls with CrossPlatformInput 1 Answer
how can i use the mobile input scripts? 0 Answers
Gamepad joystick rotation help needed, player turning strangely. 0 Answers
How to make my marble's axis change based on direction of camera on mobile 0 Answers
I need help fast! Cant jump while moving in Android 1 Answer