- Home /
Make character move according to camera position.
Hi. I'm new to unity (really new) and i'm having a problem trying to make the character move according to the camera view.
I used the transform.Translate to make the character move according to the global coordinates. but when I move the camera, i have that problem when I move the camera in which the forward direction is the backwards for the camera view. I wanted that it changed according to what I'm seeing on the camera. I searched everywhere, but the solutions I found either didn't work, or I didn't know how to adapt into the code I'm using (beucase of the inexperience, maybe)
The code I'm using on the character is:
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed = 10.0F;
public float jump = 3.0F;
public float run = 20.0F;
Animator anim;
void Start ()
{
rb = GetComponent<Rigidbody> ();
anim = GetComponent<Animator> ();
}
void Update()
{
float moveHorizontal = Input.GetAxis ("Horizontal") ;
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
if (movement != Vector3.zero)
{
transform.Translate (movement * speed * Time.deltaTime, Space.World);
anim.SetBool ("walk", true);
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(movement), 0.2f);
}
else
{
anim.SetBool ("walk", false);
}
if (movement != Vector3.zero && Input.GetButton("Fire2"))
{
transform.Translate (movement * run * Time.deltaTime, Space.World);
anim.SetBool ("fastRun", true);
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(movement), 0.2f);
}
else
anim.SetBool ("fastRun", false);
if (Input.GetButtonDown("Fire1"))
{
if (anim.GetBool ("walk") == false)
anim.SetTrigger ("Jump");
else
{
rb.AddForce (0.0f, jump, 0.0f, ForceMode.Impulse);
anim.SetTrigger ("Jump");
}
}
}
}
To move the camera i'm using:
public class CameraControl : MonoBehaviour {
public GameObject Player;
public float rotationSpeed = 4.0f;
private Vector3 offset;
void Start ()
{
offset = transform.position - Player.transform.position;
}
void LateUpdate ()
{
offset = Quaternion.AngleAxis (Input.GetAxis ("Mouse X") * rotationSpeed, Vector3.up) * offset;
offset = Quaternion.AngleAxis (Input.GetAxis ("Mouse Y") * rotationSpeed, Vector3.right) * offset;
transform.position = Player.transform.position + offset;
transform.LookAt (Player.transform.position);
}
}
Can anyone help me? (I'm also having a problem trying to set a limit to the quaternion.angleaxis, so the camera doesn't go through the ground, but one problem at a time).
Answer by Vicarian · Aug 25, 2017 at 04:23 PM
In general you'll parent the camera to the player's transform. If it's a first person game, you'll parent the camera somewhere in or near the character's head. To solve the issue with rotation going into the ground, look at Mathf.Clamp.
It's third person. Parenting the camera made it turn around when the player turms, but I only want the camera to turn around with the analog stick while accompaining the player (for that I used the second script).
The problem I'm having with the player movement is that if the camera is facing the wolrd forward, I can press up to go forward relative to the camera. But if it is facing the world backwards and I press up, the player go backwards relative to the camera, because his transform is oriented based on the world. I wanted it to always go forward relative to the camera when i press up, for example.
For the camera through the ground, should I use mathf.clamp with the transform.position? Can you give me an example?
I don't see a point where you're rotating the player in the playerController script. As far as mathf.clamp:
float m_rx = 0;
void Update() {
float my, ry;
Quaternion rotation = Quaternion.identity;
ry = Input.GetAxis("$$anonymous$$ouse X");
my = Input.GetAxis("$$anonymous$$ouse Y");
m_rx += my * m_speed;
// calculate horizontal rotation based on raw mouse input data
rotation = Quaternion.Euler(new Vector3(0, ry, 0));
// Apply horizontal rotation to player
transform.rotation *= rotation;
// Clamp rotation about the x axis to 180 degrees in front of the player (first - person) from mouse data
m_rx = $$anonymous$$athf.Clamp(m_rx, -90, 90);
// calculate vertical rotation
rotation = Quaternion.Euler(new Vector3(-m_rx, 0, 0));
// Update the camera parented to the player with the new vertical rotation
GetComponentInChildren<Camera>().transform.localEulerAngles =
new Vector3(-m_rx,
GetComponentInChildren<Camera>().transform.localEulerAngles.y,
GetComponentInChildren<Camera>().transform.localEulerAngles.z);
}
I'm rotating on line 22 and 32, with this code:
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(movement), 0.2f);
the problem is i'm moving with these codes:
float moveHorizontal = Input.GetAxis ("Horizontal") ; float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); transform.Translate (movement speed Time.deltaTime, Space.World);
then, when the camera is facing the back of the world, the character doesn't change the way it moves, so if I press up in the analog stick, he goes forward.
about the code you put, that is assu$$anonymous$$g the camera is a child of the player? I must put it as a child before using the code?
Your answer
Follow this Question
Related Questions
Character on Moving and Rotating Platform 0 Answers
Rigidbody Player Controller sticking to walls 0 Answers
character(ball) rolls with less speed when I build it 2 Answers
I have a movement script, but how can i make it so i can move half the speed while in the air. 0 Answers
Move Character to touched Position 2D (Without RigidBody and Animated Movement) 1 Answer