Make the RigidBody detect sensitivity of the x box controller axises?
Here is my script, it works great for keyboard movement but if you use a xbox controller with it the player moves at a constant speed at all times. You would want him to go at a walking pace if the axis is tilted by 25% running speed at 50%. Right now if the axis is tilted just one degree the character moves at full speed which even makes him move when you're not touching the stick at all because of how sensitive it is. How would I modify this code to be sensitive to the axises on the x box controller?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class scr : MonoBehaviour {
bool canPlayerMove = true;
Rigidbody rb;
public float speed = 5f;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = -Input.GetAxisRaw("LeftJoyStickHorizontal");
float moveVertical = Input.GetAxisRaw("LeftJoyStickVertical");
if (canPlayerMove)
{
Vector3 movement = new Vector3(moveVertical * Time.deltaTime, 0.0f, moveHorizontal);
movement.Normalize();
if (movement != Vector3.zero)
{
rb.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15f);
rb.MovePosition(transform.position + transform.forward * speed * Time.fixedDeltaTime);
}
}
}
}
Answer by rodrybest · May 08, 2018 at 11:20 PM
You are not using your movement vector in the MovePosition function. Instead, you are moving always at the same speed with transform.forward.
Changing transform.forward with movement vector should do the trick.
Best regards, Rodri
Thanks but that didn't seem to do it, still continuously moving even if the x box stick is only tilted to 1 degree. I think more is required, or I'm missing something. Works fine with the keys since if you don't press the button the horizontal vertical axes are 0. But on a xbox controller they're always going to be something, or very sensitive. Perhaps you need to give the character a velocity, I'm very new to this so any hints are much appreciated. ;)
If you comment the line movement.Normalize()
changes something when you move the player with the joystick?
Another thing that I don't undestand is why are you using Time.deltaTime in the x component off youre movement vector. You are applying the Time.fixedDeltaTime after in the $$anonymous$$ovePosition, so Time.deltaTime is not needed here.
Rodri
@rodrybest Thanks for the heads up on the Time.deltaTime. No commenting out that line does nothing.
If you care to bother you can put the script on a cube and add a rigid body to it. Just have to change LeftStickHorizontal and LeftStickVertical for Horizontal and Vertical. It probably has something to do with transform.forward. That's 1 and doesn't take into account the axises. Changing that to movement does nothing however, hmmm not sure how to resolve this. :/
FIGURED THAT OUT TOO, change the dead zone in the input manager.
@rodrybest Wait I figured it out, had to remove normalize and change the forward for movement. Now he moves around correctly, still an issue though, the rate at which he rotates and the rate at which he moves is very sensitive. A slight tilt to the right is going to make him face right. Is there a way to restrict is so if only the stick is tilted a certain amount of degrees the character starts moving or rotating?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class scr : $$anonymous$$onoBehaviour {
bool canPlayer$$anonymous$$ove = true;
Rigidbody rb;
public float speed = 5f;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = -Input.GetAxisRaw("LeftJoyStickHorizontal");
float moveVertical = Input.GetAxisRaw("LeftJoyStickVertical");
Debug.Log(moveHorizontal + moveVertical);
if (canPlayer$$anonymous$$ove)
{
Vector3 movement = new Vector3(moveVertical, 0.0f, moveHorizontal);
if (movement != Vector3.zero)
{
rb.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15f);
rb.$$anonymous$$ovePosition(transform.position + movement * speed * Time.fixedDeltaTime);
}
}
}
}