Rigidbody

Something like Rigidbody may seem trivial for some of you, but for myself at the moment, it’s a nightmare. I’m attempting to code a gameObject (named “barrel”) in such a way that it is able to have a force applied via a Raycast.

Ray ray = Camera.main.ScreenPointToRay
													(new Vector3(x,y,0));
							RaycastHit hit=new RaycastHit();
							if (Physics.Raycast(ray,out hit)){
						
									if(hit.collider.gameObject.name=="barrel"){
									
											RaycastHit.Rigidbody.AddForceAtPosition(Vector3.Raycast.direction*10.0f);
											Debug.Log("Barrel was destroyed.");

This is how far I’ve gotten just now, with line 8 being what I’m confused about.

Cheers for any help put forth! For any backstory, I’ve only just started using Unity3d at university, and only just started coding C#. I’ve checked the scripting reference, but just cant seem to gain any insight into my problem.

Never mind! I’ve gotten it at last!

Vector3 dir = ray.direction;
											hit.rigidbody.AddForce(dir*1000.0f);

Messed around a little with the values that I had created within the loop, and finally got it. 1000 is a bit overkill, but it was just to test.

Try this:

using UnityEngine;
using System.Collections;

public class Zing : MonoBehaviour 
{

	void Update( )
	{
		Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
		
		RaycastHit hit;
		if( Physics.Raycast( ray, out hit ) )
		{
			Debug.Log( "Hit: " + hit.transform.name );
			if( hit.transform.name == "barrel" )
			{
				Rigidbody rb = hit.transform.GetComponent<Rigidbody>( );
				rb.AddForceAtPosition( ray.direction.normalized * 10.0f, hit.normal );
			}
		}
	}
}

If you want to ‘destroy’ the cube btw, look into GameObject.Destroy() - have fun!

I have a Destroy script communicating with this one, I’ve just commented it out. Thanks, though!