方法①:使用组件:SpriteRenderer
使用GetAxis()
和GetAxisRaw()
没有区别
public SpriteRenderer spriteRenderer;
public float moveFactor;
void Start(){
spriteRenderer=GetComponent<SpriteRenderer>();
}
void Move(){
moveFactor = Input.GetAxisRaw("Horizontal");
if(moveFactor > 0) spriteRenderer.flipX = false;
else if (moveFactor < 0) spriteRenderer.flipX = true;
}
方法②:使用组件:Transform
情况1:使用GetAxisRaw()
;没有刹车效果,及翻转过程没有过渡,
m
o
v
e
F
a
c
t
o
r
moveFactor
moveFactor取-1,1,0
public float moveFactor;
void Move(){
moveFactor = Input.GetAxisRaw("Horizontal");
if(moveFactor!=0) transform.localScale=new Vector3(moveFactor,0.5,0.5);
}
情况1:使用GetAxis()
;有刹车效果,及翻转过程有过渡,
m
o
v
e
F
a
c
t
o
r
moveFactor
moveFactor取-1~0
public float moveFactor;
public bool facingRight=true;
void Move(){
moveFactor = Input.GetAxisRaw("Horizontal");
//当图片的方向与移动的方向不一致时,需要翻转
if(!facingRight&&moveFactor>0) Flip();
else if(facingRight&&moveFactor<0) Flip();
}
void Flip(){
Vector3 playerScale=transform.localScale;
playerScale.x*=-1;
transform.localScale=playerScale;
facingRight=!facingRight;
}
其余方法简写汇总:
01文章来源:https://www.toymoban.com/news/detail-653952.html
//01
void Move(){
float MoveX = Input.GetAxis("Horizontal");
float FlipX = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(MoveX*playerSpeed,rb.velocity.y);
if(FlipX!=0){
transform.localScale = new Vector3(FlipX,1,1);
}
}
02文章来源地址https://www.toymoban.com/news/detail-653952.html
transform.localScale = new Vector3(horizontal>0?1:-1,1);
到了这里,关于Unity2D-使人物翻转的方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!