C# 在同时移动的对象上使用transform.forward?
我正在继续编写一个允许我拖放对象的脚本,但现在我需要弄清楚如何让它们在Z轴上移动。我试图通过按钮输入来实现这一点。我目前的方法是使用transform forward,我的代码实际上没有任何错误,但问题在于Unity没有引用,我也不确定它在寻找什么 这是我的完整代码:C# 在同时移动的对象上使用transform.forward?,c#,unity3d,C#,Unity3d,我正在继续编写一个允许我拖放对象的脚本,但现在我需要弄清楚如何让它们在Z轴上移动。我试图通过按钮输入来实现这一点。我目前的方法是使用transform forward,我的代码实际上没有任何错误,但问题在于Unity没有引用,我也不确定它在寻找什么 这是我的完整代码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class DragNDrop : MonoBehavi
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragNDrop : MonoBehaviour
{
//Initialize Variables
GameObject getTarget;
bool isMouseDragging;
Vector3 offsetValue;
Vector3 positionOfScreen;
Rigidbody m_rigidbody;
float boxspeed;
// Use this for initialization
void Start()
{
boxspeed = 5;
}
void Update()
{
//Mouse Button Press Down
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
getTarget = ReturnClickedObject(out hitInfo);
if (getTarget.tag == "Box")
{
m_rigidbody = GetComponent<Rigidbody>();
isMouseDragging = true;
//Converting world position to screen position.
positionOfScreen = Camera.main.WorldToScreenPoint(getTarget.transform.position);
offsetValue = getTarget.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, positionOfScreen.z));
if (Input.GetKey(KeyCode.UpArrow))
{
//Move the Rigidbody forwards constantly at speed you define (the blue arrow axis in Scene view)
m_rigidbody.velocity = transform.forward * boxspeed;
}
if (Input.GetKey(KeyCode.DownArrow))
{
//Move the Rigidbody forwards constantly at speed you define (the blue arrow axis in Scene view)
boxspeed = -5;
m_rigidbody.velocity = transform.forward * boxspeed;
}
}
}
//Mouse Button Up
if (Input.GetMouseButtonUp(0))
{
isMouseDragging = false;
}
//Is mouse Moving
if (isMouseDragging)
{
//tracking mouse position.
Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, positionOfScreen.z);
//converting screen position to world position with offset changes.
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offsetValue;
//It will update target gameobject's current postion.
getTarget.transform.position = currentPosition;
}
}
//Method to Return Clicked Object
GameObject ReturnClickedObject(out RaycastHit hit)
{
GameObject target = null;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
{
target = hit.collider.gameObject;
}
return target;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类DragNDrop:单一行为
{
//初始化变量
游戏对象获取目标;
布尔·伊斯穆塞;
向量3偏移值;
屏幕的矢量3位置;
刚体m_刚体;
浮箱速度;
//用于初始化
void Start()
{
boxspeed=5;
}
无效更新()
{
//按下鼠标按钮
if(Input.GetMouseButtonDown(0))
{
RaycastHitInfo;
getTarget=返回ClickedObject(输出hitInfo);
if(getTarget.tag==“Box”)
{
m_rigidbody=GetComponent();
IsMouseDraging=true;
//将世界位置转换为屏幕位置。
屏幕的位置=Camera.main.WorldToScreenPoint(getTarget.transform.position);
offsetValue=getTarget.transform.position-Camera.main.ScreentToWorldPoint(新矢量3(Input.mousePosition.x,Input.mousePosition.y,positionOfScreen.z));
if(Input.GetKey(KeyCode.UpArrow))
{
//以定义的速度不断向前移动刚体(场景视图中的蓝色箭头轴)
m_rigidbody.velocity=transform.forward*boxspeed;
}
if(Input.GetKey(KeyCode.DownArrow))
{
//以定义的速度不断向前移动刚体(场景视图中的蓝色箭头轴)
boxspeed=-5;
m_rigidbody.velocity=transform.forward*boxspeed;
}
}
}
//鼠标按钮向上
if(Input.GetMouseButtonUp(0))
{
IsMouseDraging=false;
}
//老鼠在动吗
如果(正在拖动)
{
//跟踪鼠标位置。
Vector3 currentScreenSpace=新的Vector3(Input.mousePosition.x,Input.mousePosition.y,positionOfScreen.z);
//使用偏移更改将屏幕位置转换为世界位置。
Vector3 currentPosition=Camera.main.ScreentToWorldPoint(currentScreenSpace)+偏移值;
//它将更新目标游戏对象的当前位置。
getTarget.transform.position=当前位置;
}
}
//方法返回单击的对象
游戏对象返回ClickedObject(光线投射命中)
{
游戏对象目标=null;
Ray-Ray=Camera.main.screenpointoray(输入.鼠标位置);
if(物理光线投射(光线原点,光线方向*10,外击))
{
target=hit.collider.gameObject;
}
回报目标;
}
}
具体导致错误的部分是更新函数,如下所示:
void Update()
{
//Mouse Button Press Down
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
getTarget = ReturnClickedObject(out hitInfo);
if (getTarget.tag == "Box")
{
m_rigidbody = GetComponent<Rigidbody>();
isMouseDragging = true;
//Converting world position to screen position.
positionOfScreen = Camera.main.WorldToScreenPoint(getTarget.transform.position);
offsetValue = getTarget.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, positionOfScreen.z));
if (Input.GetKey(KeyCode.UpArrow))
{
//Move the Rigidbody forwards constantly at speed you define (the blue arrow axis in Scene view)
m_rigidbody.velocity = transform.forward * boxspeed;
}
if (Input.GetKey(KeyCode.DownArrow))
{
//Move the Rigidbody forwards constantly at speed you define (the blue arrow axis in Scene view)
boxspeed = -5;
m_rigidbody.velocity = transform.forward * boxspeed;
}
}
}
void Update()
{
//按下鼠标按钮
if(Input.GetMouseButtonDown(0))
{
RaycastHitInfo;
getTarget=返回ClickedObject(输出hitInfo);
if(getTarget.tag==“Box”)
{
m_rigidbody=GetComponent();
IsMouseDraging=true;
//将世界位置转换为屏幕位置。
屏幕的位置=Camera.main.WorldToScreenPoint(getTarget.transform.position);
offsetValue=getTarget.transform.position-Camera.main.ScreentToWorldPoint(新矢量3(Input.mousePosition.x,Input.mousePosition.y,positionOfScreen.z));
if(Input.GetKey(KeyCode.UpArrow))
{
//以定义的速度不断向前移动刚体(场景视图中的蓝色箭头轴)
m_rigidbody.velocity=transform.forward*boxspeed;
}
if(Input.GetKey(KeyCode.DownArrow))
{
//以定义的速度不断向前移动刚体(场景视图中的蓝色箭头轴)
boxspeed=-5;
m_rigidbody.velocity=transform.forward*boxspeed;
}
}
}
我得到的错误表明:
NullReferenceException:对象引用未设置为对象的实例
Update()(位于Assets/Scripts/DragNDrop.cs:30)
非常感谢您提供的任何帮助!如前所述,如果没有点击,您的
返回ClickedObject
返回null
您不应该在类型为UnityEngine.Object
(基本上所有Unity引用类型)的引用上使用==null
或Nullables(?。
)
还要注意的是,您不应该直接使用标签,而应该使用标签。如果给定的标签拼写错误或根本不存在,则会引发异常。一个“bug”会被==
隐藏,这会耗费您的开发时间和精力。此外,它会稍微快一点
只需将您的支票更改为
getTarget = ReturnClickedObject(out hitInfo);
if (getTarget && getTarget.CompareTag("Box"))
一般注意事项:存储和重用引用!
GetComponent
e.g.是一个非常昂贵的调用。Camera.main
也是如此,它在内部使用类似于FindObjectWithTag
的东西
// If possible already reference these via the Inspector to
// skip GetComponent entirely!
[SerializeField] private Rigidbody _rigidbody;
[Serializefield] private Camera mainCamera;
private void Start()
{
if(!_rigidbody) _rigidbody = GetComponent<Rigidbody>();
if(!mainCamera) mainCamera = Camera.main;
}
及
在尝试访问其属性之前,您应该检查getTarget是否为null。您希望ReturnClickedObject返回有效引用,但它可能不会命中任何内容并返回null。关于光线投射的if语句可能不为true,然后它返回null目标。您可以添加if(getTarget!
_rigidbody.velocity = ...
mainCamera.WorldToScreenPoint(...)