- Home /
mouse look player controller and locomotions with if then else??
Hey
i am working on a mouse-look player controller. Movement and look at mouse position are working. I use the horizontal and vertical Axis input for movement and the bleendtree. LookAt is used for the Look at mouse position.
I started working on the player input and the locomotion of the character. I ended up with a lot of ifs and angles... well i think that's the wrong way to do this.
So my question is: is there a better way to bind the locomotion state on the player input and the model direction.
sorry for my bad English `
void Update () {
//Get Player Input
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
//Get Rotation
playerRotation = spineBone.transform.eulerAngles.y;
Move (h, v);
Look ();
AnimationConrollter (h,v);
}
void Move (float h, float v)
{
movement.Set (h, 0.0f, v);
movement = movement * Speed * Time.deltaTime;
playerRigidbody.MovePosition (transform.position + movement);
}
void Look ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out dist))
{
lookAtTarget = ray.GetPoint(dist);
}
}
void AnimationConrollter(float _h, float _v)
{
if (playerRotation < 30.0f) {
anim.SetFloat ("v", _v);
anim.SetFloat ("h", _h);
}
if (playerRotation > 315.0f) {
anim.SetFloat ("v", _v);
anim.SetFloat ("h", _h);
}
if (playerRotation > 45.0f && playerRotation < 135.0f) {
anim.SetFloat ("v", _h);
anim.SetFloat ("h", _v);
}
if (playerRotation > 135.0f && playerRotation < 225.0f) {
anim.SetFloat ("v", -_v);
anim.SetFloat ("h", _h);
}
if (playerRotation > 225.0f && playerRotation < 315.0f) {
anim.SetFloat ("v", -_h);
anim.SetFloat ("h", _v);
}
if (playerRotation < 15.0f) {
Vector3 temp = new Vector3(0.0f,0.0f,0.0f);
transform.eulerAngles = temp;
}
if (playerRotation > 345.0f) {
Vector3 temp = new Vector3(0.0f,0.0f,0.0f);
transform.eulerAngles = temp;
}
if (playerRotation > 75.0f && playerRotation < 105.0f) {
Vector3 temp = new Vector3(0.0f,90.0f,0.0f);
transform.eulerAngles = temp;
}
if (playerRotation > 165.0f && playerRotation < 195.0f) {
Vector3 temp = new Vector3(0.0f,180.0f,0.0f);
transform.eulerAngles = temp;
}
if (playerRotation > 255.0f && playerRotation < 285.0f) {
Vector3 temp = new Vector3(0.0f,270.0f,0.0f);
transform.eulerAngles = temp;
}
}
[1]: /storage/temp/46438-animationcontroller.jpg
Answer by Gaming-Dudester · May 17, 2015 at 08:36 PM
Hey you might know this but if you don't there is a drop down toolbar in unity called assets click it then import package. Default packages are in there and one of them is first person player controller at the top.
Your answer
![](https://koobas.hobune.stream/wayback/20220613195709im_/https://answers.unity.com/themes/thub/images/avi.jpg)