Question by
zjellie1998 · Sep 27, 2016 at 03:36 PM ·
collision
Make Object Change to Opposite direction on collision
I made 2 scripts to make a Ball move to the oposite direction from where it is moving after a collision, it does sort of work, but it only changes direction after it stopped touching the wall.
using UnityEngine;
using System.Collections;
public class Ball_Movement : MonoBehaviour
{
public float Ball_Speed;
public Vector3 randomDirection;
// Use this for initialization
public void Start()
{
Vector3 randomDirection = new Vector3(Ball_Speed * Random.value * Time.deltaTime, 0f, Ball_Speed * Random.value * Time.deltaTime);
transform.Rotate(randomDirection);
Ball_Speed = 2f;
}
// Update is called once per frame
public void Update()
{
transform.Translate(Ball_Speed * Time.deltaTime, 0f, Ball_Speed * Time.deltaTime);
}
}
Which is the start of the game where the ball moves in a random direction
using UnityEngine;
using System.Collections;
public class Wall_Collision : MonoBehaviour
{
public float Ball_Speed;
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Right_Wall")
{
transform.Translate(Ball_Speed * -1 * Time.deltaTime, 0f, Ball_Speed * Time.deltaTime);
Ball_Speed = 2f;
}
}
}
And that is the second script.
Comment
Your answer
Follow this Question
Related Questions
How do you detect a mouse button click on a Game Object? C# 2 Answers
Destroying GameObject on collision Problems (Begginer) 1 Answer
OnCollisionEnter2D is called after object was shifted 1 Answer
orbiting mouse controlled camera goes through terrain 0 Answers
Deleting an object from a list of objects from another script. 1 Answer