- Home /
Hit an object with other object and count hits
First person controller will hit stupms with a ball, if the ball hits the stumps then the stupms will be dismanteled and hits are recorded in count variable.
I am new to unity3d .Please help
Regards M.Qayyum
Edit: I'm able to fire a ball from First Person Controller and hit this cube but i can't able to detect that if cube is hit by ball or not.
Here is my script
public var ballPrefab : Transform;
public var ballSpeed : float = 1000;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
if(!ballPrefab || !ballSpeed)
{
Debug.Log("[Shoot] 'ballPrefab' or 'ballSpeed' is undefined");
}
else
{
var ballCreate=Instantiate(ballPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity);
ballCreate.rigidbody.AddForce(transform.forward * ballSpeed);
}
}
}
We can't write your game for you, we can help with specific problems. If you are really new to Unity it would be a good idea to do one of the tutorials which will really help with your understanding of what is possible.
Do you already have the code that makes the stumps dismantle etc? If so and you just want the counting then I'm sure we can help :)
Answer by whydoidoit · Jun 17, 2012 at 10:42 AM
So the ball needs a sphere collider and the cube needs a box collider. If the cube also has a rigidbody it will automatically react to being hit. tag the ball with a new tag of "ball" and the stumps with "stumps" in the inspector.
To see the collision yourself write a function :
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.tag == "ball"){
//do something like increment your counter
}
}
That would be attached to the stumps, if you want to attach to the ball change "ball" to "stumps".
@whydoidoit Thanks $$anonymous$$ike Now cube is reacting after collision but i have write this line in above code Debug.Log("ABC"); and it is not detecting any collision. And i have a ballPrefab which is tagged as "ball" and i have attached this code with cube.
So firstly lets just check I haven't screwed something up for you. Can you do a Debug.Log right at the top of OnCollisionEnter? See if you are every getting that?
Answer by 1337GameDev · Jun 16, 2012 at 04:37 PM
Well use a first person controller script (included with unity) and then make an action for when you press the button to "fire" and then just do an collision check to see if something hits the stump (which is a prefab). Then in your script make the log dismantle when it is hit with a method call. We cannot write the game for you, but this should give some direction. I would start with video tutorials on how to set up a basic game to know the basics of unity before you delve into this. It is quite daunting. well good luck
Your answer
Follow this Question
Related Questions
swing to hit a target 1 Answer
Destroying object using his name and raycast 2 Answers
Raycast Never Hits Anything?! 2 Answers
finding vertices by distance to hit point 1 Answer
Is object at least partly visible? 1 Answer