Hi, I've been learning about this script for a few days, and I ran into something that is kind of confusing. It has to do with this line right here.
// Face Direction
float moveDir = Input.GetAxisRaw("Horizontal");
if (moveDir !=0) {
transform.eulerAngles = (moveDir>0)?Vector3.up * 180:Vector3.zero;
}
I notice that if I set the Input to positive 1 then the character I am using has its rotation flipped i.e. if transform.eulerAngles = moveDir>0 then flip the character on the y axis 180 degrees, otherwise don't do anything/keep the vector at zero.
But it still works if I turn the other way (if the moveDir is less than zero), even if I didn't actual put any code in to tell it to. So am I missing something. Is this just the way the code works or transform.eulerAngles works?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerPhysics))]
public class PlayerController : MonoBehaviour {
// Player Handling
public float gravity = 20;
public float walkSpeed = 8;
public float runSpeed = 12;
public float acceleration = 30;
public float jumpHeight = 12;
private float animationSpeed;
private float currentSpeed;
private float targetSpeed;
private Vector2 amountToMove;
private PlayerPhysics playerPhysics;
private Animator animator;
void Start () {
playerPhysics = GetComponent();
animator = GetComponent();
}
void Update () {
// Reset acceleration upon collision
if (playerPhysics.movementStopped) {
targetSpeed = 0;
currentSpeed = 0;
}
// If player is touching the ground
if (playerPhysics.grounded) {
amountToMove.y = 0;
// Jump
if (Input.GetButtonDown("Jump")) {
amountToMove.y = jumpHeight;
}
}
// Set animator parameters
animationSpeed = IncrementTowards(animationSpeed,Mathf.Abs(targetSpeed),acceleration);
animator.SetFloat("Speed",animationSpeed);
// Input
float speed = (Input.GetButton("Run"))?runSpeed:walkSpeed;
targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
// Set amount to move
amountToMove.x = currentSpeed;
amountToMove.y -= gravity * Time.deltaTime;
playerPhysics.Move(amountToMove * Time.deltaTime);
// Face Direction
float moveDir = Input.GetAxisRaw("Horizontal");
if (moveDir !=0) {
transform.eulerAngles = (moveDir>0)?Vector3.up * 180:Vector3.zero;
}
}
// Increase n towards target by speed
private float IncrementTowards(float n, float target, float a) {
if (n == target) {
return n;
}
else {
float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target
n += a * Time.deltaTime * dir;
return (dir == Mathf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
}
}
}&&
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(BoxCollider))]
public class PlayerPhysics : MonoBehaviour {
public LayerMask collisionMask;
private BoxCollider collider;
private Vector3 s;
private Vector3 c;
private Vector3 originalSize;
private Vector3 originalCentre;
private float colliderScale;
private int collisionDivisionsX = 3;
private int collisionDivisionsY =10;
private float skin = .005f;
[HideInInspector]
public bool grounded;
[HideInInspector]
public bool movementStopped;
Ray ray;
RaycastHit hit;
void Start() {
collider = GetComponent();
colliderScale = transform.localScale.x;
originalSize = collider.size;
originalCentre = collider.center;
SetCollider(originalSize,originalCentre);
}
public void Move(Vector2 moveAmount) {
float deltaY = moveAmount.y;
float deltaX = moveAmount.x;
Vector2 p = transform.position;
// Check collisions above and below
grounded = false;
for (int i = 0; i skin) {
deltaY = dst * dir - skin * dir;
}
else {
deltaY = 0;
}
grounded = true;
break;
}
}
// Check collisions left and right
movementStopped = false;
for (int i = 0; i skin) {
deltaX = dst * dir - skin * dir;
}
else {
deltaX = 0;
}
movementStopped = true;
break;
}
}
if (!grounded && !movementStopped) {
Vector3 playerDir = new Vector3(deltaX,deltaY);
Vector3 o = new Vector3(p.x + c.x + s.x/2 * Mathf.Sign(deltaX),p.y + c.y + s.y/2 * Mathf.Sign(deltaY));
ray = new Ray(o,playerDir.normalized);
if (Physics.Raycast(ray,Mathf.Sqrt(deltaX * deltaX + deltaY * deltaY),collisionMask)) {
grounded = true;
deltaY = 0;
}
}
Vector2 finalTransform = new Vector2(deltaX,deltaY);
transform.Translate(finalTransform,Space.World);
}
// Set collider
public void SetCollider(Vector3 size, Vector3 centre) {
collider.size = size;
collider.center = centre;
s = size * colliderScale;
c = centre * colliderScale;
}
}
↧