unity 移动持续,Unity3D角色连续移动问题

so I'm working on a project in Unity3D and I'm having the following issue:

When I initially start the game, the character is not moving, which is intended. Then, when I hit "W" to move, my character starts moving and animates. However, when I let off the key, she doesn't stop moving forward.

Even if I hit the "S" key to move backward, after I let up, she starts moving forward again while no keys are pressed, and for the life of me I can't figure out why.

Here's the script I'm using on her if that helps:

using UnityEngine;

using System.Collections;

public class PlayerScript : MonoBehaviour

{

private CharacterController controller;

public float speed = 20.0f;

private Vector3 moveDirection = Vector3.zero;

public float gravity = 20.0f;

private bool winState = false;

private bool loseState = false;

private bool isBlocking = false;

private Animator anim;

// Use this for initialization

void Start ()

{

controller = this.GetComponent();

anim = GetComponent();

anim.SetBool("Ready_Bool", true);

}

// Update is called once per frame

void FixedUpdate ()

{

//GameObject center = GameObject.Find("MiddleOfRing");

GameObject opponent = GameObject.Find("Opponent");

checkAnimations();

float turn = Input.GetAxis("Horizontal");

//print("Horizontal: " + turn);

if(turn < 0)

moveLeft();

else if(turn > 0)

moveRight();

float vertDirection = Input.GetAxis("Vertical");

//print ("Vertical: " + vertDirection);

print ("Controller Velocity: " + controller.velocity.magnitude); //for testing

if(controller.isGrounded)

{

moveDirection = transform.forward * vertDirection * speed;

anim.SetFloat("Speed", controller.velocity.magnitude);

}

if(vertDirection > 0)

moveForward(moveDirection);

else if(vertDirection < 0)

moveBackward(moveDirection);

else

controller.Move(moveDirection * Time.deltaTime);

moveDirection.y = moveDirection.y - gravity * Time.deltaTime;

transform.LookAt(opponent.transform.position);

opponent.transform.LookAt(this.transform.position);

}

void moveLeft()

{

GameObject opponent = GameObject.Find("Opponent");

transform.RotateAround(opponent.gameObject.transform.position, Vector3.up, speed/2 * Time.deltaTime);

//for testing purposes, to be replaced with actual AI

opponent.transform.RotateAround(transform.position, Vector3.up, speed/2 * Time.deltaTime);

//tells Animator character is moving left

anim.SetBool("MoveLeft_Bool", true);

anim.SetBool("MoveRight_Bool", false);

anim.SetBool("MoveForward_Bool", false);

anim.SetBool("MoveBackward_Bool", false);

}

void moveRight()

{

GameObject opponent = GameObject.Find("Opponent");

transform.RotateAround(opponent.gameObject.transform.position, Vector3.down, speed/2 * Time.deltaTime);

//for testing purposes, to be replaced with actual AI

opponent.transform.RotateAround(transform.position, Vector3.down, speed/2 * Time.deltaTime);

//tells Animator character is moving right

anim.SetBool("MoveRight_Bool", true);

anim.SetBool("MoveLeft_Bool", false);

anim.SetBool("MoveForward_Bool", false);

anim.SetBool("MoveBackward_Bool", false);

}

void moveForward(Vector3 move)

{

GameObject opponent = GameObject.Find("Opponent");

float distance = Vector3.Distance(this.transform.position, opponent.transform.position);

//keeps characters at a certain distance from each other

if(distance >= 3)

{

controller.Move(move * Time.deltaTime);

}

//tells Animator character is moving forward

anim.SetBool("MoveForward_Bool", true);

anim.SetBool("MoveRight_Bool", false);

anim.SetBool("MoveLeft_Bool", false);

anim.SetBool("MoveBackward_Bool", false);

}

void moveBackward(Vector3 move)

{

GameObject opponent = GameObject.Find("Opponent");

moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;

controller.Move(move * Time.deltaTime);

//tells Animator character is moving backward

anim.SetBool("MoveBackward_Bool", true);

anim.SetBool("MoveRight_Bool", false);

anim.SetBool("MoveForward_Bool", false);

anim.SetBool("MoveLeft_Bool", false);

}

void checkAnimations()

{

AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);

if(Input.GetKeyDown(KeyCode.E))

{

anim.SetTrigger("JabR_Trig");

checkHit();

isBlocking = false;

}

else if(Input.GetKeyDown(KeyCode.Q))

{

anim.SetTrigger("JabL_Trig");

checkHit();

isBlocking = false;

}

else if(Input.GetKey(KeyCode.B))

{

anim.SetTrigger("Block_Trig");

isBlocking = true;

}

else

{

isBlocking = false;

}

}

void checkHit()

{

GameObject opponent = GameObject.Find("Opponent");

float distance = Vector3.Distance(this.transform.position, opponent.transform.position);

if(distance <= 4.0f)

{

}

}

public bool getBlocking()

{

return isBlocking;

}

}

I think the problem may be that I'm using controller.velocity.magnitude incorrectly.

If anyone can help me out, I would appreciate it!

解决方案

I actually figured it out. My animator didn't have one of the transitions set up, so the character was stuck in an animation.

Thanks!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值