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 3 · Mar 17, 2013 at 10:09 PM · iosraycasttouchvelocitytranslate

How to push object along Z axis with Ray on touch?

So I want a script that when a specific object is touched, (the clone) it is pushed along the Z axis. Unfortunately, I can't get it to recognize a specific object and then give it velocity.

here is my script so far, doesn't work, besides casting a ray from touch position and detecting the object touched:

     private var hit : RaycastHit;
     private var ray : Ray;//ray we create when we touch the screen
       function FixedUpdate () {
         if(iPhoneInput.touchCount == 1) {
             ray = Camera.main.ScreenPointToRay(iPhoneInput.touches[0].position);
             Debug.DrawLine(ray.origin,ray.direction * 10);
             if(Physics.Raycast(ray.origin, ray.direction * 10,hit)){
      Debug.Log(hit.transform.name);//Object you touched
 //So right here is my problem, I need to find out which object was touched, and if its a clone of the ball (or any object I want), then it is pushed along the Z axis.
 
                             if(hit.transform.tag == "clone"){
                             clone.velocity = transform.TransformDirection (Vector3.forward * 10);
                             Debug.Log("By God, IT WORKED!");
                                              }
             }
         }
     } 
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
1
Best Answer

Answer by sparkzbarca · Mar 17, 2013 at 10:33 PM

tag isn't the name it's something you assign unless during your creation you assigned it a tag it wouldnt have that tag

you need to go back to the ball creation code and do something like

 public Gameobject ball; //in the editor your going to drag the prefab into this slot
 Gameobject BallClone
 void start(){
 BallClone =  Instantiate(ball,//position,//rotation) as GameObject;
 //now start making this clone noticable as a clone
 BallClone.name = "BallClone";
 BallClone.transform.tag = "Clone";

now your hit code should work

also

clone.velocity is wrong thats instant use add force and dont transform direction the forward is stored in the objects transform

use hit to move the object not clone. clone.velocity by the way is wierd it means clone is equal to a rigidbody which is just BIZARRE. nothing about the word clone says to me its a rigidbody. It seems it should be an entire game object.

 if(hit.transform.tag == "clone")
 hit.rigidbody.addforce(hit.transform.forward * 10);

mark as answerd and have a nice day!! :)

Comment
Add comment · Show 1 · 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 3 · Mar 17, 2013 at 11:46 PM 0
Share

Thanks a ton, saved me hours.

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Limit the position of the object, but still keep following the touch? How to use ClampMagnitude properly? 1 Answer

Raycast issue ... 1 Answer

Raycast Hit not working 0 Answers

Help With Touch to Drag Script 1 Answer

Dragging an object in iOS (UnityScript) 3 Answers


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