Hello, I just recently took up Unity and the first thing I’m looking to make is a 3D platformer. I looked at some of the tutorials and I noticed that they use controller scripts. I decided to not go with that as I wanted to create everything myself and learn how to do it instead of relying on scripts already mostly made.
So far, I’ve managed to make a character move forward, back, left, right and up. It seems that I’m having problems figuring out how to make the character rotate so that way I can move towards that new direction. (i.e, Mario, Crash, Zelda, etc.)
Can someone please point me towards tutorials that use only rigid bodies. I don’t want to use any controllers. If anyone is able to show me examples of how I can do what I want to accomplish, I’d greatly appreciate it.
The next few things I’d like to accomplish is:
Have “falling” animation play when coming down.
Have “landing” animation play when touching ground.
Rotating character then walk forward.
Have character turn towards camera then start moving while camera rotates around to go back behind character.
Here is my code so far:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float playerSpeed = 5f;
public float jumpSpeed = 200.0f;
public float rotateSpeed = 20f;
private bool grounded = false;
private int jumpReached;
private bool falling = false;
// Use this for initialization
void Start () {
animation["jump"].wrapMode = WrapMode.Once;
animation["jumpfall"].wrapMode = WrapMode.ClampForever;
}
// Update is called once per frame
void Update () {
//this is because the rigidbody would fall over
rigidbody.freezeRotation = true;
//if movement is less than 0.1, play idle
if (Mathf.Abs(Input.GetAxis("Vertical")) <= 0.1){
animation.CrossFade("Idle");
}
//moves player right
if(Input.GetKey(KeyCode.RightArrow))
transform.position += new Vector3(Time.deltaTime * playerSpeed,0,0);
//moves player left
if(Input.GetKey(KeyCode.LeftArrow))
transform.position -= new Vector3(Time.deltaTime * playerSpeed,0,0);
//moves player forwards
if(Input.GetKey(KeyCode.UpArrow))
{
transform.position += new Vector3(0,0,Time.deltaTime * playerSpeed);
animation.Play("walk");
}
//moves player backwards
if(Input.GetKey(KeyCode.DownArrow))
transform.position -= new Vector3(0,0,Time.deltaTime * playerSpeed);
//makes player jump
if (Input.GetKeyDown(KeyCode.Space) grounded == true) // Jump
{
rigidbody.AddForce(Vector3.up * jumpSpeed);
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX;
rigidbody.constraints = RigidbodyConstraints.FreezeRotationY;
animation.Play ("jump");
}
}
void OnCollisionStay (Collision collisionInfo)
{
grounded = true;
}
void OnCollisionExit (Collision collisionInfo)
{
grounded = false;
falling = true;
}
}