Search Unity

raycast to find normal of terrain

Discussion in 'Scripting' started by TheRaider, Jan 22, 2011.

  1. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I am trying to get my character controller to move over the surface in such a way it rotates with the curves of a surface.

    I figure I need to raycast and then use that information to match the rotation with my controller?

    Is there a better way?


    Help please :D


    Im also starting to think a character controller might not of been the best idea. I am going to try the method of making a car (although I guess you could say i want mine to control more like a tank in the sense it doesn't slide or anything unless put to extremes (and i also want to when those extremes are met be able to create a death).
     
    Last edited: Jan 22, 2011
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No need for raycasting; use TerrainData.GetInterpolatedNormal. And no, a character controller won't work.

    --Eric
     
  3. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    well i guess i have to go down the using wheel colliders?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's one way, but you don't have to, depending on realism, etc.

    --Eric
     
  5. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    What other ways would you suggest?

    As far as realism goes, I am not looking for something totally realistic. Think for a golf buggy with 1 speed. Nothing crazy, always on the ground and not going fast enough to skid etc.

    Also I purchased your vectrosity and have a quick question (related to a sample not the product lol) in the one where you make all the spheres appear randomly how do you ensure none are overlapping? I looked at the code and it looked like it was somehow automatically doing it.

    I have been playing with the wheel colliders and can't seem to set it up in a way which more mimics the character controller (I basically want to set it up so it works similar to the character controller except rotates with the slope of the land.

    I also wondered if was possible to someway say have the character controller on the front wheels, then somehow many the chasis and back wheels a rigid body then lock it into the contoller to limit how it can react and just "drag" it around and it would automatically give the effect i am looking for. Althoughi have no idea how to do that or if it is possible. I tried attaching a rigid body but it floats further away as it hits things.
     
    Last edited: Jan 22, 2011
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Doesn't sound like you really need physics for that; just a box collider for collisions.

    I didn't bother to check at all actually...if any two spheres overlap when instantiated, the physics engine will take care of it. If I did want to check, I would use Physics.OverlapSphere.

    Nah, just do it procedurally. i.e., fake it. There's a post around somewhere about Off-Road Velociraptor Safari, where they say the vehicle is actually implemented basically as a hovercraft (the wheels are just for show), and there are a number of topics about hovercraft around.

    --Eric
     
  7. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    Thought I would post where I am up too.

    I am making 4 raycasts around the object which I use to find the angle and now it stays nicely lined up with the surface.

    However since I am manually changing the angle of the object it is a bit jerky. How would i fix that? I am thinking I need to use transform.rotate but how can i use it in terms of saying I want the object to rotation.z = certain value with a line like transform.rotation.eulerAngles.x = rotationAmountSurface;


    Also I am still using a character controller, but I don't like the shape of the collider. However I am unsure how to change to using a box collider in terms of moving it.

    Currently I used var controller : CharacterController = GetComponent(CharacterController); to define a controller and then controller.SimpleMove to move it forward transform.Rotate to rotate.
     
    Last edited: Jan 24, 2011
  8. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    bump..

    I think I need to dampen the camera, except I don't know how. I can't use smooth follow cause I need to be able to pan and tilt the camera.