- Home /
Question by
KERNALDONUT · Jul 02, 2019 at 08:27 PM ·
c#camerarotationcontrols
I need help with TPS controls!
I am making a third-person doom-like game. The one thing I can't figure out is making the camera rotate around the character while making the character rotate it's self so it can move in other directions. I am using a capsule and a box collider.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public Rigidbody rb;
public GameObject camera;
public GameObject player;
public GameObject graphics;
public float speed;
// Update is called once per frame
void FixedUpdate()
{
if (Input.GetKey(KeyCode.W))
{
graphics.transform.position = new Vector3(player.transform.position.x, (graphics.transform.position.y + 1f), player.transform.position.z);
rb.AddForce(0, 0, speed * Time.deltaTime);
graphics.transform.position = new Vector3(player.transform.position.x, (graphics.transform.position.y - 1f), player.transform.position.z);
}
if (Input.GetKey(KeyCode.S))
{
graphics.transform.position = new Vector3(player.transform.position.x, (graphics.transform.position.y + 1f), player.transform.position.z);
rb.AddForce(0, 0, -speed * Time.deltaTime);
graphics.transform.position = new Vector3(player.transform.position.x, (graphics.transform.position.y - 1f), player.transform.position.z);
}
if (Input.GetKey(KeyCode.A))
{
graphics.transform.position = new Vector3(player.transform.position.x, (graphics.transform.position.y + 1f), player.transform.position.z);
rb.AddForce(-speed * Time.deltaTime, 0, 0);
graphics.transform.position = new Vector3(player.transform.position.x, (graphics.transform.position.y - 1f), player.transform.position.z);
}
if (Input.GetKey(KeyCode.D))
{
graphics.transform.position = new Vector3(player.transform.position.x, (graphics.transform.position.y + 1f), player.transform.position.z);
rb.AddForce(speed * Time.deltaTime, 0, 0);
graphics.transform.position = new Vector3(player.transform.position.x, (graphics.transform.position.y - 1f), player.transform.position.z);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
graphics.transform.Rotate(graphics.transform.rotation.x, (graphics.transform.rotation.y - 5f), graphics.transform.rotation.z);
player.transform.Rotate(player.transform.rotation.x, (player.transform.rotation.y - 5f), player.transform.rotation.z);
}
if (Input.GetKey(KeyCode.RightArrow))
{
graphics.transform.Rotate(graphics.transform.rotation.x, (graphics.transform.rotation.y + 5f), graphics.transform.rotation.z);
player.transform.Rotate(player.transform.rotation.x, (player.transform.rotation.y + 5f), player.transform.rotation.z);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Rotation seems to pass through condition checks 1 Answer
How to keep the player on the left side o the screen? 2 Answers
Gravity Modification And Camera Rotation Issue 1 Answer
C#. Rotation Help! 4 Answers