平滑转向C#Unity

发布于 2025-01-20 18:54:49 字数 1428 浏览 0 评论 0原文

我设法与我的卡丁车团结一致地向前前进。但是,当涉及到转弯时,这成为一个真正的挑战,我无法通过A&amp平稳地转动我的车辆; D键。

请帮忙!

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class KartController : MonoBehaviour
    {
        private Rigidbody rb;
        // adding a speed variable
        public float speed = 3.0f;
        // adding a brake variable
        public float BrakeSpeed = 1.0f;
        // adding a rotation position variable
        
        // Start is called before
        void Start()
        {
            // refering to the game object component/class
            rb = GetComponent<Rigidbody>();
        }
    
        // Update is called once per frame
        void Update()
        {
            // adding movement for forward
            if (Input.GetKey(KeyCode.W))
            {
                rb.velocity = new Vector3(speed * -2, 0, 0);
            }

            // adding movement for backward
            if (Input.GetKeyDown(KeyCode.S))
            {
                rb.velocity = new Vector3(speed * 2, 0, 0); 
            }
    
            // adding movement for rotation
            float rotation_speed = 10f;
    
            if (Input.GetButtonDown("Horizontal"))
            {
                transform.Rotate(0.0f, -Input.GetAxis("Horizontal") * rotation_speed, 0.0f);
            }

            Debug.Log(rotation_speed);
        }
    }

I have managed to go backwards and forwards with my kart in Unity. But when it comes to turning, it becomes a real challenge and I am unable to turn my vehicle smoothly with my a & d keys.

