[英]Collision and direction in Unity C#…?
我正在使用此代碼向正確的方向移動:
transform.Translate(1,0,0);
然后,我使用它向相反方向移動,我需要將x軸更改為正/負。
void OnCollisionEnter2D(Collision2D col){
}
幫我實現它。...在此先感謝。
這是給像我這樣的新手使用的,對我來說有用!
using UnityEngine;
using System.Collections;
public class CollisionAndtheMovement : MonoBehaviour {
public float speed;
public bool toRight;`enter code here`
// Use this for initialization
void Start () {
toRight = true;
}
// Update is called once per frame
void Update (){
if (toRight) {
transform.Translate (Vector2.right*Time.deltaTime*speed, 0);
}
if (!toRight) {
transform.Translate (Vector2.left*Time.deltaTime*speed, 0);
}
}
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "Crate" && toRight) {
toRight = false;
} else {
toRight = true;
}
}
}
您可以將transform.Right
用於此目的,也可以用於相反方向-transform.Right
可以解決問題。
要相反,只需將數字設為負數即可。
void OnCollisionEnter2D(Collision2D col){
transform.Translate(-1,0,0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.