- Home /
character movement follows the direction of camera
Hi i have this script in c# :
public class PlayerMove : MonoBehaviour {
public float speed;
public float moveSpeed = 10;
public float rotateSpeed = 10;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
float moveForward = moveSpeed * Time.smoothDeltaTime * Input.GetAxis("Vertical");
float moveLeft = moveSpeed * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
float rotate = rotateSpeed * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
transform.Translate(Vector3.forward * moveForward);
transform.Translate(Vector3.right * moveLeft);
}
}
i want that the character movement on base the direction of camera. So the axes of camera decides the direction of the character , if i rotated the camera to right and i click "up arrow" ( forward movement) the my character go to right but if i don't have rotated the camera but i click to "right arrow" my character moved laterally to right. I hope I explained. For instance, do you remember the games on Conole? here is the same type of dynamic motion that i want reach with the chamber and the movement of the same character, for example of a game such as Call of Duty this is .
Please help me. Thank you
Hah there are -1 answers to this question.
But, to give you a tip, the lines you will want to change are lines 18 and 19.
Specifically you will want to change "Vector3.forward" to the cameras forward direction.
Vector3.forward should get the forward axis in world space, transform.forward will get the forward axis of the object the script is attached to.
SO, what you want to do is add a reference to the camera object and get it's forward axis ins$$anonymous$$d e.g. cam.transform.forward and cam.transform.right
Thank You , this is the new script c# with the modify (is present the Jump) :
public class Player$$anonymous$$ove : $$anonymous$$onoBehaviour {
// These variables are for adjusting in the inspector how the object behaves
public static float maxSpeed = 7;
public static float force = 8;
public static float jumpSpeed = 0;
public GameObject cam;
// These variables are there for use by the script and don't need to be edited
private int state = 0;
private bool grounded = false;
private float jumpLimit = 0;
// Don't let the Physics Engine rotate this physics object so it doesn't fall over when running
void Awake ()
{
rigidbody.freezeRotation = true;
}
// This part detects whether or not the object is grounded and stores it in a variable
void OnCollisionEnter ()
{
state ++;
if(state > 0)
{
grounded = true;
}
}
void OnCollisionExit ()
{
state --;
if(state < 1)
{
grounded = false;
state = 0;
}
}
public virtual bool jump
{
get
{
return Input.GetButtonDown ("Jump");
}
}
public virtual float horizontal
{
get
{
return Input.GetAxis("Horizontal") * force;
}
}
public virtual float vertical
{
get
{
return Input.GetAxis("Vertical") * force;
}
}
// This is called every physics frame
void FixedUpdate ()
{
// If the object is grounded and isn't moving at the max speed or higher apply force to move it
if(rigidbody.velocity.magnitude < maxSpeed && grounded == true)
{
rigidbody.AddForce (transform.rotation * cam.transform.forward * vertical);
rigidbody.AddForce (transform.rotation * cam.transform.right * horizontal);
}
// This part is for jumping. I only let jump force be applied every 10 physics frames so
// the player can't somehow get a huge velocity due to multiple jumps in a very short time
if(jumpLimit < 10) jumpLimit ++;
if(jump && grounded && jumpLimit >= 10)
{
rigidbody.velocity = rigidbody.velocity + (Vector3.up * jumpSpeed);
jumpLimit = 0;
}
}
Your answer
Follow this Question
Related Questions
Camera relative movement 0 Answers
character turning where he looks 0 Answers
Newbie Question 2 Answers
Smooth Camera Movement script with problem. 0 Answers
Make a camera move towards player when the player moves out of the camera's vision 2 Answers