Please help!

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class KartController : MonoBehaviour
    {
        private Rigidbody rb;
        // adding a speed variable
        public float speed = 3.0f;
        // adding a brake variable
        public float BrakeSpeed = 1.0f;
        // adding a rotation position variable
        
        // Start is called before
        void Start()
        {
            // refering to the game object component/class
            rb = GetComponent<Rigidbody>();
        }
    
        // Update is called once per frame
        void Update()
        {
            // adding movement for forward
            if (Input.GetKey(KeyCode.W))
            {
                rb.velocity = new Vector3(speed * -2, 0, 0);
            }

            // adding movement for backward
            if (Input.GetKeyDown(KeyCode.S))
            {
                rb.velocity = new Vector3(speed * 2, 0, 0); 
            }
    
            // adding movement for rotation
            float rotation_speed = 10f;
    
            if (Input.GetButtonDown("Horizontal"))
            {
                transform.Rotate(0.0f, -Input.GetAxis("Horizontal") * rotation_speed, 0.0f);
            }

            Debug.Log(rotation_speed);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2025-01-27 18:54:49

您有几种不同的选择,并且正在混合一些东西。

您的卡丁车是具有物理模拟的僵化体。要向前或向后移动它,您将设置卡丁车速度。如果对象在物理模拟中具有速度,则将尝试移动,因为在模拟中求解了标准物理公式。 IE距离移动=速度 *

旋转时间适用相同的概念。刚性体的角速度。如果对象具有角速度,则尝试相应地旋转。

使用变换旋转,您将直接设置对象的旋转。您基本上是在跳过物理模拟。如果您想在比赛开始时重置卡丁车的方向和位置,但不适用于在模拟中控制卡丁车,这是很好的。

您可以直接设置rb。离速度类似于设置卡丁车方向速度的方式。

根据游戏类型和现实主义的类型,您可能需要考虑控制加速度而不是速度。甚至是轮廓派...( https://docs.unity3d.com/manual/manual/wheelcollcolliderterorial.htmllorial.htmllorial..htmllliorial.htmllliorial.htmll

You've got a few different options and you are mixing a few things up.

Your Kart is a Rigidbody with a physics simulation. To move it forward or backwards you are setting the Karts velocity. If an object has a velocity in the physics simulation it will try to move because in the simulation the standard physics formula are solved. I.e. distance moved = velocity * time

For rotation the same concepts apply. There is the angular velocity of a rigidbody. If an object has angular velocity it tries to rotate accordingly.

With transform Rotate you are setting the rotation of the object directly. You are basically skipping the physics simulation. This is good if you want to for example reset your kart orientation and position at the start of the race but is less suited to control your Kart in the simulation.

You could directly set rb.angularVelocity similar how you set the Karts directional velocity.

Depending on the type of game and the realism you might want to look into controlling the acceleration instead of the velocity. Or even wheelcollider... (https://docs.unity3d.com/Manual/WheelColliderTutorial.html)

情深如许 2025-01-27 18:54:49

您可以尝试以下代码选车,希望对您有帮助,谢谢

       private float y;
      //vehicle control speed parameters
       private float speedOne = 0f; //Vehicle real-time speed
       private float speedMax = 120f; //The maximum speed of the vehicle
       private float speedMin = -20f; //The minimum speed of the vehicle (the maximum speed of reversing)
       private float speedUpA = 2f; //Vehicle acceleration acceleration (A key control)
       private float speedDownS = 4f; //Vehicle deceleration acceleration (S key control)
       private float speedTend = 0.5f; //Acceleration when no operation real-time speed tends to 0
       private float speedBack = 1f; //Vehicle reversing acceleration
      // Update is called once per frame
      void Update()
    {
        //mouse hide
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
       //Press the W key and the speed does not reach the maximum, then the speed increases
        if (Input.GetKey(KeyCode.W) && speedOne < speedMax)
       {
          speedOne = speedOne + Time.deltaTime * speedUpA;
       }
       //Press the S key and the speed does not reach zero, then the speed is reduced
       if (Input.GetKey(KeyCode.S) && speedOne > 0f)
      {
         speedOne = speedOne - Time.deltaTime * speedDownS;
      }
     // No speed operation is performed and the speed is greater than the minimum speed, then the slow operation
      if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && speedOne > 0f)
      {
         speedOne = speedOne - Time.deltaTime * speedTend;
      }
      if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && speedOne < 0f)
      {
        speedOne = speedOne + Time.deltaTime * speedTend;
      }

      //When the S key is pressed and the speed does not reach the maximum reversing speed, and the vehicle is in a state where the vehicle can be reversed, the vehicle reverses
      if (Input.GetKey(KeyCode.S) && speedOne > speedMin && speedOne<=0)
      {
        speedOne = speedOne - Time.deltaTime * speedBack;
      }

        /// press space, the car stops
      if (Input.GetKey(KeyCode.Space) && speedOne != 0)
      {
        speedOne = Mathf.Lerp(speedOne, 0, 0.4f);
        if (speedOne < 5) speedOne = 0;
       }
      transform.Translate(Vector3.forward * speedOne * Time.deltaTime);
     //Use A and D to control the left and right rotation of the object
       if(speedOne>1f||speedOne<-1f)
       {
           y = Input.GetAxis("Horizontal") * 60f * Time.deltaTime;
           transform.Rotate(0, y, 0);
       }
    
       /* if (transform.eulerAngles.z != 0)
       {
           transform.eulerAngles = new Vector3(transform.eulerAngles.x, t 
           ransform.eulerAngles.y, 0);
       }*/
     }

You can try the following code to select the car, I hope it can help you, thank you

       private float y;
      //vehicle control speed parameters
       private float speedOne = 0f; //Vehicle real-time speed
       private float speedMax = 120f; //The maximum speed of the vehicle
       private float speedMin = -20f; //The minimum speed of the vehicle (the maximum speed of reversing)
       private float speedUpA = 2f; //Vehicle acceleration acceleration (A key control)
       private float speedDownS = 4f; //Vehicle deceleration acceleration (S key control)
       private float speedTend = 0.5f; //Acceleration when no operation real-time speed tends to 0
       private float speedBack = 1f; //Vehicle reversing acceleration
      // Update is called once per frame
      void Update()
    {
        //mouse hide
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
       //Press the W key and the speed does not reach the maximum, then the speed increases
        if (Input.GetKey(KeyCode.W) && speedOne < speedMax)
       {
          speedOne = speedOne + Time.deltaTime * speedUpA;
       }
       //Press the S key and the speed does not reach zero, then the speed is reduced
       if (Input.GetKey(KeyCode.S) && speedOne > 0f)
      {
         speedOne = speedOne - Time.deltaTime * speedDownS;
      }
     // No speed operation is performed and the speed is greater than the minimum speed, then the slow operation
      if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && speedOne > 0f)
      {
         speedOne = speedOne - Time.deltaTime * speedTend;
      }
      if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && speedOne < 0f)
      {
        speedOne = speedOne + Time.deltaTime * speedTend;
      }

      //When the S key is pressed and the speed does not reach the maximum reversing speed, and the vehicle is in a state where the vehicle can be reversed, the vehicle reverses
      if (Input.GetKey(KeyCode.S) && speedOne > speedMin && speedOne<=0)
      {
        speedOne = speedOne - Time.deltaTime * speedBack;
      }

        /// press space, the car stops
      if (Input.GetKey(KeyCode.Space) && speedOne != 0)
      {
        speedOne = Mathf.Lerp(speedOne, 0, 0.4f);
        if (speedOne < 5) speedOne = 0;
       }
      transform.Translate(Vector3.forward * speedOne * Time.deltaTime);
     //Use A and D to control the left and right rotation of the object
       if(speedOne>1f||speedOne<-1f)
       {
           y = Input.GetAxis("Horizontal") * 60f * Time.deltaTime;
           transform.Rotate(0, y, 0);
       }
    
       /* if (transform.eulerAngles.z != 0)
       {
           transform.eulerAngles = new Vector3(transform.eulerAngles.x, t 
           ransform.eulerAngles.y, 0);
       }*/
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文