I have two cubes that move back and forth. One moves on the x axis, one on the z axis. They move in different speeds and are bound to collide. Now I want them to change directions after collisions, so as to make them seem to bounce back from one another.
I have added Colliders to both, but they still seem to go through the other when they meet.
This is the script for Cube1, Cube2 is the same except it moves on the z axis.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveOnX : MonoBehaviour
{
public float speed = 2.5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = new Vector3(Mathf.PingPong(Time.time * speed, 5), transform.position.y, transform.position.z);
}
}
Edit:
I also tried:
private void OnTriggerEnter(Collider other)
{
speed = speed * -1;
}
But it’s not doing the trick