Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by iaind78 · Dec 21, 2011 at 10:10 PM · instantiateraycastprefabthrow

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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:

http://itween.pixelplacement.com/gettingstarted.php

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iaind78 · Dec 22, 2011 at 09:36 AM 0
Share

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.

avatar image Julien-Lynge · Dec 22, 2011 at 08:19 PM 0
Share

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.

avatar image iaind78 · Jan 20, 2012 at 10:55 AM 0
Share

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

avatar image Julien-Lynge · Jan 30, 2012 at 06:00 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate a prefab and parenting it? 1 Answer

A way to instantiate an item with 90 Degree increments? 2 Answers

The prefab you want to instantiate is null. 4 Answers

Grid Placement In Game 2 Answers

Help~!! Raycast on an instantiated prefab can not work!! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges