I'm using this code to move to right direction:
transform.Translate(1,0,0);
Then I'm using this to move to opposite direction, I need to change x axis positive/negative.
void OnCollisionEnter2D(Collision2D col){
}
Help me to make it happen.... Thanks in advance.
This Is for newbie like me, it's work for me!
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
可以解决问题。
To go the opposite direction, just make the number negative.
void OnCollisionEnter2D(Collision2D col){
transform.Translate(-1,0,0);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.