[ad_1]
I’m making a sport the place you shoot enemies in Unity, however I can not determine the way to cease the participant from pointing the gun at himself.
I’ve tried detecting if the gun is on the left aspect of the participant and if it is rotation on the z-axis is lower than 0, however no luck
Here’s the code for the gun:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class GunController : MonoBehaviour
{
public bool testbool;
public Transform participant;
public CharacterController2D playercontroller;
public float smoothness = 0.125f;
public float winglength = 2;
non-public Vector2 distinction;
bool facingright = false;
public GameObject bullet;
public int maxammo;
non-public int ammo;
non-public bool firing;
public Vector3 bulletspawnpos;
non-public SpriteRenderer sprite;
public Texture2D normalGun;
public Texture2D emptyGun;
public GameObject emptyBottle;
// Start is known as earlier than the primary body replace
void Start()
{
sprite = GetComponent<SpriteRenderer>();
participant = remodel.dad or mum.remodel;
ammo = maxammo;
}
// Update is known as as soon as per body
void Update()
{
}
non-public void FastenedUpdate()
{
firing = false;
bool wasfacingright = facingright;
if (Input.GetMouseButton(0) & ammo !>= 0 & !firing )
{
firing = true;
Instantiate(bullet, remodel.place+bulletspawnpos, remodel.rotation);
}
if (participant.remodel.place.x - remodel.place.x < 0)
{
facingright = false;
sprite.flipY = false;
}
if (participant.remodel.place.x - remodel.place.x > 0)
{
facingright = true;
sprite.flipY = true;
}
if (wasfacingright != facingright) { playercontroller.Flip(); }
distinction = Camera.most important.ScreenToWorldPoint(Input.mousePosition) - remodel.place;
float rot = Mathf.Atan2(distinction.y, distinction.x) * Mathf.Rad2Deg;
Vector3 pos = Camera.most important.ScreenToWorldPoint(Input.mousePosition) - participant.place;
pos.z = 0;
Vector3 desiredpos = participant.place + (winglength * pos.normalized);
Vector3 smoothpos = Vector3.Lerp(remodel.place, desiredpos, smoothness);
remodel.place = smoothpos;
remodel.rotation = Quaternion.Euler(0, 0, rot);
}
}
Before you level out that the ammo would not go down, simply know I did not add that for testing functions.
[ad_2]
