TwinStick射击游戏控制器问题c#unity
我正在学习统一和C 我试着用我的Xbox平板电脑的左右拇指制作一个俯视射击游戏。我的角色使用左手拇指在x轴和z轴上正确移动。但枪在y轴上用右手指杆旋转时工作不正常,卡在45°、135°、225°、315°的角度。我不能平稳地旋转TwinStick射击游戏控制器问题c#unity,c#,unity3d,C#,Unity3d,我正在学习统一和C 我试着用我的Xbox平板电脑的左右拇指制作一个俯视射击游戏。我的角色使用左手拇指在x轴和z轴上正确移动。但枪在y轴上用右手指杆旋转时工作不正常,卡在45°、135°、225°、315°的角度。我不能平稳地旋转 using UnityEngine; using System.Collections; CharacterMotor controller; public Transform player_turret; public float deadzone = 0.25f;
using UnityEngine;
using System.Collections;
CharacterMotor controller;
public Transform player_turret;
public float deadzone = 0.25f;
private Quaternion desireRotation;
void Update (){
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(Input.GetAxis("Horizontal")*speed*Time.deltaTime,
0,
Input.GetAxis("Vertical")*speed*Time.deltaTime);
moveDirection = transform.TransformDirection(moveDirection);
controller.Move(moveDirection);
//right stick dead zone
Vector2 shootDirection = new Vector2(Input.GetAxis("FireHorizontal"),
Input.GetAxis("FireVertical"));
if(shootDirection.magnitude < deadzone){
shootDirection = Vector2.zero;
}else
{
Vector3 shootRotation = new Vector3(shootDirection.x, 0, shootDirection.y);
desireRotation = Quaternion.LookRotation(shootRotation);
player_turret.rotation = desireRotation;
if((Time.time >= nextFireTime) && Input.GetButton("Button A")){
FireProjectile();
}
}
}
使用UnityEngine;
使用系统集合;
特征电机控制器;
公共转换玩家_炮塔;
公共浮动死区=0.25f;
私有四元数渴望;
无效更新(){
CharacterController=GetComponent();
moveDirection=新矢量3(Input.GetAxis(“水平”)*速度*时间.deltaTime,
0,
输入.GetAxis(“垂直”)*速度*时间.deltaTime);
moveDirection=transform.TransformDirection(moveDirection);
控制器。移动(移动方向);
//右死区
Vector2 shootDirection=新Vector2(Input.GetAxis(“FireHorizontal”),
Input.GetAxis(“FireVertical”);
if(爆炸方向.震级<死区){
shootDirection=Vector2.0;
}否则
{
Vector3 shootRotation=新矢量3(shootDirection.x,0,shootDirection.y);
desireRotation=四元数。LookRotation(shootRotation);
玩家旋转炮塔=渴望;
if((Time.Time>=nextFireTime)和&Input.GetButton(“按钮A”)){
火箭弹();
}
}
}
我不确定,因为我无法测试,但您的枪是角色对象的一部分吗?旋转角色时,枪将随之旋转,而不会更改枪的局部旋转。这意味着如果将角色旋转几度,并将枪的旋转设置为90度,则它将与角色偏离90度。枪将在场景中旋转90+(character.rotation)。嗨,枪对象在角色对象内部的空游戏对象(玩家炮塔)中玩家炮塔对象单独旋转,角色不是这正是我要说的。如果对象旋转,它将基于父对象旋转。角色的父对象可能什么都不是,因此它将基于场景的轴进行旋转。你的玩家是角色的孩子。也就是说,如果你旋转你的玩家炮塔,它会根据父轴来旋转。如果你的父母(角色)转动,你的玩家炮塔也会随之转动,因为角色(父母)的旋转将保持不变。我认为你的旋转是错误的,或者实际上是你正在看的东西<代码>矢量3 shootRotation=新矢量3(shootDirection.x,0,shootDirection.y)代码>可能应更改为Vector3 shootRotation=new Vector3(this.transform.x+shootDirection.x,0,this.transform.y+shootDirection.y)代码>