移动平台

时间:2015-03-25 14:22:59

标签: c# unity3d 2d

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

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

1 个答案:

答案 0 :(得分:0)

你需要设置MovingPlatform是HoldPlayerPlatform的ParentObject。 检查下面的图像也是

这是在移动平台上举行播放器脚本。

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的图像 setup HoldPlayerPlatform

这是MovingPlatform的设置 enter image description here