Object Rotate with Camera
I have game where I have a rigid body cylinder on a terrain. I have the Main Camera in mouse orbit with the cylinder, (using the MouseOrbit.js with cylinder as target). I also added a script so when the 'W' key is pressed, the cylinder moves in one direction.
#pragma strict
var speed : float;
var target : GameObject;
function Start () {
}
function Update () {
if (Input.GetKey(KeyCode.W)){
transform.Translate(-Vector3.forward*speed*Input.GetAxis("Vertical"));
}
}
What do I add to make the cylinder's direction change with the camera's direction so that the forward direction is pointing away from the camera. So basically it's like a third person control.
So basically it needs to be like a third person player when 'W' is pressed, the cylinder needs to move away from the camera and the mouse changes the direction.
Thanks in advance.
Buddy, time to update your standart assets package and import Cameras package, there you find many work ready camera setups, or build camera rig based on these scripts.
Also what script would I be looking for and what do I exactly do.
Sorry but you haven't replied yet. I need to know which script.
Answer by Abhiroop-Tandon · Apr 04, 2016 at 08:41 AM
Put the camera as a child of your player and it should work !
Which script? For the controls did nothing but not allow me to move forward. The $$anonymous$$ouse Orbit script only just made the camera stay in one place.
Look if you place the camera as a child of the player, the movement and rotation of the child depends on the parent (the player). So i dont get why it is not working for you, you should be doing something wrong.
Answer by LieutenantIvan · Apr 05, 2016 at 08:59 AM
I have decided just to make 'A' and 'D' rotate the cylinder.
#pragma strict
var speed1 : float = 0.2;
var speed2 : float = 1;
var target : GameObject;
function Start () {
}
function Update () {
if (Input.GetKey(KeyCode.W)){
transform.Translate(Vector3.forward*speed1);
}
if (Input.GetKey(KeyCode.D)){
transform.Rotate(Vector3.up*speed2);
}
if (Input.GetKey(KeyCode.A)){
transform.Rotate(-Vector3.up*speed2);
}
}
However anymore input on how to solve this is very much welcomed.
Your answer
Follow this Question
Related Questions
Camera rotation with mouse cursor 0 Answers
Rotate ball in camera's direction.... 0 Answers
Make the camera rotate around the player and face the player. 1 Answer
Rotate towards mouse pointer 1 Answer