- Home /
Please, I need help with Raycasting!
What i'm trying to do is get the mouse position, and do this: If mouse position (FPS crosshair) is in the terrain, if I click left mouse button, it makes a little "hole" in the terrain, not a real hole, just, make a crate, a small crate.
Answer by kag359six · Aug 01, 2012 at 11:23 PM
well for the raycast thing i would do this:
var hit : RaycastHit; //collision detection variable
if(Physics.RayCast(transform.position, transform.forward, hit, 10) { //create ray
if(hit.collider.gameObject.tag == "terrain") { //if collider we aim at has tag terrain
if(Input.GetMouseButtonDown(0)) { //if we hit the left mouse button
//do some action
}
}
}
But I'm not sure about the terrain thing. To me it sounds like a complicated task since you are editing geometry during gameplay, and I don't see how you can accomplish that without some knowledge on modifying meshes through code. Or maybe there's some way to access the terrain tools..?
Answer by Ingen · Aug 01, 2012 at 11:00 PM
hi I think this can help you
// put this in the camera,
var Range : float = 2; // distance of the ray from player, don't want the player piercing anywhere
public var ClicktAudio : AudioClip;
function Update (){
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward * Range);
// Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
if(Physics.Raycast(transform.position, DirectionRay, Hit, Range))
{
Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
if(Hit.collider.CompareTag ("terrain"))
{
if(/*Input.GetButtonDown ("Fire1")*/ Input.GetKeyDown(KeyCode.R))// or any key you wont
{
//DO THE HOLE
}
}
}
}
Is just to make a small crate at the crosshair position.
Umm sorry, wrong word, not a crate, i want to make it dig a hole, yes, a hole, sorry, but a small hole.
Ha! ha! :) no problem,
I'm also interest in to, take a look at this
http://answers.unity3d.com/questions/294572/modify-terrain-by-player.html
if some one answer
but maybe you can instantiate something like a hole mole on the terrain,
Answer by midomido · Aug 01, 2012 at 11:09 PM
function Update () {
if (Input.GetButtonDown ("Fire1")) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
Instantiate(crate, hit.point, Quaternion.identity);
//hit.point contain vector3 of collision location.
}
}
}
no, crate is a prefab in this case. You are saying crate, but dont you mean a hole? You are confusing everybody by saying crate lol
Answer by kag359six · Aug 02, 2012 at 06:35 PM
You are trying to do something that's not so simple, but I found this for you:
Here's an article on modifying terrain heights, perhaps a hole as you say
Your answer
Follow this Question
Related Questions
When editing Terrain brush does not fall on cursor 1 Answer
Make a simple tree 1 Answer
Mouse Aim for a 2.5D game 1 Answer
Character Customization Problem, Hard to explain... JS 0 Answers
How To make a digging system 1 Answer