Hi there.
I’m trying to create a 2.5d game
The player can throw strawberries from a game object, x = right, y = up, z=away.
I’ve created a plane prefab which is textured with the strawberry image.
The plane has been rotated so that it faces the player, standing up, rather flat. x = right, y = up, z=away
I’m using a raycast, which I can instantiate a prefab at the raycast detection point. Magic!
But, I’d like to instantiate the prefab from a game object to be projected at the raycast detection point.
So I’d like to throw it from the game object.
I’ve butchered a few scripts and attempted to modify the script to my needs with no joy
Can anyone point me in the right direction on how I might achieve this?
Here is my script so far, please excuse my rambling comments they’re there to help me remember what I’m trying to do.
//Use a raycast to draw line to mouse click position... I think
///*
//var rayCastPlane : Transform;
var preFab : Transform;
//var spawnpoint : Transform;
//var speed =1000;
function Update () {
if (!Input.GetMouseButtonUp (0))
return;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 200)) {
Debug.DrawLine (ray.origin, hit.point, Color.green);
//var rot = Quaternion.FromToRotation(Vector3.up, -transform.forward); //this can be used in the below line to rotate the preFab correctly, replace from Quaternion
//Instantiate(preFab, hit.point, Quaternion.LookRotation(hit.normal));
Instantiate(preFab, hit.point, Quaternion.FromToRotation(Vector3.up, -transform.forward));
// Ok now I want to instantiate the preFab from the gameobject to the Raycast Target
//and Add force
//is the answer in here somewhere???
//Instantiate(lookpoint, hit.point, Quaternion.identity); // I don't understand what the lookpoint is supposed to be...
//Instantiate(preFab, spawnpoint.position, Quaternion.identity);
//preFab.transform.LookAt(lookpoint);
//preFab.rigidbody.AddRelativeForce (Vector3.forward * speed);
//transform.position.x = hit.point.x; //This will move the game object to the mousePosition
//transform.position.z = hit.point.z; //This will move the game object to the mousePosition
}
}
//*/
If the comments in this code are a bit messy to read I can repost, again never mind the ramblings
Kind Regards
Iain