Unity3d 中的 Arrow GameObject 不会飞
Arrow GameObject in Unity3d won't fly
我在unity5中实现了一个弓箭脚本。但是箭并没有飞出去。有人可以建议为什么会这样。我是初学者。
这是弓脚本。
using UnityEngine;
using System.Collections;
public class Bow : MonoBehaviour {
private GameObject arrow = null;
private Vector3 fwd = Vector3.zero;
public GameObject arrowPrefab = null;
public int initialSpeed = 30;
public GameObject launchPosition = null;
public float waitTime = 0.1f;
public LayerMask layerMask;
// Use this for initialization
void Start ()
{
}
void FixedUpdate()
{
fwd = transform.TransformDirection(Vector3.forward);
if (!Physics.Raycast(transform.position, fwd, 1, layerMask))
{
if (Input.GetMouseButtonDown(0))
{
Fire();
}
}
}
private void Fire()
{
//yield return new WaitForSeconds(waitTime);
new WaitForSeconds(waitTime);
arrow = (GameObject)Instantiate(arrowPrefab, launchPosition.transform.position, launchPosition.transform.rotation);
}
}
这是箭头脚本
using UnityEngine;
using System.Collections;
public class Arrow : MonoBehaviour {
private Vector3 velocity = Vector3.zero;
private Vector3 newPos;
private Vector3 oldPos;
private bool hasHit = false;
private Vector3 direction;
private RaycastHit hit;
private GameObject follow;
private Vector3 dir;
private float dist;
public LayerMask layerMask;
public float speed;
public Transform arrowRotation;
public float forceToApply;
public float arrowGravity;
void Start()
{
newPos = transform.position;
oldPos = newPos;
velocity = speed * transform.forward;
}
void update()
{
if (hasHit)
{
transform.position = follow.transform.position;
transform.rotation = follow.transform.rotation;
return;
}
newPos += (velocity + direction) * Time.deltaTime;
dir = newPos - oldPos;
dist = dir.magnitude;
dir /= dist;
if(dist > 0)
{
if (Physics.Raycast(oldPos,dir,out hit,dist,layerMask))
{
newPos = hit.point;
if (hit.collider)
{
if (hit.rigidbody)
{
GameObject hitpoint = (GameObject)Instantiate(new GameObject(), hit.point, transform.root.rotation);
hitpoint.transform.parent = hit.transform;
follow = hitpoint;
GetComponent<Collider>().isTrigger = true;
hit.rigidbody.AddForceAtPosition(forceToApply * dir, hit.point);
hasHit = true;
}
else
{
enabled = false;
}
}
}
}
oldPos = transform.position;
transform.position = newPos;
velocity.y -= arrowGravity * Time.deltaTime;
arrowRotation.transform.rotation = Quaternion.LookRotation(dir);
}
}
您在 Arrow 脚本中声明,但未定义 'direction'。我怀疑将它包含在 newPos 计算中意味着没有添加任何内容。
我在unity5中实现了一个弓箭脚本。但是箭并没有飞出去。有人可以建议为什么会这样。我是初学者。
这是弓脚本。
using UnityEngine;
using System.Collections;
public class Bow : MonoBehaviour {
private GameObject arrow = null;
private Vector3 fwd = Vector3.zero;
public GameObject arrowPrefab = null;
public int initialSpeed = 30;
public GameObject launchPosition = null;
public float waitTime = 0.1f;
public LayerMask layerMask;
// Use this for initialization
void Start ()
{
}
void FixedUpdate()
{
fwd = transform.TransformDirection(Vector3.forward);
if (!Physics.Raycast(transform.position, fwd, 1, layerMask))
{
if (Input.GetMouseButtonDown(0))
{
Fire();
}
}
}
private void Fire()
{
//yield return new WaitForSeconds(waitTime);
new WaitForSeconds(waitTime);
arrow = (GameObject)Instantiate(arrowPrefab, launchPosition.transform.position, launchPosition.transform.rotation);
}
}
这是箭头脚本
using UnityEngine;
using System.Collections;
public class Arrow : MonoBehaviour {
private Vector3 velocity = Vector3.zero; private Vector3 newPos; private Vector3 oldPos; private bool hasHit = false; private Vector3 direction; private RaycastHit hit; private GameObject follow; private Vector3 dir; private float dist;
public LayerMask layerMask;
public float speed;
public Transform arrowRotation;
public float forceToApply;
public float arrowGravity;
void Start()
{
newPos = transform.position;
oldPos = newPos;
velocity = speed * transform.forward;
}
void update()
{
if (hasHit)
{
transform.position = follow.transform.position;
transform.rotation = follow.transform.rotation;
return;
}
newPos += (velocity + direction) * Time.deltaTime;
dir = newPos - oldPos;
dist = dir.magnitude;
dir /= dist;
if(dist > 0)
{
if (Physics.Raycast(oldPos,dir,out hit,dist,layerMask))
{
newPos = hit.point;
if (hit.collider)
{
if (hit.rigidbody)
{
GameObject hitpoint = (GameObject)Instantiate(new GameObject(), hit.point, transform.root.rotation);
hitpoint.transform.parent = hit.transform;
follow = hitpoint;
GetComponent<Collider>().isTrigger = true;
hit.rigidbody.AddForceAtPosition(forceToApply * dir, hit.point);
hasHit = true;
}
else
{
enabled = false;
}
}
}
}
oldPos = transform.position;
transform.position = newPos;
velocity.y -= arrowGravity * Time.deltaTime;
arrowRotation.transform.rotation = Quaternion.LookRotation(dir);
}
}
您在 Arrow 脚本中声明,但未定义 'direction'。我怀疑将它包含在 newPos 计算中意味着没有添加任何内容。