unity的一个获取轴小问题

using UnityEngine;

public class TankMovement : MonoBehaviour
{
public int m_PlayerNumber = 1; // Used to identify which tank belongs to which player. This is set by this tank's manager.
public float m_Speed = 12f; // How fast the tank moves forward and back.
public float m_TurnSpeed = 180f; // How fast the tank turns in degrees per second.
public AudioSource m_MovementAudio; // Reference to the audio source used to play engine sounds. NB: different to the shooting audio source.
public AudioClip m_EngineIdling; // Audio to play when the tank isn't moving.
public AudioClip m_EngineDriving; // Audio to play when the tank is moving.
public float m_PitchRange = 0.2f; // The amount by which the pitch of the engine noises can vary.

private string m_MovementAxisName;          // The name of the input axis for moving forward and back.
private string m_TurnAxisName;              // The name of the input axis for turning.
private Rigidbody m_Rigidbody;              // Reference used to move the tank.
private float m_MovementInputValue;         // The current value of the movement input.
private float m_TurnInputValue;             // The current value of the turn input.
private float m_OriginalPitch;              // The pitch of the audio source at the start of the scene.


private void Awake ()
{
    m_Rigidbody = GetComponent<Rigidbody> ();
}


private void OnEnable ()
{
    // When the tank is turned on, make sure it's not kinematic.
    m_Rigidbody.isKinematic = false;

    // Also reset the input values.
    m_MovementInputValue = 0f;
    m_TurnInputValue = 0f;
}


private void OnDisable ()
{
    // When the tank is turned off, set it to kinematic so it stops moving.
    m_Rigidbody.isKinematic = true;
}


private void Start ()
{
    // The axes names are based on player number.
    m_MovementAxisName = "Vertical" + m_PlayerNumber;
    m_TurnAxisName = "Horizontal" + m_PlayerNumber;

    // Store the original pitch of the audio source.
    m_OriginalPitch = m_MovementAudio.pitch;
}


private void Update ()
{
    // Store the value of both input axes.
    m_MovementInputValue = Input.GetAxis (m_MovementAxisName);
    m_TurnInputValue = Input.GetAxis (m_TurnAxisName);

    EngineAudio ();
}


private void EngineAudio ()
{
    // If there is no input (the tank is stationary)...
    if (Mathf.Abs (m_MovementInputValue) < 0.1f && Mathf.Abs (m_TurnInputValue) < 0.1f)
    {
        // ... and if the audio source is currently playing the driving clip...
        if (m_MovementAudio.clip == m_EngineDriving)
        {
            // ... change the clip to idling and play it.
            m_MovementAudio.clip = m_EngineIdling;
            m_MovementAudio.pitch = Random.Range (m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
            m_MovementAudio.Play ();
        }
    }
    else
    {
        // Otherwise if the tank is moving and if the idling clip is currently playing...
        if (m_MovementAudio.clip == m_EngineIdling)
        {
            // ... change the clip to driving and play.
            m_MovementAudio.clip = m_EngineDriving;
            m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
            m_MovementAudio.Play();
        }
    }
}


private void FixedUpdate ()
{
    // Adjust the rigidbodies position and orientation in FixedUpdate.
    Move ();
    Turn ();
}


private void Move ()
{
    // Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
    Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;

    // Apply this movement to the rigidbody's position.
    m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
}


private void Turn ()
{
    // Determine the number of degrees to be turned based on the input, speed and time between frames.
    float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;

    // Make this into a rotation in the y axis.
    Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);

    // Apply this rotation to the rigidbody's rotation.
    m_Rigidbody.MoveRotation (m_Rigidbody.rotation * turnRotation);
}

}

这是官方代码
m_MovementAxisName = "Vertical" + m_PlayerNumber;
m_TurnAxisName = "Horizontal" + m_PlayerNumber;

            这一句怎么分析
            不是GetAxis("Horizontal")吗
            这个怎么也能那么用

            还有最后一句
            怎么用的是乘法,不是应该从初始位置旋转吗?旋转角度不是用的加法吗?
            移动跟旋转的操作方式不一样?

1个回答

