【问题标题】:Get player to jump with touch screen control让玩家通过触摸屏控制跳跃
【发布时间】:2014-11-07 11:42:40
【问题描述】:

您好,感谢您阅读这篇文章。

我有 2 个对象:一个播放器对象和一个 JumpButton 对象。

Player 对象是玩家。俄亥俄州JumpButton 对象是我想让玩家在您使用触摸屏设备(即安卓手机)时跳跃的对象。

这是我的 JumpButton 脚本:使用 UnityEngine;使用 System.Collections;

using UnityEngine;
using System.Collections;

public class JumpButtonScript : MonoBehaviour {
public GameObject player;
public GameObject JumpButton;

// Use this for initialization
void Start () {
    player = GameObject.Find ("player");
}

// Update is called once per frame
void Update () {
    if ((Input.touchCount == 1) && (Input.GetTouch(0).phase == TouchPhase.Began)) 
    {

    }
  }
}

这是允许我用箭头键控制播放器的脚本。

using UnityEngine;
using System.Collections;

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;
}
}

如您所见,然后我设法让玩家在箭头键上移动,但是当我按下跳转按钮(触摸屏按下)时如何让它跳转,就像在 Android 手机上一样?

我真的希望你能帮助我。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    如果你的问题是如何处理输入,试试这段代码,我们将在屏幕上从相机到鼠标位置发出光线,并检查标签以查看我们点击了什么

    public class handleInput: MonoBehaviour {
    
        void Update () {
            if (Input.GetMouseButtonDown (0)) {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit)) {
                    if (hit.collider.tag=="jumpbutton"){
                        jump();
                    }      
                }
            }
        }
    }
    

    对于跳跃部分,你可以这样做,向玩家添加一个力将他送上去,重力应该将他拉下来

    if (grounded == true)
        {
            rigidbody.AddForce(Vector3.up* jumpSpeed);
    
            grounded = false;
        } 
    

    【讨论】:

    • 感谢您的回复。但是当我点击按钮时什么都没有发生。
    • 您是否在按钮游戏对象中添加了 jumpbutton 标签
    • 我有你在上面的代码中看到的东西。提醒我是新手,所以标签,我如何放置它
    猜你喜欢
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多