- Home /
Character move in camera direction
I have two characters, one in first person and other in third person. How to make the character in the third person move in the direction of where the camera of first person is?
When the camera rotates, the third person player still moves in the same direction.
using UnityEngine;
using System.Collections;
public class MoverScripts : MonoBehaviour {
//Variables
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal2"), 0, Input.GetAxis("Vertical2"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed;
//Jumping
if (Input.GetButton("Jump2"))
moveDirection.y = jumpSpeed;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
Please help me I'm beginner
Answer by Vire · Jan 21, 2014 at 03:00 AM
I achieved what you're after with similar to the above code as well as the following lines:
desiredRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredRotation, rotateSpeed * 10 * Time.deltaTime);
My rotateSpeed was set to 50f.
Error desiredRotation and rotateSpeed does not exist in the current context and error in Quaternion XD what is bad?
You need to declare it.. private Quaternion desiredRotation = Quaternion.identity; This goes at the top just under your private Vector3 moveDirection = Vector3.zero; ... You might want to start with something easier and learn the fundamentals of program$$anonymous$$g first.
Your answer
Follow this Question
Related Questions
[C#] Movement Direction Change 2 Answers
How not to penetrate the walls? 3 Answers
Multiple Cars not working 1 Answer
How can I limit the rotation on the Y axis so the player cant spin the camera 360 in an FPS game? 0 Answers
[C#] Jump on slopes 1 Answer