How would I go about achieviing a ball hitting a paddle and once it hits the paddle it goes in a diagonal way?
I have it up to hitting the paddle and boucing, but how do i go about making it go in a specific direction?
i'm in 2d land.
How would I go about achieviing a ball hitting a paddle and once it hits the paddle it goes in a diagonal way?
I have it up to hitting the paddle and boucing, but how do i go about making it go in a specific direction?
i'm in 2d land.
There certainly are many solutions. This is how I would do it.
ballcontrol:
using UnityEngine;
using System.Collections;
public class ballcontrol : MonoBehaviour
{
public Vector3 direction;
void Update ()
{
transform.Translate(direction*Time.deltaTime);
}
public void SetDirection(Vector3 newdirection)
{
direction=newdirection;
}
}
paddlecontrol
using UnityEngine;
using System.Collections;
public class paddlecontrol : MonoBehaviour
{
void Update()
{
//move the paddle
}
void OnTriggerenter(Collider other)
{
GameObject something=other.gameObject;
if(something.CompareTag("ball"))
{
Vector3 newdirection=Vector3.up;
/*calculate the balls new direction. For example like
* newdirection=new Vector3(0,10,transform.position.z-something.transform.position.z);
*/
something.SendMessage("SetDirection",newdirection);
}
}
}