zoukankan      html  css  js  c++  java
  • Unity 2D物体左右移动脚本(鼠标触摸+键盘)

    using UnityEngine;
    using System.Collections;
    
    public class Paddle : MonoBehaviour
    {
        void Update()
        {
            if (GameManager.Instance.currentState == GameState.Playing && GameManager.Instance != null)
            {
                //鼠标或触摸
                if (Input.GetMouseButton(0))
                {
                    //Create a ray from camera to playfield
                    Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
                    Plane p = new Plane(Vector3.up, transform.position);
    
                    //Shoot the ray to know where the click/tap found place in 3D
                    float distance;
                    if (p.Raycast(mouseRay, out distance))
                    {
                        //Use current position as starting point
                        Vector3 position = transform.position;
                        //The player can only move the paddle in the x axis, so don't use the y and z
                        position.x = mouseRay.GetPoint(distance).x; //GetPoint gives us the position in 3D
                                                                    //Apply the new position
                        transform.position = position;
                    }
                    else
                    {
                        //The ray missed
                    }
                }
    
                // 键盘
                else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
                {
                    //Use current position as starting point
                    Vector3 position = transform.position;
                    position.x = position.x - Time.deltaTime * 50;
                    transform.position = position;
    
    
                }
                else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
                {
                    //Use current position as starting point
                    Vector3 position = transform.position;
                    position.x = position.x + Time.deltaTime * 50;
                    transform.position = position;
                }
            }
            
            //Make sure the paddle stays inside the level
            float freedom = 19.25F;
            Vector3 limitedPosition = transform.position;
            if (Mathf.Abs(limitedPosition.x) > freedom)
            {
                //Paddle is outside the level so move it back in
                limitedPosition.x = Mathf.Clamp(transform.position.x, -freedom, freedom);
                transform.position = limitedPosition;
            }
        }
    }

     补充:

    1.Unity实现物体左右循环移动效果

    2.Unity 3D 控制物体前后左右均匀移动脚本

    365个夜晚,我希望做到两天更一篇博客。加油,小白!
  • 相关阅读:
    i.MX6UL: i.MX 6UltraLite处理器
    温控产品解决方案
    原子层沉积(ALD)和化学气相沉积(CVD)微电子制造铜金属化的研究进展
    camera数字降噪(DNR)
    光刻机技术领地
    晶圆代工解决方案
    Camera噪声问题
    camera中LENS和SENSOR的CRA是如何搭配的?
    Camera Lens Coating
    (转)唐家三少--写书赚钱还是太少了
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15033604.html
Copyright © 2011-2022 走看看