Unity点击一个对象触发另一个对象

本文关键字:一个对象 Unity | 更新日期: 2023-09-27 18:29:47

请记住,我是Unity的新手。我有两个脚本,我想"组合",但当我尝试时,它不起作用。

我有一个脚本(Name : RobotController)。这个脚本控制玩家的移动。向上/跳跃、向下、向左和向右。(仅适用于atm的键盘键)

这个脚本运行得很好,但现在我想添加手机触摸键的功能。我的意思是,如果一个人点击"向上箭头",玩家就会跳起来。

向上箭头是一个对象。

这就是我的问题所在。我用一个对撞机和一个脚本创建了向上箭头。

向上箭头脚本:

public class NewJumpScript : MonoBehaviour {

// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseOver()
{
    if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0))) 
    {
        Debug.Log("test");
    }
}
}

这是RobotController脚本´,包括地面检查等。

public class RobotController : MonoBehaviour {
//This will be our maximum speed as we will always be multiplying by 1
public float maxSpeed = 2f;
public GameObject player;
public GameObject sprite;
//a boolean value to represent whether we are facing left or not
bool facingLeft = true;
//a value to represent our Animator
Animator anim;
//to check ground and to have a jumpforce we can change in the editor
bool grounded = true;
public Transform groundCheck;
public float groundRadius = 1f;
public LayerMask whatIsGround;
public float jumpForce = 300f;
private bool isOnGround = false;
void OnCollisionEnter2D(Collision2D collision) {
        isOnGround = true;  
    }
void OnCollisionExit2D(Collision2D collision) {
    anim.SetBool ("Ground", grounded);
    anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    isOnGround = false;
}
// Use this for initialization
void Start () {
    player = GameObject.Find("player");
    //set anim to our animator
    anim = GetComponent <Animator>();
}

void FixedUpdate () {
    //set our vSpeed
    //set our grounded bool
    grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    //set ground in our Animator to match grounded
    anim.SetBool ("Ground", grounded);
    float move = Input.GetAxis ("Horizontal");//Gives us of one if we are moving via the arrow keys
    //move our Players rigidbody
    rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y);   
    //set our speed
    anim.SetFloat ("Speed",Mathf.Abs (move));
    //if we are moving left but not facing left flip, and vice versa
    if (move > 0 && !facingLeft) {
        Flip ();
    } else if (move < 0 && facingLeft) {
        Flip ();
    }
}
void Update(){
    if ((isOnGround == true && Input.GetKeyDown (KeyCode.UpArrow)) || (isOnGround == true && Input.GetKeyDown (KeyCode.Space))) {
        anim.SetBool("Ground",false);
        rigidbody2D.AddForce (new Vector2 (0, jumpForce));
    }
    if (isOnGround == true && Input.GetKeyDown (KeyCode.DownArrow)) 
    {
        gameObject.transform.localScale = new Vector3(transform.localScale.x, 0.2f, 0.2f);
    }
    if (isOnGround == true && Input.GetKeyUp (KeyCode.DownArrow)) 
    {
        gameObject.transform.localScale = new Vector3(transform.localScale.x, 0.3f, 0.3f);
    }
}

//flip if needed
void Flip(){
    facingLeft = !facingLeft;
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
}
}

需要发生的是,当点击游戏中的"向上箭头"时,该人将像RobotController脚本中那样泪流满面。我希望你能理解我的问题。

谢谢你的时间和帮助。

Unity点击一个对象触发另一个对象

我发布了这两个问题的相同答案,因为它们非常相似。您可以根据需要轻松地修改代码。

这只是一种方法。可能还有很多,但这是我迄今为止遇到的最好的方法。

在QUI按钮上,需要这样的脚本:

private Mover playerMover;
void Start()
{
    playerMover = GameObject.Find("Character").GetComponent<Mover>();
}
void OnMouseOver()
{
    if (Input.GetMouseButton(0)) 
    {
        Debug.Log("pressed");
        playerMover.MoveButtonPressed();
    }
}

注意,find只在start函数中完成一次,因为它的计算量很大。

在角色游戏对象上,需要这样的脚本组件:

using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
    public void MoveButtonPressed()
    {
        lastPressedTime = Time.timeSinceLevelLoad;
    }
    public double moveTime = 0.1;
    private double lastPressedTime = 0.0;
    void Update()
    {
        if(lastPressedTime + moveTime > Time.timeSinceLevelLoad)
        {
            // Character is moving
            rigidbody.velocity = new Vector3(1.0f, 0.0f, 0.0f);
        }
        else
        {
            rigidbody.velocity = new Vector3(0.0f, 0.0f, 0.0f);
        }
    }
}