我对团结3d和c锐利全新。我正在设计一个球体正在追逐立方体的程序。我尝试过,但它显示出一些错误。
控制台出错:
Assets/chaserr.cs(8,40): error CS0019: Operator `-' cannot be applied to operands of type `UnityEngine.Transform' and `UnityEngine.Vector3'
Assets/chaserr.cs(11,38): error CS0019: Operator `+' cannot be applied to operands of type `UnityEngine.Vector3' and `float'
Assets/chaserr.cs(11,27): error CS1502: The best overloaded method match for `UnityEngine.Transform.Translate(UnityEngine.Vector3)' has some invalid arguments
Assets/chaserr.cs(11,27): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Vector3'
Chaserr.cs
using UnityEngine;
using System.Collections;
public class chaserr : MonoBehaviour {
public Transform target;
float speed = 8;
void Update () {
Vector3 displacement = target - transform.position;
Vector3 direction = displacement.normalized;
Vector3 velocity = direction * speed;
transform.Translate (velocity + Time.deltaTime);
}
}
move.cs
using UnityEngine;
using System.Collections;
public class move : MonoBehaviour {
float speed = 10;
void Update () {
Vector3 input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
Vector3 direction = input.normalized;
Vector3 velocity = direction * speed;
Vector3 moveAmount = velocity * Time.deltaTime;
transform.Translate (moveAmount);
}
}
如果您还想要其他任何其他信息,请发表评论..
答案 0 :(得分:1)
您可以使用Rigidbody(或2D游戏的Rigidbody2D)更改GameObject的速度。
首先将Rigidbody组件添加到GameObject中;
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Rigidbody rigid;
Vector3 yourVelocityVariable;
void Start() {
rigid = GetComponent<Rigidbody>();
}
void FixedUpdate() {
rigid.velocity = yourVelocityVariable;
}
}
答案 1 :(得分:0)
而不是
Vector3 displacement = target - transform.position;
使用
Vector3 displacement = target.position - transform.position;
和
而不是
velocity + Time.deltaTime
使用
velocity * Time.deltaTime