wei.li23 2021-02-12 14:01 采纳率: 0%
浏览 11

我想解决赛道方向变为相反方向时小车移动监听方向会变反?uinty小白求帮忙

我遇到一个问题 小车在跑道上按钮监听左右移动 我用的是小车自身的坐标 然后当赛道方向反过来时按向左移动的按钮小车却往右走 按世界坐标则相反 一开始正方向移动反着来 反方向的时候方向却正着来 我觉得应该是小车的方向改变但是赛道坐标没有变化导致的 求解决方案


public class Control : MonoBehaviour
{
    private Vector3 offsetPosition;     
    private bool isMove;               
    public Transform luge;              
    private float Delay = 3f;
    private float LastDownTime;
  
    void Start()
    {
        
    }

   
    void Update()
    {
        if (isMove)
        {
            if (Time.time - LastDownTime >= Delay)
            {
                Debug.Log("长按");
                LastDownTime = Time.time;
                
                
                luge.Translate(offsetPosition * Time.deltaTime*-1 ,Space.Self );
            }
            
            luge.Translate(offsetPosition * Time.deltaTime *-1, Space.Self);
        }
    }
    public void MoveLuge(int i)
    {       
        Vector3[] poses = new Vector3[2] { Vector3.left, Vector3.right};
        offsetPosition = poses[i];
        isMove = true;
    }
    public void MoveButtonUp()
    {           
        isMove = false;
    }
}
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-24 12:07
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    using UnityEngine;
    
    public class Control : MonoBehaviour
    {
        // Use this for initialization
        void Start()
        {
            // Set the initial position of the luge
            transform.position = new Vector3(0, 0, 0);
    
            // Initialize variables
            float speed = 5.0f;
            float acceleration = 0.5f;
            int currentDirection = 1; // 1 for left, -1 for right
    
            // Create a variable to store the direction change
            bool directionChanged = false;
    
            // Define the movement function
            void Movement(float deltaTime)
            {
                // Check if the direction has changed
                if (directionChanged)
                {
                    // If it's been more than 3 seconds since the last update,
                    // reset the direction and check again.
                    if ((Time.time - LastUpdate) > 3)
                    {
                        directionChanged = false;
                        currentDirection *= -1;
                    }
                }
    
                // Calculate the velocity based on the current direction
                Vector3 velocity = Vector3.zero;
                if (currentDirection == 1)
                {
                    velocity += Vector3.right * speed;
                }
                else
                {
                    velocity -= Vector3.right * speed;
                }
    
                // Apply the acceleration
                velocity += Vector3.up * acceleration * deltaTime;
    
                // Apply the velocity
                transform.Translate(velocity * deltaTime);
            }
    
            // Define the update function that calls the movement function
            void Update()
            {
                // Reset the direction change flag
                directionChanged = false;
    
                // Call the movement function with the current time delta
                Movement(Time.deltaTime);
            }
    
            // Define the button press event
            void OnButtonDown(int buttonIndex)
            {
                // Toggle the direction
                if (!directionChanged && buttonIndex == 1)
                {
                    directionChanged = true;
                }
            }
    
            // Define the button release event
            void OnButtonUp(int buttonIndex)
            {
                // Reset the direction change flag after releasing the button
                directionChanged = false;
            }
    
            // Add the buttons to the game object
            GameObject.FindObjectOfType<Control>().OnButtonDown = OnButtonDown;
            GameObject.FindObjectOfType<Control>().OnButtonUp = OnButtonUp;
        }
    }
    

    在这个例子中,我们创建了一个Control类,它有一个Start方法和一个Update方法。在Update方法中,我们定义了Movement函数,这个函数根据当前的方向来计算速度,并应用到小车的运动上。

    此外,我们还添加了一些事件(OnButtonDownOnButtonUp),当按下或释放按钮时,它们都会触发这些事件并更新控制台。

    请注意,这只是一个基本的例子,实际的实现可能需要更多的错误处理和逻辑。

    评论

报告相同问题?