C# 变换位置并添加恒定的z轴值C
我刚刚开始从事另一个项目,其中一个游戏机制是将无人机重新定位到鼠标光标的位置 下面是我的代码,其中有几个问题需要说明C# 变换位置并添加恒定的z轴值C,c#,unity3d,C#,Unity3d,我刚刚开始从事另一个项目,其中一个游戏机制是将无人机重新定位到鼠标光标的位置 下面是我的代码,其中有几个问题需要说明 using System.Collections; using System.Collections.Generic; using UnityEngine; public class lightDrone : MonoBehaviour { Vector3 newPosition; void Start() { newPosition = transform.posi
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class lightDrone : MonoBehaviour
{
Vector3 newPosition;
void Start()
{
newPosition = transform.position + (0,0,10); //***Problem A***
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
newPosition = hit.point;
transform.position = newPosition; // ***Problem B***
}
}
}
}
问题A-我正在尝试设置新位置=光标在y轴上单击+10的位置,因此无人机似乎在飞行,而不是在地面上。这并没有实现任何目标,只是给了我编译错误
我希望精确的位置是cursor.x,public int y,cursor.z,但我有一个非常模糊的想法
问题B-
当前,当我单击鼠标时,对象移动到光标处,但它似乎只是瞬间传送。我希望它以一定的速度移动,我想我需要一个公共的浮动来这样做,并将transform.position更改为translate.position,但这同样不起作用
提前感谢您回答我的问题,我正在努力学习这些新的机制以及如何对它们进行编码 问题A
如果希望新位置是光标在y轴上单击+10的位置,那么将任何代码放在开始位置都没有意义。这仅用于初始化。它在场景开始时只运行一次。只需在Update方法中将10添加到新位置
问题B
将transform.position设置为某个值将使变换立即移动到该位置。如果要随时间推移进行变换,则需要一次将变换移动几个增量。由于这引入了随时间变化的元素,您需要使用随时间变化的方法,因此您需要协同路由、异步,或者如果您有点狡猾,您可以只使用更新方法
你还需要光线相交并击中一些东西。就像一个平面,地面,地形,任何有对撞机的东西。如果它没有击中任何地方,那么你就不会有一个新的地方移动
密码
非常感谢,我还没有测试它,但是你告诉我raycasts是如何工作的。我不会想到所有这些事情都要考虑到这样一个简单的概念在纸上。你让我开心:嘿,我没想到会得到这么积极的回应。我很高兴这有帮助!
using UnityEngine;
public class LightDrone : MonoBehaviour
{
public float speed = 60.0f;
// Our destination needs to be remembered outside a single iteration of
// Update. So we put it outside of the method in order to remember it
// across multiple frames.
private Vector3 currentDestination;
// We need to check if we're at the destination yet so we know when to stop.
private bool notAtDestinationYet;
// When we're closer to the destination than this tolerance, we decide that
// we have arrived there.
private float tolerance = 0.1f;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
var newPosition = hit.point;
// You can add whatever height you want here. I added
// just 2 instead of 10.
currentDestination = newPosition + new Vector3(0, 2.0f, 0);
notAtDestinationYet = true;
}
}
if (notAtDestinationYet)
{
// Use a bit of vector math to get the direction from our current
// position to the destination. The direction is a normalized Vector
// that we can just multiply with our speed to go in that direction
// at that specific speed!
var heading = currentDestination - transform.position;
var distance = heading.magnitude;
var direction = heading / distance;
// Check if we've arrived at our destination.
if (distance < tolerance)
{
notAtDestinationYet = false;
}
else
{
// If the remaining distance between us and the destination is
// smaller than our speed, then we'll go further than necessary!
// This is called overshoot. So we need to make our speed
// smaller than the remaining distance.
// We also multiply by deltaTime to account for the variable
// amount of time that has passed since the last Update() call.
// Without multiplying with the amount of time that has passed
// our object's speed will increase or decrease when the
// framerate changes! We really don't want that.
float currentSpeed = Mathf.Clamp(speed * Time.deltaTime,
Mathf.Epsilon, distance);
transform.position += direction * currentSpeed;
}
}
}
}