Hey, I’m fairly new with Unity, and I have some experience in C#. But basically I am making a game where you spawn blocks that float on water (using the floater script). That is working fine, but what I don’t know how to do Is spawn my floating cube prefab where I click (I know it involves using raycast, but I don’t know how to go about it. I’m using the FPS walker script that comes with the basic assets.
Also, how would I implement some buoyancy to the basic FPS walker script? I can do it to objects, but not to the actual player. Thanks heaps in advance
if (Input.GetMouseButton(0))
{
var ray: Ray = mapCamera.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit))
{
if (hit.transform == target1) //target i defined as a transform variable but you could just put object here
{
GameObject.Find("Cube").transform.position = hit.point;
}
}
}
I just did this. Hope it helps.
note mapCamera would probably be mainCamera for you.
Thanks, so would I attatch that to what? The end of the fps walker script? or just as a script on its own as a component of the camera?
Put it where it makes logical sence.
I have a blank game object i rename code and attach stuff to that I don’t have anywhere else to attach it.
Hey, I finally got it working without errors, with this code ( a little modified to work with the dragridgidbody script that’s in the standard assets).I put it just before the end and nothing happens. I don’t know what to do. The aboveWater thing is the water plane my water material is applied to.
var spring = 50.0;
var damper = 5.0;
var drag = 10.0;
var angularDrag = 5.0;
var distance = 0.2;
var attachToCenterOfMass = false;
private var springJoint : SpringJoint;
function Update ()
{
// Make sure the user pressed the mouse down
if (!Input.GetMouseButtonDown (0))
return;
var mainCamera = FindCamera();
// We need to actually hit an object
var hit : RaycastHit;
if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 100))
return;
// We need to hit a rigidbody that is not kinematic
if (!hit.rigidbody || hit.rigidbody.isKinematic)
return;
if (!springJoint)
{
var go = new GameObject("Rigidbody dragger");
body = go.AddComponent ("Rigidbody");
springJoint = go.AddComponent ("SpringJoint");
body.isKinematic = true;
}
springJoint.transform.position = hit.point;
if (attachToCenterOfMass)
{
var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
anchor = springJoint.transform.InverseTransformPoint(anchor);
springJoint.anchor = anchor;
}
else
{
springJoint.anchor = Vector3.zero;
}
springJoint.spring = spring;
springJoint.damper = damper;
springJoint.maxDistance = distance;
springJoint.connectedBody = hit.rigidbody;
StartCoroutine ("DragObject", hit.distance);
}
function DragObject (distance : float)
{
var oldDrag = springJoint.connectedBody.drag;
var oldAngularDrag = springJoint.connectedBody.angularDrag;
springJoint.connectedBody.drag = drag;
springJoint.connectedBody.angularDrag = angularDrag;
var mainCamera = FindCamera();
while (Input.GetMouseButton (0))
{
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
springJoint.transform.position = ray.GetPoint(distance);
yield;
}
if (springJoint.connectedBody)
{
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
}
}
if (Input.GetMouseButton(0))
{ var mainCamera = FindCamera();
var ray: Ray = mainCamera.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit))
{
if (hit.transform == GameObject.Find("spawnCube") ) //target i defined as a transform variable but you could just put object here
{
GameObject.Find("Abovewater").transform.position = hit.point;
}
}
}
function FindCamera ()
{
if (camera)
return camera;
else
return Camera.main;
}
bump Sorry, I really need help.
bump. Please? Anyone? haha