51 lines
1.2 KiB
C#
51 lines
1.2 KiB
C#
using UnityEngine;
|
|
|
|
public class charactercontroller_sidescroller : MonoBehaviour
|
|
{
|
|
private float itsVelocity = 200f;
|
|
|
|
private Rigidbody itsRigidBody;
|
|
|
|
private bool itsGrounded = true;
|
|
|
|
private void Awake()
|
|
{
|
|
itsRigidBody = GetComponent<Rigidbody>();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
|
|
{
|
|
itsRigidBody.AddForce(0f - itsVelocity, 0f, 0f, ForceMode.Force);
|
|
}
|
|
else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
|
|
{
|
|
itsRigidBody.AddForce(itsVelocity, 0f, 0f, ForceMode.Force);
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.UpArrow) || (Input.GetKey(KeyCode.Space) && itsGrounded))
|
|
{
|
|
GetComponent<Rigidbody>().AddForce(0f, 1500f, 0f);
|
|
itsGrounded = false;
|
|
}
|
|
if (itsRigidBody.velocity.x > 5f)
|
|
{
|
|
itsRigidBody.velocity = new Vector3(5f, itsRigidBody.velocity.y, 0f);
|
|
}
|
|
else if (itsRigidBody.velocity.x < -5f)
|
|
{
|
|
itsRigidBody.velocity = new Vector3(-5f, itsRigidBody.velocity.y, 0f);
|
|
}
|
|
itsRigidBody.AddForce(0f, -50f, 0f);
|
|
if (base.transform.position.y < -10f)
|
|
{
|
|
itsRigidBody.MovePosition(new Vector3(base.transform.position.x, 20f, 0f));
|
|
}
|
|
}
|
|
|
|
public void OnCollisionEnter(Collision theCollision)
|
|
{
|
|
itsGrounded = true;
|
|
}
|
|
}
|