- Home /
Ball Dropping, once hit make it go diagonal
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.
Comment
Best Answer
Answer by lhk · Oct 30, 2010 at 08:33 AM
There certainly are many solutions. This is how I would do it.
- Add a collider to the ball
- Add a trigger to the paddle
- Add the script ballcontrol to the ball
- Add the script paddlecontrol to the paddle
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);
}
}
}