EDIT: figured out how to put my code in the proper box.
Hi there,
I am starting out in game development and trying to keep things simple at first with a simple “jump the hurdles” type game. I’d like my player to be able to move forward (perhaps not backwards) using the UpArrow/W keys and rotate using the Left/Right/A/D keys. Then, once the player has rotated, it can move forward in the new direction set by its rotation. I haven’t gotten this far yet, but from the reading I’ve been doing I’ve also been anticipating a problem with having the Up and Left/Right arrow keys being hit at the same time.
So I’ve decided that Rigidbody AddForce/AddTorque is the best way to go since I want my player to follow physics (eventually I want to register if the player collides with a hurdle). But I’m having trouble with it. Here’s my script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed = 6f;
public float TurnSpeed = 0.5f;
Vector3 turning;
Vector3 movement;
Rigidbody playerRigidbody;
void Start ()
{
playerRigidbody = GetComponent <Rigidbody> ();
}
void FixedUpdate ()
{
float v = Input.GetAxis ("Vertical");
float j = Input.GetAxis ("Jump");
float t = Input.GetAxis ("Horizontal");
Move (v, j);
Rotate (t);
}
void Move (float v, float j)
{
movement.Set (0f, j, v);
playerRigidbody.AddForce (movement * speed);
}
void Rotate(float t)
{
turning.Set (0f, t, 0f);
playerRigidbody.AddTorque(turning * TurnSpeed);
}
}
Right now I am not freezing position on any axis. When I freeze both x and y axis, the player will rotate but not move forward. If I unfreeze either the x or y axis, the player moves forward but flips over while it is doing so.
Any help would be greatly appreciated. Thank you very much in advance!