Hello…
I’m trying to make an Arkanoid-type paddle with a rigidbody on a configurable joint follow the mouse cursor, while conforming to the surface of my play area.
I’m doing this so that the user can swat at the ball at different speeds and have the physics play nice.
I cannibalized some script that I used for an earlier project to show a targeting reticle, but this just won’t cooperate.
Here is the script I’m trying to use:
[var range : float = 15;
var minTargetElevation : float = -15;
var maxTargetElevation : float = 15;
var initialRotation: Quaternion;
var initialPosition: Vector3;
private var hit : RaycastHit;
public var layerMask : LayerMask;
public var target : Vector3;
function Awake()
{
initialRotation = transform.rotation;
initialPosition = transform.position;
}
function Update()
{
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, hit, range, layerMask))
{
target = hit.point;
Debug.Log (hit);
Debug.DrawLine (ray.origin, target);
}
else
{
// this creates a horizontal plane passing through this object's center
var plane = Plane(transform.position, Vector3.up);
// create a ray from the mousePosition
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// plane.Raycast returns the distance from the ray start to the hit point
var distance: float = range;
if (plane.Raycast(ray, distance))
{
// some point of the plane was hit - get its coordinates
target = ray.GetPoint(distance);
}
}
if (target.y > maxTargetElevation || target.y < minTargetElevation)
{
target.y = 00;
}
Debug.Log(target);
Debug.Log(transform.rotation);
this.GetComponent(ConfigurableJoint).targetPosition = target;
Debug.Log(this.GetComponent(ConfigurableJoint).targetPosition);
}]
I have the layer mask set to “Everything”. The script is attached to a gameObject with a rigidbody and configurable joint, with the paddle being a sub-object with a mesh collider and no rigidbody.
It’s moving, but not as predicted. It’s kind of acting like it’s moving around a reverse pivot at the 0,0,0 location? I’ve attached a project file if anyone feels brave enough to look at it…
Thank you! Test File