Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

How to Rotate/Translate a GameObject via C# Script

$
0
0
So, I'm writing a script to move my gameObject (basically a submarine). I'm collecting the x/z coordinates as well as my rotation from a text file. In my code, I convert the lat, long and heading coordinates to meter distances and change in rotations. using UnityEngine; using UnityEngine.UI; using System.Text; using System.IO; using System.Collections; using System; public class PlayerController: MonoBehaviour { public GameObject remus; private Vector3 position; public static void Main (string[] args) { float speed = 1.0f; //Import text file and read all. string text = System.IO.File.ReadAllText("/Applications/Unity/Projects/Underwater/SampleFile.txt"); if (text != null) { //Split the string of text into an array containing each number in the file. string[] strings = text.Split (new[] { " ", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); //Convert each string number to a double. double[] nums = new double[strings.Length]; for (int i = 0; i < strings.Length; i++) { nums [i] = Convert.ToDouble (strings [i]); } //Common variables double R = 6371000; int Entries = 86; //Determine the Z distance travelled using the latitudes double zDistances; float zMeters; for (int i = 0; i < Entries; i += 3) { zDistances = nums [i + 3] - nums [i]; zMeters = Convert.ToSingle(zDistances * R * Mathf.PI / 180); transform.Translate (0, 0, zMeters); } //Determine the X distances using the longitudes double xDistances; double lat; float xMeters; for (int i = 1; i < Entries; i += 3) { xDistances = nums [i + 3] - nums [i]; lat = nums[i-1]; xMeters = Convert.ToSingle(R * (xDistances) * (Mathf.PI / 180) * Math.Cos(lat)); transform.Translate (xMeters, 0, 0); } //Determine change in heading float rotation; for (int i = 2; i < Entries; i += 3) { rotation = Convert.ToSingle(nums [i + 3] - nums [i]); transform.Rotate(0, rotation, 0); } } } My issue occurs with the 3 "transform. " in each if statement. I am getting this error message: > /Applications/Unity/Projects/Underwater/Assets/Scripts/PlayerController.cs(5,5): Error CS0120: An object reference is required to access non-static member `UnityEngine.Component.transform' (CS0120) (Assembly-CSharp) and I've checked out so many other questions that ask about this error and none of them seem to help my issue. I have tried using capital and lowercase letters, .rotation and .translation, and I still get them same error. I'm wondering what I'm doing wrong or if there is a better way to go about it. Thanks in advance!

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>