These are the two codes i am currently using, one for my gameObject and the other is for a third person camera.
Player controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent ();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 12)
{
winText.text = "You Win";
}
}
}
And this is the code for the third person camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour {
private const float Y_ANGLE_MIN = 0.0f;
private const float X_ANGLE_MAX = 50.0f;
public Transform lookAt;
public Transform camTransform;
private Camera cam;
private float distance = 10.0f;
private float currentX = 0.0f;
private float currentY = 0.0f;
private float sensitivityX = 4.0f;
private float sensitivityY = 1.0f;
// Use this for initialization
void Start () {
camTransform = transform;
cam = Camera.main;
}
// Update is called once per frame
private void Update () {
currentX += Input.GetAxis ("Mouse X");
currentY += Input.GetAxis ("Mouse Y");
currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN, X_ANGLE_MAX);
}
private void LateUpdate () {
Vector3 dir = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
camTransform.position = lookAt.position + rotation * dir;
camTransform.LookAt (lookAt.position);
}
}
There isn't an issue with necessarily both, the rotation for the third person camera is good and the movement for the sphere is good as well, I need help with merging the two together so that when the camera angle changes the direction keys for the sphere change along with the camera.,Currently these are the two scripts that i am using:
For the player controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent ();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 12)
{
winText.text = "You Win";
}
}
}
And then for the Third Person Camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour {
private const float Y_ANGLE_MIN = 0.0f;
private const float X_ANGLE_MAX = 50.0f;
public Transform lookAt;
public Transform camTransform;
private Camera cam;
private float distance = 10.0f;
private float currentX = 0.0f;
private float currentY = 0.0f;
private float sensitivityX = 4.0f;
private float sensitivityY = 1.0f;
// Use this for initialization
void Start () {
camTransform = transform;
cam = Camera.main;
}
// Update is called once per frame
private void Update () {
currentX += Input.GetAxis ("Mouse X");
currentY += Input.GetAxis ("Mouse Y");
currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN, X_ANGLE_MAX);
}
private void LateUpdate () {
Vector3 dir = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
camTransform.position = lookAt.position + rotation * dir;
camTransform.LookAt (lookAt.position);
}
}
Rotating the camera isn't an issue and neither is the control, but i am trying to merge the two together where the controls will work continuously with the direction the camera is facing. Please help
↧