- Home /
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.
Answer by Julien-Lynge · Dec 27, 2011 at 04:24 PM
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)).
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Why _terrainData.SetHeights() resizes the terrain? 0 Answers
Instantiating a object in a certain position.. 1 Answer
[C#] Multiple Terrains && Loading "Random" Height Maps from Directory 0 Answers