Snippets Collections
 //this will store the initial posityion of the joystick, so it returns to its position when we dont click the screen
    private Vector3 initPosOfJoystick;
    //this will store the transform of joystick
    public RectTransform joystick;

    [SerializeField]
    //this will store the canvas 
    Canvas canvas;

    [SerializeField]
    //this will store the limit of the movement of the joystick
    float limitOfJoystick;

    //this will be true if we are dragging the joystick, we need this, so that we only start moving the joystick when we have clicked it, and not outside of it
    bool amIDragging = false;

    private void Start()
    {
        //get the initial position of the joystick
        initPosOfJoystick = joystick.position;
    }

    // Update is called once per frame
    void Update()
    {
        
        //if the mouse is clicking
        if(Input.GetMouseButton(0))
        {
            //this will store the movepos of the mouse
            Vector2 movePos;
            //get the mouse position on the canvas
            RectTransformUtility.ScreenPointToLocalPointInRectangle(
                canvas.transform as RectTransform,
                Input.mousePosition, canvas.worldCamera,
                out movePos);

            

            //if the distance between the mouse and the 
            if (Vector3.Distance(canvas.transform.TransformPoint(movePos), initPosOfJoystick) < limitOfJoystick)
            {
                //move the joystick to where the mouse is 
                joystick.position = canvas.transform.TransformPoint(movePos);
                amIDragging = true;
            }
                
            //if we are not inside the joystick space, we need to move the joystick to where the mouse is pointing within the limit of the joystick movement
            //and we have started dragging this joystick, we can move him even tho the mouse pos is outside the joystick
            else if(amIDragging==true)
            {
                //this will get the offset from the mouse position and the initial joystick position
                Vector3 offset = canvas.transform.TransformPoint(movePos) - initPosOfJoystick;
                //this will move the mouse within the limitis of the joystick space
                joystick.position = initPosOfJoystick + Vector3.ClampMagnitude(offset, limitOfJoystick);
            }
                
        }
        //if we are not clicking the mouse / touchscreen e move the joystick to its initial position
        else
        {
            //move the joystick to its init pos
            joystick.position = initPosOfJoystick;
            //tell the code that we are no longer dragging
            amIDragging = false;
        }
            
        
    }
using Photon.Pun;
using UnityEngine;

namespace PhotonTutorial.Weapons
{
    public class Weapon : MonoBehaviourPun
    {
        //this will store how fast the bullet moves
        [SerializeField] private float projectileSpeed = 5f;
        //this will store the projectile that is going to be shooted from the gun
        [SerializeField] private GameObject projectile = null;
        //this will store where the bullets will spawn
        [SerializeField] private Transform spawnPoint = null;

        private void Update()
        {
            //if this is my photon view (if this is my character and not any other players character) call the function that thakes input
            if (photonView.IsMine)
            {
                //call the function that handles the input
                TakeInput();
            }
        }

        private void TakeInput()
        {
            //if the player hasnt clicked mouse button, leave this function
            if(!Input.GetMouseButtonDown(0)) { return; }
            //since the user clicked the shooting button, call the function fire projectile, that is going to spawn a bullet on every player pc
            FireProjectile();
        }

    
        private void FireProjectile()
        {
            //this is going to spawn the bullet on every computer (since we are using PunRPC)
            GameObject projectileInstance = PhotonNetwork.Instantiate(projectile.name, spawnPoint.position, spawnPoint.rotation);
            //this is going to add velocity to where the spawn bullet is facing, in this case is from the spawn point
            projectileInstance.GetComponent<Rigidbody>().velocity = projectileInstance.transform.forward * projectileSpeed;
        }
    }
}
export JAVA_HOME=/Users/kenmiya/workspace/Unity/2021.1.1f1/PlaybackEngines/AndroidPlayer/OpenJDK/jre
echo $JAVA_HOME
export PATH=$JAVA_HOME/bin:$PATH
echo $PATH
    public float movementSpeed;

    public Rigidbody2D rb;



    float mx;



    private void Update()
    {

        mx = Input.GetAxisRaw("Horizontal");

    }



    private void FixedUpdate()
    {

        Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);



        rb.velocity = movement;



    }
if (isExplosion) {
    foreach (GameObject enemy in units) {
        float distanceToEnemy = Vector3.Distance (transform.position, enemy.transform.position);
        if (distanceToEnemy <= explosionRadius) {
            Unit unit = enemy.GetComponent<Unit> ();
            float proximity = (transform.position - enemy.transform.position).magnitude;
            float effect = 1 - (proximity / explosionRadius);
            unit.TakeDamage ((int) (damage * effect));
            if (enemy.GetComponent<Setup> ().currentHealth <= 0) {
                GameObject explosionDestroy = Instantiate (enemy.GetComponent<Setup> ().explosionDestroy, enemy.transform.position, Quaternion.identity);
                Destroy (explosionDestroy, 1);
                Destroy (enemy);
            }
        }
    }
}
star

Wed May 04 2022 13:55:40 GMT+0000 (UTC) https://localcoder.org/position-ui-to-mouse-position-make-tooltip-panel-follow-cursor

#c# #unity
star

Tue Apr 12 2022 02:19:39 GMT+0000 (UTC) https://www.youtube.com/watch?v=NA5rO8j21JY&list=PLS6sInD7ThM0nJcyLfxgP9fnHKoQMJu-v&index=5

#c# #unity #unity #photon
star

Sun Jun 20 2021 02:03:51 GMT+0000 (UTC)

#shell #unity

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension