将 unity2D 中的对象移向另一个单击的对象
Move an object in unity2D towards another clicked object
- 我的场景中有一个名为“Player”的对象。
- 我还有多个名为“Trees”的对象。
现在我希望每当用户单击 'Tree' 时,'Player' 缓慢移动到该位置(使用 Lerp 或 moveTowards)对我来说都是 O.K。
现在这段代码有 2 个问题:
我希望此代码是通用的
每当我单击 树对象 我希望这样可以将 player 移向 tree。我不想编写此脚本并将其附加到每个 树对象。
我应该把脚本放在哪里?
目前我将此代码附加到每个 树对象。
我应该如何写下来才能适用于每个树对象
在现场?
如果在移动时再次点击,则取消之前的移动并开始移动到新位置
- 我应该怎么写,这样当我点击另一个对象的时候
玩家正在向另一个被点击的物体移动,玩家停止向其先前位置移动,并开始
朝着新的点前进。
我在适应新的 UnityScript 时遇到了一些问题。我完全来自 Javascript 背景,看起来它们中的 2 种是语义截然不同的语言。因此,如果有人用代码回答了这个问题(这是我想要的:)),我也会感谢一些冗长的评论。
我目前这样做:
var playerIsMoving = false;
Public playerObject: Gameobject; //I drag in the editor the player in this public var
function update(){
var thisTreePosition = transform.point; //this store the X pos of the tree
var playerPosition = player.transform.point;
if(playerIsMoving){
player.transform.position = Vector2.MoveTowards(playerPosition, thisTreePosition, step);
}
}
function OnMouseDown(){
playerIsMoving = true;
}
我在家里写这篇文章,我没有 Unity,我忘记了代码语法,因此希望上面的代码有拼写错误或问题,在工作中它工作得很好,除了非常粗鲁和质朴.
我建议你把运动脚本放在播放器上。以及如何使用 Raycast 来测试您是否撞到树?
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Vector3 positionToWalkTo = new Vector3();
void Update() {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
Ray ray = new Ray(transform.position, direction);
if (Physics.Raycast(ray, out hit))
if (hit.gameObject.tag.Equals("tree")){
positionToWalkTo = hit.gameObject.transform.position;
}
}
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, positionToWalkTo, step);
}
要得到这样的东西运行,你应该标记所有的树。
'JavaScript'
var target: Transform;
// Speed in units per sec.
var speed: float;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Raycasting is like shooting something into the given direction (ray) and hit is the object which got hit
if (Physics.Raycast(ray, hit)) {
if (hit.gameObject.tag = "tree")
target = hit.gameObject.transform.position; // Sets the new target position
}
}
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
好的,所以我自己完成了(在 Freshchris 的回答 一些 帮助下)。
这里是:
var target:Vector2;
var targetObject:GameObject;
var initialY:float;
var tolerance = 1;
var speed = 3;
var isMoving = false;
function Start(){
target = transform.position;
}
function Update () {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); //raycast the scene
// if we hit something
if (hit.collider != null) {
isMoving=true; //it should start moving
targetObject = hit.collider.gameObject; //the target object
target = hit.collider.gameObject.transform.position;
}
}
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
// if it should be moving - move it - and face the player to the correct direction
if (isMoving){
if(transform.position.x>target.x){
gameObject.transform.localScale.x = -0.7;
}else {
gameObject.transform.localScale.x = 0.7;
}
transform.position = Vector2.MoveTowards(transform.position, target, step);
}
// if it reached the target object by a specified tolerance, tell it to stop moving
if(isMoving){
if((transform.position.x < target.x+tolerance)&&(transform.position.x > target.x-tolerance)){
print("position reached"); //this is fired just once since isMoving is switched to false just one line below
print(targetObject);
isMoving=false;
}
}
}
- 我的场景中有一个名为“Player”的对象。
- 我还有多个名为“Trees”的对象。
现在我希望每当用户单击 'Tree' 时,'Player' 缓慢移动到该位置(使用 Lerp 或 moveTowards)对我来说都是 O.K。
现在这段代码有 2 个问题:
我希望此代码是通用的
每当我单击 树对象 我希望这样可以将 player 移向 tree。我不想编写此脚本并将其附加到每个 树对象。
我应该把脚本放在哪里?
目前我将此代码附加到每个 树对象。
我应该如何写下来才能适用于每个树对象 在现场?
如果在移动时再次点击,则取消之前的移动并开始移动到新位置
- 我应该怎么写,这样当我点击另一个对象的时候 玩家正在向另一个被点击的物体移动,玩家停止向其先前位置移动,并开始 朝着新的点前进。
我在适应新的 UnityScript 时遇到了一些问题。我完全来自 Javascript 背景,看起来它们中的 2 种是语义截然不同的语言。因此,如果有人用代码回答了这个问题(这是我想要的:)),我也会感谢一些冗长的评论。
我目前这样做:
var playerIsMoving = false;
Public playerObject: Gameobject; //I drag in the editor the player in this public var
function update(){
var thisTreePosition = transform.point; //this store the X pos of the tree
var playerPosition = player.transform.point;
if(playerIsMoving){
player.transform.position = Vector2.MoveTowards(playerPosition, thisTreePosition, step);
}
}
function OnMouseDown(){
playerIsMoving = true;
}
我在家里写这篇文章,我没有 Unity,我忘记了代码语法,因此希望上面的代码有拼写错误或问题,在工作中它工作得很好,除了非常粗鲁和质朴.
我建议你把运动脚本放在播放器上。以及如何使用 Raycast 来测试您是否撞到树?
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Vector3 positionToWalkTo = new Vector3();
void Update() {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
Ray ray = new Ray(transform.position, direction);
if (Physics.Raycast(ray, out hit))
if (hit.gameObject.tag.Equals("tree")){
positionToWalkTo = hit.gameObject.transform.position;
}
}
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, positionToWalkTo, step);
}
要得到这样的东西运行,你应该标记所有的树。
'JavaScript'
var target: Transform;
// Speed in units per sec.
var speed: float;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Raycasting is like shooting something into the given direction (ray) and hit is the object which got hit
if (Physics.Raycast(ray, hit)) {
if (hit.gameObject.tag = "tree")
target = hit.gameObject.transform.position; // Sets the new target position
}
}
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
好的,所以我自己完成了(在 Freshchris 的回答 一些 帮助下)。
这里是:
var target:Vector2;
var targetObject:GameObject;
var initialY:float;
var tolerance = 1;
var speed = 3;
var isMoving = false;
function Start(){
target = transform.position;
}
function Update () {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); //raycast the scene
// if we hit something
if (hit.collider != null) {
isMoving=true; //it should start moving
targetObject = hit.collider.gameObject; //the target object
target = hit.collider.gameObject.transform.position;
}
}
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
// if it should be moving - move it - and face the player to the correct direction
if (isMoving){
if(transform.position.x>target.x){
gameObject.transform.localScale.x = -0.7;
}else {
gameObject.transform.localScale.x = 0.7;
}
transform.position = Vector2.MoveTowards(transform.position, target, step);
}
// if it reached the target object by a specified tolerance, tell it to stop moving
if(isMoving){
if((transform.position.x < target.x+tolerance)&&(transform.position.x > target.x-tolerance)){
print("position reached"); //this is fired just once since isMoving is switched to false just one line below
print(targetObject);
isMoving=false;
}
}
}