x


getting an object to stick to terrain in c#

I'm working with a turret and i want to add in a target ring that lets you know where you are going to be shooting. I can get the turret to rotate and move guns back and forth. I've added the target ring and can get it to rotate and move back and forth. but it gets stuck in like ditches and gulleys I've made.

I've made a simple cylinder for the target area and attached a rigid body and locked constraints on it except where i want it to rotate and move.

I've given it gravity as well. I'm not sure how to code this part for making it go up the hills on the little paths I've made.

this is the code i have so far split into two scripts. one rotates the entire turret the other just moves the ring back and forth along the Z axis.

shotArea.cs (moves back and forth)

    using UnityEngine;
using System.Collections;

public class shotAreaBehave : MonoBehaviour {

    public float rotateRate = Mathf.PI /6; // 30 degrees per second
    public bool rotate = false;
    public Transform ShotArea;
    // Use this for initialization
    void Start () {
       ShotArea = transform.Find("ShotArea");
       rotate = false;

    }

    // Update is called once per frame
    void Update () {

       if(Input.GetKeyDown(KeyCode.W))
       {
         rotate = true;
       }
       if (Input.GetKeyDown(KeyCode.S))
       {
         rotate = true;
       }
       if (rotate)
       {
         if  (Input.GetKey(KeyCode.S))
         {
          ShotArea.position = ShotArea.position + new Vector3 (0, 0, rotateRate);


         }
         else if  (Input.GetKey(KeyCode.W))
         {
          ShotArea.position = ShotArea.position - new Vector3 (0, 0, rotateRate);
         }
    }
}
}

rotation script:

using UnityEngine; using System.Collections;

public class turretbehave : MonoBehaviour {

public Transform Shot; public float rotateRate = Mathf.PI /6; // 30 degrees per second public bool rotate = false; public GameObject turret;

// Use this for initialization
void Start () {
turret = GameObject.Find("Player");

    rotate = false;
}

// Update is called once per frame
void Update () {

    if(Input.GetKeyDown(KeyCode.D))
    {
       rotate = true;
    }
    if (Input.GetKeyDown(KeyCode.A))
    {
       rotate = true;
    }
    if (rotate)
    {
       if (Input.GetKey(KeyCode.D))
       {
         turret.transform.RotateAroundLocal(Vector3.up, (rotateRate * Time.deltaTime));


       }
       else if  (Input.GetKey(KeyCode.A))
       {
         turret.transform.RotateAroundLocal(Vector3.up, (rotateRate * Time.deltaTime) * -1f);

       }
       else
       {
         rotate = false;
       }
    }
}

}

thanks for any help.

more ▼

asked Dec 27 '11 at 11:58 AM

kievar1983 gravatar image

kievar1983
66 9 11 15

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

First of all, if your goal is simply to project a marker onto a shape (your terrain), why not use a projector? You could move the projector around at a set height and the projected marker would perfectly follow the terrain.

If for some reason you absolutely have to have an object follow the terrain, physics is definitely not the way to go. Physics is for simulations, where you don't know the outcome and want Unity to figure out the math. When you do know the outcome, as in your case ("I want the marker to be at this X and Z and whatever the height of the terrain is there as the Y"), you're much better off setting the position yourself. The easiest way to figure out the height is to use raycasting and point the ray straight down (e.g. (0, -1, 0)).

more ▼

answered Dec 27 '11 at 04:24 PM

Julien.Lynge gravatar image

Julien.Lynge
7.3k 20 25 49

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5059
x4142
x1467
x321

asked: Dec 27 '11 at 11:58 AM

Seen: 781 times

Last Updated: Dec 27 '11 at 04:24 PM