Ok, after deciding that my old control scheme wasn’t working… I abandoned my game for a while, turned back to it and started fiddling around with it. What I decided to do was to make my control scheme similar to “Savage: Battle for Newerth”'s control scheme. I really didn’t want to copy someone else’s control scheme, but my old one wasn’t working and was really messing with any progress with my shooting mechanism (which I can just use a look-at-camera method and have it fire in reverse… which actually fires forwards).
Why I’m looking to replicate the Savage rotation script is because it keeps the camera behind the character at all times, and it allows me to control my character a lot easier than what I had (and add a free-look which allows me to rotate the camera without the character rotating by holding the CTRL button or something out of the way) Now, what the Savage camera does, is that the character rotates depending on the mouse’s movement along the axises. So basically, if I move my mouse towards the right side of the screen, my character will rotate towards the right (and always stay facing away from the camera).
Problem is, that even if I have the same value for the speeds, my rotation is slower than my camera rotation, and it would be very tedious to try and find the EXACT value to get the rotations to be the same, and it may not be the exact value. The Mouse-Look script may work for an FPS (where you don’t see your pill of a character, like in the tutorial), but I need the entire character to rotate, not a weapon.
Changing the FPSWalker script to react to mouse movement:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var rotateSpeed = 250;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime, 0);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
I’ve tried a look-away script that I found, but what happens is that my character rotates in random directions O.o
All you need to do is manually place the camera as a child of the GameObject with the FPSWalker attached, then set the camera to the proper distance and angle behind the player, then, as the Player G/O w/ the walker script rotates, the camera will rotate with it since it stays in the same local position within the Player G/O.
Camera’s already a child of my player, but if you notice with the mouse-look script, it only rotates the camera. In the FPS tutorial, only the weapon rotates because the weapon is a child of the camera, the “pill” does not. I need my entire character to rotate, not just its invisible weapon (which will soon be a bubble blower that appears from hammerspace X3).
That’s what I was looking for, Thank you. I don’t need up and down looking, as the head-bone is tricky to animate in Unity, and it’s meant to be cartoony anyways (locking the up and down rotation of the camera within the head movement will work too). That’ll certainly get it working, I’ll try it this afternoon after my capstone (that is, if I don’t decide to do it for my capstone X3)
It might just be Unity 3 instead of 2, but it doesn’t see ‘cam’ as an identifier. Is that a name for your camera in your scene or is it meant to be the name of the camera script.
If it’s the name of the camera script, it’s telling me “An instance of type ‘UnityEngine.Component’ is required to access non static member ‘transform’.”
And if I switch ‘cam’ to ‘camera’, it tells me “You are not allowed to call get_camera when declaring a variable.
Move it to the line after without a variable declaration.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.”
It’s the only spot that’s causing a snag.
EDIT: Ah, nevermind, I looked at my Bubbleblower script, I don’t need to worry too much about it, as the camera-look thing works in that script, I can just take examples from there and re-structure it.
you’d then set that to whichever ccamera it is you’re using to direct the player (very likely Camera.main)
using cam instead of Camera.main is slightly more optimized, is why I wrote it like that. I wasn’t even thinking; I should have included that in my post. >…<