尝试切换字符统一 2d
Trying to switch character unity 2d
我正在制作一款平台游戏,您必须针对关卡的不同方面改变角色。每个角色都有不同的属性等等。我看了这个教程 https://www.youtube.com/watch?v=vD5pW97-050 但这基本上只是改变了精灵,因为两个角色都是同一个游戏对象的一部分,具有相同的刚体和对撞机。我正在使用 c#,如果有人知道解决方案,我真的很想得到一些帮助。这是我的播放器控制器脚本,其中包括教程中的角色切换脚本。任何帮助将不胜感激谢谢!
using UnityEngine;
using System.Collections;
public class controller : MonoBehaviour
{
public float topSpeed = 15f;
bool facingRight = true;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
GameObject Player, Player2;
int characterselect;
public float jumpForce = 700f;
public LayerMask whatIsGround;
void Start()
{
characterselect = 1;
Player = GameObject.Find("Player");
Player2 = GameObject.Find("Player2");
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
float move = Input.GetAxis("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(move * topSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
flip();
else if (move < 0 && facingRight)
flip();
}
void Update()
{
if(grounded&& Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
{
if (Input.GetKeyDown(KeyCode.E))
{
if (characterselect==1)
{
characterselect = 2;
}
else if (characterselect==2)
{
characterselect = 1;
}
}
if (characterselect==1)
{
Player.SetActive(true);
Player2.SetActive(false);
}
else if (characterselect==2)
{
Player.SetActive(false);
Player2.SetActive(true);
}
}
}
void flip()
{
facingRight = ! facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
拥有一个控制器是一个好的开始,但你必须考虑你希望你的角色有何不同。如果它们将完全不同,那么请考虑创建一个 "PlayerCharacter" 接口,该接口具有适用于所有可能命令的功能,例如
public interface IPlayerCharacter
{
void DoActionA();
void DoActionB();
}
如果您不知道什么是接口,它基本上是一个空的 class,用于定义函数,但尚未为它们创建代码。因此,您可以非常轻松地实现非常不同的播放器。因为您可以从该接口的实例调用函数。
然后,将这个组件添加到你的控制器中,如果你现在从玩家 1 切换到玩家 2 大概应该是这样的
public class PlayerController_Jumper : MonoBehaviour, IPlayerCharacter
{
// Does a jump
void DoActionA()
{
rigidbody.AddVelocity(Vector3.Up, 100.0f);
}
}
接口必须做的事情是您必须定义所有预定义的函数,因此它可能会抱怨此代码,因为没有 DoActionB()。但是让我们暂时忽略它。
然后将 "IPlayerController Jumper" 之类的东西和您打算制作的任何其他酷角色添加到您的控制器!
我正在制作一款平台游戏,您必须针对关卡的不同方面改变角色。每个角色都有不同的属性等等。我看了这个教程 https://www.youtube.com/watch?v=vD5pW97-050 但这基本上只是改变了精灵,因为两个角色都是同一个游戏对象的一部分,具有相同的刚体和对撞机。我正在使用 c#,如果有人知道解决方案,我真的很想得到一些帮助。这是我的播放器控制器脚本,其中包括教程中的角色切换脚本。任何帮助将不胜感激谢谢!
using UnityEngine;
using System.Collections;
public class controller : MonoBehaviour
{
public float topSpeed = 15f;
bool facingRight = true;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
GameObject Player, Player2;
int characterselect;
public float jumpForce = 700f;
public LayerMask whatIsGround;
void Start()
{
characterselect = 1;
Player = GameObject.Find("Player");
Player2 = GameObject.Find("Player2");
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
float move = Input.GetAxis("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(move * topSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
flip();
else if (move < 0 && facingRight)
flip();
}
void Update()
{
if(grounded&& Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
{
if (Input.GetKeyDown(KeyCode.E))
{
if (characterselect==1)
{
characterselect = 2;
}
else if (characterselect==2)
{
characterselect = 1;
}
}
if (characterselect==1)
{
Player.SetActive(true);
Player2.SetActive(false);
}
else if (characterselect==2)
{
Player.SetActive(false);
Player2.SetActive(true);
}
}
}
void flip()
{
facingRight = ! facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
拥有一个控制器是一个好的开始,但你必须考虑你希望你的角色有何不同。如果它们将完全不同,那么请考虑创建一个 "PlayerCharacter" 接口,该接口具有适用于所有可能命令的功能,例如
public interface IPlayerCharacter
{
void DoActionA();
void DoActionB();
}
如果您不知道什么是接口,它基本上是一个空的 class,用于定义函数,但尚未为它们创建代码。因此,您可以非常轻松地实现非常不同的播放器。因为您可以从该接口的实例调用函数。
然后,将这个组件添加到你的控制器中,如果你现在从玩家 1 切换到玩家 2 大概应该是这样的
public class PlayerController_Jumper : MonoBehaviour, IPlayerCharacter
{
// Does a jump
void DoActionA()
{
rigidbody.AddVelocity(Vector3.Up, 100.0f);
}
}
接口必须做的事情是您必须定义所有预定义的函数,因此它可能会抱怨此代码,因为没有 DoActionB()。但是让我们暂时忽略它。
然后将 "IPlayerController Jumper" 之类的东西和您打算制作的任何其他酷角色添加到您的控制器!