Hello, I’m making a small game and I’ve a problem. I have a unity sphere that is always moving forward. When it his a wall, i’d like it to rotate 90° and keep going forward in that new direction. Being a total beginner in Unity and C#, I’m a bit stuck.
The ball rolls without problem. When it his the wall, i can see the ball rotating but it doesn’t change direction and keeps hitting the wall.
Can someone tell me what is wrong in my code and maybe give me a hint how to fix it? Thank you.
using UnityEngine;
using System.Collections;
public class Boulder : MonoBehaviour {
public float ballSpeed = 1;
private Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
void Update () {
transform.Rotate (1, 0, 0); //
rb.AddForce (0, 0, ballSpeed);
}
void OnCollisionEnter(Collision col){
if (col.gameObject.tag != "Player"){
rb.transform.Rotate (90,0,0);
Debug.Log (col.gameObject.name);
}
}
}