Question by
blimp47 · Oct 17, 2020 at 01:43 PM ·
inputmanagertransform.translate
Input manager or Transform.'direction' issue
I have a weird issue with the code below where movement on the z "vertical" axis does not occur. the Debug logs show me that the input manager is working perfectly. the vertical inputs produce either -1 or +1 but the object only responds to the horizontal inputs. If i comment out the xPlayerMove(); in update function then verticle movement on zPlayerMove(); works just fine. Any Ideas? Also, please see my input manager at the bottom. Cheers.
public class ExampleCode: MonoBehaviour
{
[Tooltip("In ms^-1, total normal spped")] [SerializeField] float normalPanSpeed = 20f;
[Tooltip("In ms^-1, total boosted speed")] [SerializeField] float boostPanSpeed = 40f;
float panSpeed;
float xThrow, zThrow;
Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Boost Pan Speed"))
{
panSpeed = boostPanSpeed;
}
else
{
panSpeed = normalPanSpeed;
}
zPlayerMove();
xPlayerMove();
}
private void zPlayerMove()
{
zThrow = Input.GetAxis("Vertical");
Debug.Log(zThrow);
rb.velocity = transform.forward * (zThrow * panSpeed);
}
private void xPlayerMove()
{
xThrow = Input.GetAxis("Horizontal");
Debug.Log(xThrow);
rb.velocity = transform.right * (xThrow * panSpeed);
}
}
capture.png
(49.0 kB)
Comment
Best Answer
Answer by blimp47 · Oct 17, 2020 at 02:05 PM
Ok, so i discovered that if i swap the playerMove calls round then the the vert works and horz stops working. I added an if statement ensuring one function would stop if the other needed to work. Movement now works as desired.
void Update()
{
if (Input.GetButton("Boost Pan Speed"))
{
panSpeed = boostPanSpeed;
}
else
{
panSpeed = normalPanSpeed;
}
if (Input.GetButton("Horizontal"))
{
xPlayerMove();
}
else
zPlayerMove();
}