首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >移动平台

移动平台
EN

Stack Overflow用户
提问于 2015-03-25 22:22:59
回答 1查看 107关注 0票数 0

我们刚开始学习Unity,所以我们决定创建一个迷你平台。我们已经制作了硬币、平台和角色动画,但当我们试图制作一个平台的动画时,一个巨大的灾难出现了。问题是角色不能站在平台上。当平台移动时,他就会倒下(看起来没有摩擦,但我们试图设置一个-它是无能为力的)。也许,我们会重复之前已经问过的问题,但希望你能帮助我们解决这个悖论。祝您一天愉快;)

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CharControl : MonoBehaviour
{

    public float maxSpeed = 10f; 
    private bool isFacingRight = true;
    private Animator anim;
    private bool isGrounded = false;
    public Transform groundCheck;
    private float groundRadius = 0.2f;
    public LayerMask whatIsGround;
    public Text scoreText;
    public float score = 0;


    private void Start()
    {
        anim = GetComponent<Animator>();
    }


    private void FixedUpdate()
    {

        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround); 
        anim.SetBool ("Ground", isGrounded);

        anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);

        if (isGrounded && rigidbody2D.velocity.y != 0)
            return;

        float move = Input.GetAxis("Horizontal");

        anim.SetFloat("Speed", Mathf.Abs(move));

        rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);

        if(move > 0 && !isFacingRight)
            Flip();
        else if (move < 0 && isFacingRight)
            Flip();
    }

    private void Update()
    {
        if (isGrounded && (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown(KeyCode.Joystick1Button0)))
        {
            anim.SetBool("Ground", false);
            rigidbody2D.AddForce(new Vector2(0, 600));              
        }
    }

    private void Flip()
    {

        isFacingRight = !isFacingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
    void OnTriggerEnter2D(Collider2D col){
        if(col.gameObject.name == "Skull"){
            score++;
            Destroy (col.gameObject);
            scoreText.text = "" + score;
        }
        if ((col.gameObject.name == "dead"))
            Application.LoadLevel (Application.loadedLevel);
    }}
EN

回答 1

Stack Overflow用户

发布于 2017-06-30 04:27:38

您需要设置MovingPlatform为HoldPlayerPlatform的ParentObject。也可以查看下面的图片

这是在移动平台上保存的播放器脚本。

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HoldPlayer : MonoBehaviour
{
   private GameObject target = null;
   private Vector3 offset;
   void Start()
   {
         target = null;
   }
   void OnTriggerStay(Collider col)
   {
         target = col.gameObject;
         offset = target.transform.position - transform.position;
   }
   void OnTriggerExit(Collider col)
   {
         target = null;
   }
   void LateUpdate()
   {
         if (target != null)
         {
             target.transform.position = transform.position + offset;
         }
   }
}

这是设置HoldPlayerPlatform的图像

下面是MovingPlatform的设置

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29258345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档