- Home /
Object Movement in Camera facing direction
Hi,
I'm new to Unity and right now trying to learn the basics. I have successfully created a sphere, sphere movements, a 3rd person camera, camera rotation around the sphere. The only thing is that I want the sphere to move in the direction that the camera is facing by changing the axes values according to the camera.
And I have tried many methods, so any help would be nice. Thank you.
Player Controller Code:
public class Playercontroller1 : MonoBehaviour {
public GameObject emptyobject;
public float speed=0;
private Vector3 offset;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
//count = 0;
//setcountText ();
//win.text = "";
}
void Update()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement*speed);
}
}
using UnityEngine; using System.Collections;
Camera Controller Code:
public class cameracontroller1 : MonoBehaviour {
public GameObject player;
private Vector3 offset;
private float x_rotation = 0.0f;
private float y_rotation = 0.0f;
public float x_sensitivty = 5.0f;
public float y_sensitivty = 5.0f;
public float speed=5;
public Transform target;
public Rigidbody rb;
void Start ()
{
offset = transform.position - player.transform.position;
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
transform.position = player.transform.position + offset;//Camera position behind ball
x_rotation += Input.GetAxis ("Mouse X") * x_sensitivty;//Mouse input X axis
y_rotation -= Input.GetAxis ("Mouse Y") * y_sensitivty;//Mouse input Y axis
transform.LookAt(target);//Look at the ball
transform.RotateAround (target.transform.position, Vector3.up, x_rotation*speed);//Rotate the camera around the ball in x axis
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612034333im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stop 3rd person camera going throught walls ? 1 Answer
3rd Person Camera,How would I make it so that the character will turn the way of the camera? 0 Answers
transform.rotation and localRotation automatically reset after rotating. 1 Answer
Guiding a sphere 2 Answers
How to move player in direction where the camera is aiming ? 1 Answer