提问人:HarrisonO 提问时间:7/17/2020 最后编辑:HarrisonO 更新时间:7/17/2020 访问量:27
如何将 characterScale.x = -1 替换为 transform。在此上下文中旋转 (0f, 180f, 0f),而不是每帧都执行?
How can I replace characterScale.x = -1 with transform.Rotate (0f, 180f, 0f) in this context, without it being executed every frame?
问:
我对编码视频游戏非常陌生,所以我一直在使用很多在线教程来帮助我制作基本的平台游戏/射击游戏。为了让我的角色能够双向射击,我需要精灵和它的子“FirePoint”来翻转。我怎样才能翻转我的角色,每当水平值 = -1 而没有每帧执行相同的命令时,它都是子角色。
提前致谢!
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public float jumpforce;
private float moveInput;
private Rigidbody2D rb;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
public Animator animator;
void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
void Update()
{
animator.SetFloat("Horizontal", moveInput);
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpforce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpforce;
}
if (isGrounded == true)
{
extraJumps = extraJumpsValue;
}
Vector3 characterScale = transform.localScale;
if (Input.GetAxisRaw("Horizontal") < 0)
{
characterScale.x = -1;
}
if (Input.GetAxisRaw("Horizontal") > 0)
{
characterScale.x = 1;
}
transform.localScale = characterScale;
}
}
答: 暂无答案
评论
Rotate
Rotate
"Horizontal"