qq_37296877
qq_37296877 谢谢帮助,但不是我想要的
2 年多之前 回复
qq_37296877
qq_37296877 大神帮忙分析下这一句为什么是乘法m_Rigidbody.MoveRotation (m_Rigidbody.rotation * turnRotation);
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
Unity之虚拟轴
虚拟轴 概念: 首先,啥子叫虚拟轴呢? 用我们通俗的话来说就是我们自己想象的一条范围是 -1~1之间的数轴。 为啥子要用到虚拟轴呢? 就是为了方便获取键盘的输入,其实就是一种简便方法。比如我们想使用A、D键和左、右键来控制游戏物体的左右移动时,按照我们的之前的思路是用一个if条件来判断四个键是否按下, 而有了虚拟轴之后,我们只需要获取水平方向的输入就可以。 文字有点枯燥,我们画个图来...
Unity虚拟轴
查看虚拟轴:Edit / project settings / Input 1.Horizontal 水平轴 Name:Horizontal (默认就是“Horizontal”,这里可以修改它的名字,一般不动它) --- ---这两个是对轴的描述 Negative Button :Left (负轴,键盘左键) Positive Button:right (正轴,键盘...
unity 虚拟轴
input.GetAxis 用法(GetAxis(&quot;Mouse X&quot;),GetAxis(&quot;Mouse Y&quot;),GetAxis(&quot;Mouse ScrollWheel&quot;),GetAxis(&quot;Vertical &quot;),GetAxis(&quot;Horizontal &quot;),           GetAxis 是个方法,需要传参数,参数为string类型,参数如下:            一:触屏类        ...
Unity 物体移动之虚拟轴移动
物体移动有很多方式,比如刚体的速度或者直接添加力,3d物体还能够使用角色控制器来进行移动,但是2D物体的移动很多就是采用插值的方式移动,因为2D物体需要精准的控制坐标 获取虚拟轴是我们最常用的获取移动的方式,方向以及大小都可以通过这个控制 这个方法是通过获取键盘的虚拟轴通过GetAxisRaw这个方法,将键盘的输入转化为移动的距离 public Vector2 targetPos =...
Unity之绕轴进行旋转
先上一张效果图 using UnityEngine; using System.Collections; public class TestRotateRound : MonoBehaviour { public GameObject Sphere; private float curtTime = 0.0f; void Update() { ...
【Unity】摄像机锁定z轴
第三人称视角锁定z轴,避免出现视角倾斜。
【Unity】Unity 获取网络状态
if (Application.internetReachability == NetworkReachability.NotReachable) { Debugger.Log ("没有联网!!!"); } if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork) { Debu
【Unity】Unity 获取当前按键
就用一个方法 void OnGUI() { if (Input.anyKeyDown) { Debug.Log(Event.current.keyCode); } }
Jquery获取DOME的一个小问题
(function(win)rn win.autoAni = function()rn alert($('#banner').length); rn rn)(window)rnwindow.autoAnima();rnrnfunction win()rn this.autoAni = function()rn alert($('#banner').length); rn rnrnvar _win=new win();rn_win.autoAni();rn上面两段代码均提示 0 ,为什么会获取不到$('#banner').length ,其它方式能获取到为 1 ,如何才能在上面2中方式中获取到$('#banner').length ,请各位大神说下原理
一个小问题!!获取元素的ID
JS中,可以通过方法var elem = document.getElementById("elemId");获取变量。rnrn反过来,能否通过elem得到元素的ID。。rnrnrn谢谢了!!rnrn要写个通用的函数,需要上述功能。
Unity遇到的小问题
1.只有public类型的属性才能在编译器窗口实例化。 2.对于需要重复使用的游戏体,需要将其制作成prefab(活动房间)。
第一篇博客 unity的小问题
这是我写的第一篇博客哦,哈哈。我要坚持写博客,大三一年我要逆袭逆袭。 这个第一篇就来记录unity中的一些小问题吧。 嗯,,比如unity放大之后一部分就会消失,如何解决这个问题呢,就选中我要放大看的物体,然后按F键,就可以锁定啦。 第一篇完成!!哈哈
求帮忙,unity的小问题
using UnityEngine;nnpublic class ShellExplosion : MonoBehaviournn public LayerMask m_TankMask; // Used to filter what the explosion affects, this should be set to "Players".n public ParticleSystem m_ExplosionParticles; // Reference to the particles that will play on explosion.n public AudioSource m_ExplosionAudio; // Reference to the audio that will play on explosion.n public float m_MaxDamage = 100f; // The amount of damage done if the explosion is centred on a tank.n public float m_ExplosionForce = 1000f; // The amount of force added to a tank at the centre of the explosion.n public float m_MaxLifeTime = 2f; // The time in seconds before the shell is removed.n public float m_ExplosionRadius = 5f; // The maximum distance away from the explosion tanks can be and are still affected.nnn private void Start ()n n // If it isn't destroyed by then, destroy the shell after it's lifetime.n Destroy (gameObject, m_MaxLifeTime);n nnn private void OnTriggerEnter (Collider other)n n // Collect all the colliders in a sphere from the shell's current position to a radius of the explosion radius.n Collider[] colliders = Physics.OverlapSphere (transform.position, m_ExplosionRadius, m_TankMask);nn // Go through all the colliders...n for (int i = 0; i < colliders.Length; i++)n n // ... and find their rigidbody.n Rigidbody targetRigidbody = colliders[i].GetComponent ();nn // If they don't have a rigidbody, go on to the next collider.n if (!targetRigidbody)n continue;nn // Add an explosion force.n targetRigidbody.AddExplosionForce (m_ExplosionForce, transform.position, m_ExplosionRadius);nn // Find the TankHealth script associated with the rigidbody.n TankHealth targetHealth = targetRigidbody.GetComponent ();nn // If there is no TankHealth script attached to the gameobject, go on to the next collider.n if (!targetHealth)n continue;nn // Calculate the amount of damage the target should take based on it's distance from the shell.n float damage = CalculateDamage (targetRigidbody.position);nn // Deal this damage to the tank.n targetHealth.TakeDamage (damage);n nn // Unparent the particles from the shell.n m_ExplosionParticles.transform.parent = null;nn // Play the particle system.n m_ExplosionParticles.Play();nn // Play the explosion sound effect.n m_ExplosionAudio.Play();nn // Once the particles have finished, destroy the gameobject they are on.n Destroy (m_ExplosionParticles.gameObject, m_ExplosionParticles.duration);nn // Destroy the shell.n Destroy (gameObject);n nnn private float CalculateDamage (Vector3 targetPosition)n n // Create a vector from the shell to the target.n Vector3 explosionToTarget = targetPosition - transform.position;nn // Calculate the distance from the shell to the target.n float explosionDistance = explosionToTarget.magnitude;nn // Calculate the proportion of the maximum distance (the explosionRadius) the target is away.n float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius;nn // Calculate damage as this proportion of the maximum possible damage.n float damage = relativeDistance * m_MaxDamage;nn // Make sure that the minimum damage is always 0.n damage = Mathf.Max (0f, damage);nn return damage;n nnnm_ExplosionParticles.transform.parent = null;nn这行代码实现了什么功能
很迷的Unity小问题
最近在写脚本的时候出了个挺诡异的bug。 有GameObj1和GameObj2 obj1上挂载的脚本监听了obj2上的脚本里的事件。 而obj1上用于响应事件的函数里使用了自身的gameObject属性。 接着我需要把obj1销毁。 在obj1被销毁后,obj2触发了事件,这时log窗口疯狂地报错,提示gameObject已被销毁。 懵逼了很久,之后花了大量的时间来...
Unity 向量围绕一个轴旋转 求新向量
1、四元数法 Vector3 newVec = Quaternion.AngleAxis(angle,axis)*oriVec; 或者 Vector3 newVec = Quaternion.Eular(0,angle,0)*oriVec; 2、二维向量运算公式: x1=xcosθ+ysinθ, y1=-xsinθ+ycosθ
highcharts获取Y轴名称
this.point.series.yAxis.axisTitle.textStr
一个小问题。。。。。如何获取一个网址?
如rn我在http://community.csdn.net/Expert/PostNew.asp?room=301这个页面,我想获取这条网址并且赋予给一个变量rn谁可以指教一下吗
unity 获取摄像头视频流
unity 获取摄像头视频流
Unity获取场景
Unity4.x获取场景分为两种: 编辑模式 string path = EditorApplication.currentScene;   运行状态下 string name = Application.loadedLevelName;   Unity2017: 编辑模式 string name = EditorSceneManager.GetActiveSce...
unity 获取磁盘空间
备注: System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); 在unity下使用会报错 NotImplementedException: The requested feature is not implemented. 本方法在unity下可以使用/
Unity获取经纬度(GPS)
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Text; using System; public class UserInfo : MonoBehaviour {     public Text GPS_infoText;     string sGPS_inf
Unity 深度图片获取
1、  在Unity中,要得到深度缓存(Depthbuffer),就需要用到一个RenderTexture纹理。 2、  深度缓存,或者深度纹理,其实就是一个包含有场景里物体离照相机的距离值的一张纹理图。 3、  为了让摄像机生成一张深度纹理,只需要设置Camera.depthTextureMode即可。但这个时候生成了纹理,还需要我们去获取。所以我们需要把它传到一个shader中,于是就用到
unity获取设备分辨率
unity获取设备分辨率 设备分辨率 using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Start() { Resolution[...
Unity获取串口数据
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO.Ports; using System; using System.Threading; using UnityEngine.UI; public class PortManager  {
Unity获取网络时间
using System.Collections; using System.Collections.Generic; using UnityEngine; public class IntTime : MonoBehaviour { private int year, mouth, day, hour, min, sec; private string timeURL = "h
unity 获取电量;
unity 获取电量;
unity中获取FPS
1.获取适合时间内的FPS平均值,此处为0.5s内的平均帧数  public class FPS : MonoBehaviour  {  public UnityEngine.UI.Text textObj_fps;  public bool showFPS = true;  private float updateInterval = 0.5f;  private float a
Unity获取系统信息
Unity的原生类UnityEngine.SystemInfo 以前为了获取手机的一些信息去写一些Android或着IOS的原生脚本最近才发现Unity已经为开发者准备好了这些接口。在Unity中有一个UnityEngine.SystemInfo的类。 在这个类中有一些方法下面简单的列出一些信息出来, 这个类中提供的一些属性信息均为只读 //获取电池电量 SystemInfo.batteryLe...
Unity时间获取及应用
Unity游戏 时间获取 及开发 常用于游戏签到系统等,里面整理了常用的时间获取方法
unity获取摄像头
测试一下摄像头功能:WebCamTexture deviceName Set this to specify the name of the device to use. didUpdateThisFrame Did the video buffer update this frame? isPlaying Returns if the camera i
Unity获取安卓电量
博文地址:http://blog.csdn.net/bill501y/article/details/49679371
unity 获取包围盒
1.获取中心点 public Vector3 GetCenter(GameObject target) { Renderer[] mrs = target.gameObject.GetComponentsInChildren&amp;lt;Renderer&amp;gt;(); Vector3 center = target.tr...
Unity获取NoloVR手柄
这里对NoloVR的手柄获取进行了搬移记录,来自官方文档; 记得去官方去下载插件导入哦;不然没有NoloVR类; https://www.nolovr.com/003_3_1?treeidp=003&amp;amp;treeid=303 public class Input_Test : MonoBehaviour { void Update () { //右手手柄 ...
unity 获取方法运行时间
方法一: Stopwatch sw = new Stopwatch(); sw.Start(); TestMethod(); sw.Stop(); UnityEngine.Debug.Log(string.Format(&quot;total: {0} ms&quot;,sw.ElapsedMilliseconds));   方法二:需要打开打开Profiler界面找到那一帧 Profiler.Beg...
unity获取精灵宽度
Sprite spr = Resources.Load&amp;lt;Sprite&amp;gt;(&quot;Picture/&quot; + url); go.AddComponent&amp;lt;SpriteRenderer&amp;gt;().sprite = spr; //固定在场景上的大小,不受图片控制 float w = spr.bounds.size.x; go.transform.loc...
Unity获取隐藏物体
Unity场景中通常有许多隐藏物体,我们需要获取它并且SetActive(true)。 例如,想要使BBB显示出来。通过GameObject.Find(“BBB”),得到一个空指针。 解决方法有两个:1.Editor赋值在脚本中声明共有变量GameObject,编辑器中直接拖拽赋值,然后SetActive(“true”)。 这种方法可行,但是灵活性太差。如果GameObject不确定,则实
unity 获取AssetBundle的哈希值
1. 在Unity官方文档可以知道 AssetBundle manifestBundle = AssetBundle.LoadFromFile(manifestBundlePath); AssetBundleManifest manifest = manifestBundle.LoadAsset&amp;amp;lt;AssetBundleManifest&amp;amp;gt;(&amp;quot;AssetBundleManifest&amp;quot;); ...
unity获取设备ID
unity获取设备唯一标识 android ios 包括ios7以上没有问题
Unity 获取唯一标识符的问题解决方案
环境:只适用于PC一:遇到的坑    Unity 提供了给我一套获取硬件信息的API 链接 蛮牛上的一篇帖子:链接    文中都提到了一个获取设备的唯一标识符    但是我可以明确告诉你 这个值会改变(过一段时间就会变 就是被他坑了)    查看api文档        是根据主板序列号.BIOS序列号.cpu信息.硬盘序列号.操作系统信息 来进行加密或者其他算法得出的,也许是某个东西会随着时间而...
Unity 获取当前网络状态
private bool GetNetWorkState() {         if (Application.internetReachability==NetworkReachability.NotReachable)         {             return false;         }         return true;     }
相关热词 c++和c#哪个就业率高 c# 批量动态创建控件 c# 模块和程序集的区别 c# gmap 截图 c# 验证码图片生成类 c# 再次尝试 连接失败 c#开发编写规范 c# 压缩图片好麻烦 c#计算数组中的平均值 c#获取路由参数