Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kievar1983 · Dec 27, 2011 at 11:58 AM · c#terrainobjects

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges