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.