I’ve been working on a driving game and one of the features I’m trying to add is a driver view look script. What I want to happen is the user presses “q” to look right and “e” to look left out of the car. However the script I wrote causes the camera to spin forward on the X axis. Would someone please be kind enough to help out a n00b. Thank you in advance.
var LeftofCar : Transform;
var RightofCar : Transform;
function Update() {
if (Input.GetKeyDown(“q”)){
transform.LookAt(LeftofCar);
}
if (Input.GetKeyDown(“e”)){
transform.LookAt(RightofCar);
}else{
transform.Rotate( 3.508224,0,0);
}
}
How would I use that in my script to stop the random camera rotation? Can you give me an example? sorry if my question seems redundant but I’m new to unity.
unfortunately it still doesn’t work. However I added a Debug.Log to make sure that Unity detected the button presses and even after pressing the keys multiple times, it only recognizes one press. I’m not sure if this is a Unity error or something wrong with the script.
Its ok trying to teach my self unity has been a challenge for the past month but I have learned a lot form tutorials on the internet and advice from the entire unity community. However I still appreciate you trying to help.
I’m going to be blunt and suggest that you might be getting a little ahead of yourself in offering some of the solutions and code examples you’re offering. Your code examples neither compile nor do anything meaningful, and as such will likely only cause confusion for the OP and others who might read the thread. (Don’t worry, you’re not the first person to fall into this trap. I think most of us - or at least a lot of us - have gotten ahead of ourselves in this way at one time or another.)
You only have one weak point? You’re doing a lot better than me then
@The OP: A couple of things I noticed about your original code (added as comments):
function Update()
{
// Remember that GetKeyDown() only returns true during the update
// when the specified key was first pressed. The way you have your
// logic set up, the code 'transform.Rotate( 3.508224,0,0)' will
// execute almost every update. Since that code applies a relative
// rotation of ~3.5 degrees about the local x axis, the object will
// continually spin around said axis.
// Leaving aside the issue of interpolation for the time being, you
// probably want to use GetKey() here rather than GetKeyDown(), and
// you probably want to set the rotation directly in each of the
// three cases, the three cases being one key down, the other key down,
// and neither key down. (How to handle the case of both keys being down
// is up to you.)
if (Input.GetKeyDown("q")) {
transform.LookAt(LeftofCar);
}
if (Input.GetKeyDown("e")) {
transform.LookAt(RightofCar);
} else {
transform.Rotate( 3.508224,0,0);
}
}