- Home /
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!");
}
}
}
}
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!! :)