碰撞后改变玩家的控制?
Change the controls of a player after a collision?
所以基本上玩家的移动一切都很好。直到他撞到一个物体。我想改变玩家与某个物体碰撞时的动作。例如:Homer Simpson 运行转来转去,但当他碰到一个达夫时,他的动作会改变几秒钟。
到目前为止,这是我的代码:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 1.0f;
public int score = 0;
public bool gameOverRed = false;
public float size = 1f;
public float duffSpeed = 1.0f;
private float timer;
private bool hitDuff = false;
void start() {
timer = 0.0f;
}
// Update is called once per frame
void Update () {
if (hitDuff) {
timer += Time.deltaTime;
if (timer > 5.0) {
start ();
duffMovement ();
hitDuff = false;
}
} else {
movement ();
}
}
public void OnCollisionEnter2D(Collision2D coll)
{
if(coll.gameObject.CompareTag("Cube")) {
speed -= 2;
score++;
transform.localScale += new Vector3(size, size, 0);
}
if(coll.gameObject.CompareTag("Duff")){
hitDuff = true;
}
}
private void movement()
{
if (Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, 0));
}
if (Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, 0));
}
if (Input.GetKey (KeyCode.S)) {
transform.rigidbody2D.velocity = (new Vector2 (0, -speed));
}
if (Input.GetKey (KeyCode.W)) {
transform.rigidbody2D.velocity = (new Vector2 (0, speed));
}
if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, -speed));
}
if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, -speed));
}
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, speed));
}
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, speed));
}
if (speed <= 20) {
speed = 20;
}
}
private void duffMovement() {
if (Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, 0));
}
if (Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, 0));
}
if (Input.GetKey (KeyCode.S)) {
transform.rigidbody2D.velocity = (new Vector2 (0, speed));
}
if (Input.GetKey (KeyCode.W)) {
transform.rigidbody2D.velocity = (new Vector2 (0, -speed));
}
if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, speed));
}
if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, speed));
}
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, speed));
}
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, -speed));
}
}
}
当我 运行 时会发生什么,我击中了 duff,然后我的播放器一直朝这个方向移动(无法使用 duffMovement()
方法)几秒钟,然后我又可以了。
在您想要使用的时间段结束后,您正在执行达夫运动;
if (hitDuff) {
timer += Time.deltaTime;
duffMovement();
if (timer > 5.0) {
start ();
hitDuff = false;
}
}
所以基本上玩家的移动一切都很好。直到他撞到一个物体。我想改变玩家与某个物体碰撞时的动作。例如:Homer Simpson 运行转来转去,但当他碰到一个达夫时,他的动作会改变几秒钟。
到目前为止,这是我的代码:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 1.0f;
public int score = 0;
public bool gameOverRed = false;
public float size = 1f;
public float duffSpeed = 1.0f;
private float timer;
private bool hitDuff = false;
void start() {
timer = 0.0f;
}
// Update is called once per frame
void Update () {
if (hitDuff) {
timer += Time.deltaTime;
if (timer > 5.0) {
start ();
duffMovement ();
hitDuff = false;
}
} else {
movement ();
}
}
public void OnCollisionEnter2D(Collision2D coll)
{
if(coll.gameObject.CompareTag("Cube")) {
speed -= 2;
score++;
transform.localScale += new Vector3(size, size, 0);
}
if(coll.gameObject.CompareTag("Duff")){
hitDuff = true;
}
}
private void movement()
{
if (Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, 0));
}
if (Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, 0));
}
if (Input.GetKey (KeyCode.S)) {
transform.rigidbody2D.velocity = (new Vector2 (0, -speed));
}
if (Input.GetKey (KeyCode.W)) {
transform.rigidbody2D.velocity = (new Vector2 (0, speed));
}
if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, -speed));
}
if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, -speed));
}
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, speed));
}
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, speed));
}
if (speed <= 20) {
speed = 20;
}
}
private void duffMovement() {
if (Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, 0));
}
if (Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, 0));
}
if (Input.GetKey (KeyCode.S)) {
transform.rigidbody2D.velocity = (new Vector2 (0, speed));
}
if (Input.GetKey (KeyCode.W)) {
transform.rigidbody2D.velocity = (new Vector2 (0, -speed));
}
if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, speed));
}
if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, speed));
}
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.D)) {
transform.rigidbody2D.velocity = (new Vector2 (speed, speed));
}
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.A)) {
transform.rigidbody2D.velocity = (new Vector2 (-speed, -speed));
}
}
}
当我 运行 时会发生什么,我击中了 duff,然后我的播放器一直朝这个方向移动(无法使用 duffMovement()
方法)几秒钟,然后我又可以了。
在您想要使用的时间段结束后,您正在执行达夫运动;
if (hitDuff) {
timer += Time.deltaTime;
duffMovement();
if (timer > 5.0) {
start ();
hitDuff = false;
}
}