我在互联网上找到了我的问题代码,但我需要用C#,而不是UnityScript。我试过,但我不知道如何解决一些问题。我几乎翻译了所有内容。
原件:
#pragma strict
var prefab : GameObject;
function Start () {
spawn();
}
function Update () {
}
function GetPointOnMesh() : RaycastHit {
var length : float = 100.0;
var direction : Vector3 = Random.onUnitSphere;
var ray : Ray = Ray(transform.position + direction*length,-direction);
var hit : RaycastHit;
GetComponent.<Collider>().Raycast (ray, hit, length*2);
return hit;
}
function spawn() {
var randomPoint = GetPointOnMesh();
var spawnPreferences = Instantiate(prefab, randomPoint.point, Quaternion.identity);
spawnPreferences.transform.eulerAngles.y = Random.Range(0, 360);
}
半翻译为C#:
using UnityEngine;
using System.Collections;
public class ResourceGenerator : MonoBehaviour
{
GameObject prefab;
void Start()
{
spawn();
}
RaycastHit GetPointOnMesh()
{
float length = 100.0f;
Vector3 direction = Random.onUnitSphere;
Ray ray = new Ray(transform.position + direction * length, -direction);
RaycastHit hit;
GetComponent<Collider>().Raycast(ray, out hit, length * 2);
return hit;
}
void spawn()
{
var randomPoint = GetPointOnMesh();
var spawnPreferences = Instantiate(prefab, randomPoint.point, Quaternion.identity);
spawnPreferences.transform.eulerAngles.y = Random.Range(0, 360);
}
}
我不知道变量类型需要什么而不是var,并且最后一行有一个错误“对象没有为”转换“定义。
答案 0 :(得分:2)
你试过吗?
void spawn()
{
RaycastHit randomPoint = GetPointOnMesh();
GameObject spawnPreferences= (GameObject)Instantiate(prefab, randomPoint.point, Quaternion.identity);
spawnPreferences.transform.eulerAngles = new Vector3(0,Random.Range(0, 360),0);
}