- Home /
Throw stuff! Instantiate preFab from game object to raycast point, at least I'd like to.
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
Answer by Julien-Lynge · Dec 21, 2011 at 11:20 PM
One option would be to use a library for path generation, given the start point, end point (hit.point), and what kind of trajectory you'd like. I'd highly suggest looking into iTween, which is free:
Yeah itween looks like one option, there's so many different ways to achieve some thing. I'm going to invest some time in itween and see where it takes me. $$anonymous$$aybe it might be simpler than fumbling through coding. But at the same time I'd like to find a solution to the code that I've put together, it looks like I'm missing just a few parts of the puzzle.
Thanks for you advice.
It certainly wouldn't hurt to continue playing with physics. There are some good tutorials out there, for instance 3DBuzz (http://www.3dbuzz.com/vbforum/sv_videonav.php?fid=504655656a7ab401656d5c2bed758fa1). However, in this case, where you know the path you want to take and where you want to end up, physics is probably more trouble than it's worth, and I'm a fan of not reinventing the wheel.
Hi Julien, excuse the late reply.
$$anonymous$$any thanks for pointing out iTween. I've been fumbling through the itween examples that I purchased from pixel placement.
Actually the Accurate Lob example is a close as it gets to what I'd like to achieve.
Although getting use to iTween is throwing up as many challenges as working out the original problem.
The iTween documentation seem comprehensive but not for a beginner. It be nice to see some example code for each of the functions, like the unity scripting reference. Suppose that would defeat the purpose of purchasing the examples. Even some comments in the example scripts would be useful.
Do you have any pointers or sites that might help with this?
$$anonymous$$ind Regards
Iain
Iain,
There are a number of tutorials out there, though I don't have any in particular to recommend. A quick google search for 'iTween tutorial' led me to this one:
http://www.youtube.com/watch?v=qE5hpp4YaH4
which explains a bit how to set up an iTween event in code and add properties (a list of which you can find in the documentation: http://itween.pixelplacement.com/documentation.php#$$anonymous$$oveTo)
Good luck.