I looked around for a WoW like camera/movement system and found a thread with dozens of examples. I took one, copied it and it worked beautifully until I tried right clicking. In WoW when you right-click the character will follow wherever the mouse is, however this one does nothing, and I am met with a null reference exception. I looked throughout that entire thread but nobody had a problem with it.
Here’s the part that’s wrong :
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(1)) {
transform.rotation = Quaternion.Euler(Camera.main.transform.eulerAngles.x,Camera.main.transform.eulerAngles.y,0);
}
else {
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}
Here’s the entire script if needed:
// Movement Variables:
var jumpSpeed:float = 9.0;
private var gravity:float = 20.0;
static var runSpeed:float = 4.0;
static var swimSpeed:float = 2.0;
static var walkSpeed:float = 1.7;
static var runSpeedsave = 4.0;
static var walkSpeedsave = 3.7;
static var curSpeed:float = 0.0;
private var rotateSpeed:float = 150.0;
private var grounded:boolean = false;
private var moveDirection:Vector3 = Vector3.zero;
static var isWalking:boolean = false;
private var moveStatus:String = "idle";
private var xSpeed = 250.0;
private var ySpeed = 120.0;
private var yMinLimit = -40;
private var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
static var ddistance:float = 0.0;
static var strafemod:Vector3 = Vector3.zero;
static var strafing = false;
static var animationspeed = 1.0;
var hit : RaycastHit;
static var isSwimming = false;
static var bmove = false;
static var bSpeed = 0.0;
//
//UPDATE
//
function Update ()
//check if we fell down the world and teleport to a specific location
{
if(transform.position.y < -200)
{
transform.position.y = 16.07609;
transform.position.x = 579.2826;
transform.position.z = 130.8261;
}
//Move controller
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
//check if we're moving backwards
if(Input.GetAxis("Vertical") < 0){
bmove = true;
}
else{
bmove = false;
}
//check if we're swimming
if(isSwimming == true){
swimSpeed = runSpeed/2;
//reduce speed when moving backwards
if(bmove == true){
if(bSpeed > 0){
bSpeed = bSpeed;
}
else{
bSpeed = swimSpeed;
swimSpeed = bSpeed * 0.6;
}
}
else{
if(bSpeed > 0){
swimSpeed = bSpeed;
bSpeed = 0.0;
}
}
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
if(Input.GetMouseButton(1) && Input.GetAxis("Vertical") && Input.GetAxis("Horizontal")) {
moveDirection *= .7;
}
animationspeed = runSpeed/4;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= swimSpeed;
moveStatus = "swim_idle";
if(moveDirection != Vector3.zero) {
moveStatus = "swimming";
//invoke swim animation here
}
else {
//swim idle animation
}
// Jump (or rather dive upwards)!
if(Input.GetButton("Jump"))
{
// call JUMP animation here
moveDirection.y = swimSpeed*1.6;
}
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(1)) {
transform.rotation = Quaternion.Euler(Camera.main.transform.eulerAngles.x,Camera.main.transform.eulerAngles.y,0);
}
else {
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}
//simple strafing
//now let's do the quick check if we're walking or running
//walking goes here
//left strafe button is pressed -> strafe to the left
if (Input.GetButton("Strafel")){
strafemod = new Vector3(-1, 0, 0);
strafemod = transform.TransformDirection(strafemod);
strafemod = swimSpeed * strafemod;
controller.Move (strafemod * Time.deltaTime);
}
//right strafe button is pressed -> strafe to the right
if (Input.GetButton("Strafer")){
strafemod = new Vector3(1, 0, 0);
strafemod = transform.TransformDirection(strafemod);
strafemod = swimSpeed * strafemod;
controller.Move (strafemod * Time.deltaTime);
}
}
else{
//check if we're moving
if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0 || Input.GetButton("Strafel") || Input.GetButton("Strafer")){
if(isWalking == true){
curSpeed = walkSpeed;
}
else{
curSpeed = runSpeed;
}
}
else{
curSpeed = 0.0;
//since the backward slowing can have some permanent effects to our runSpeed and walkSpeed (i.e. the speed will stick to 60%) here's some failsave code making use of the run/walk Speedsave variables
if(runSpeed < runSpeedsave){
runSpeed = runSpeedsave;
}
if(walkSpeed < walkSpeedsave){
walkSpeed = walkSpeedsave;
}
}
//reduce speed when moving backwards
if(isWalking == true){
if(bmove == true){
if(bSpeed > 0){
bSpeed = bSpeed;
}
else{
bSpeed = walkSpeed;
walkSpeed = bSpeed * 0.6;
}
}
else{
if(bSpeed > 0){
walkSpeed = bSpeed;
bSpeed = 0.0;
}
}
}
else{
if(bmove == true){
if(bSpeed > 0){
bSpeed = bSpeed;
}
else{
bSpeed = runSpeed;
runSpeed = bSpeed * 0.6;
}
}
else{
if(bSpeed > 0){
runSpeed = bSpeed;
bSpeed = 0.0;
}
}
}
//check if we're moving - if we're moving track distance and save to ddistance
if(curSpeed > 0){
ddistance = ddistance + (curSpeed * Time.deltaTime);
}
//
// Only allow movement and jumps while ----------------- GROUNDED -------------
if(grounded) {
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
if(Input.GetMouseButton(1) && Input.GetAxis("Vertical") && Input.GetAxis("Horizontal")) {
moveDirection *= .7;
}
animationspeed = runSpeed/4;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= isWalking ? walkSpeed : runSpeed;
moveStatus = "idle";
if(moveDirection != Vector3.zero) {
moveStatus = isWalking ? "walking" : "running";
if (isWalking){
// invoke WALK animation here
} else {
// call RUN animation here
}
} else {
// call IDLE animation here
}
// Jump!
if(Input.GetButton("Jump"))
{
// call JUMP animation here
moveDirection.y = jumpSpeed;
}
}
// END "IS GROUNDED"
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(1)) {
transform.rotation = Quaternion.Euler(Camera.main.transform.eulerAngles.x,Camera.main.transform.eulerAngles.y,0);
}
else {
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}
// Toggle walking/running with the walk/run key
if(Input.GetButtonDown("walk"))
isWalking = !isWalking;
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
//var controller:CharacterController = GetComponent(CharacterController);
//var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
//simple strafing
//now let's do the quick check if we're walking or running
//walking goes here
if(isWalking == true){
//left strafe button is pressed -> strafe to the left
if (Input.GetButton("Strafel")){
strafemod = new Vector3(-1, 0, 0);
strafemod = transform.TransformDirection(strafemod);
strafemod = walkSpeed * strafemod;
controller.Move (strafemod * Time.deltaTime);
}
//right strafe button is pressed -> strafe to the right
if (Input.GetButton("Strafer")){
strafemod = new Vector3(1, 0, 0);
strafemod = transform.TransformDirection(strafemod);
strafemod = walkSpeed * strafemod;
controller.Move (strafemod * Time.deltaTime);
}
}
//running goes here
else{
//left strafe button is pressed -> strafe to the left
if (Input.GetButton("Strafel")){
strafemod = new Vector3(-1, 0, 0);
strafemod = transform.TransformDirection(strafemod);
strafemod = runSpeed * strafemod;
controller.Move (strafemod * Time.deltaTime);
}
//left strafe button is pressed -> strafe to the right
if (Input.GetButton("Strafer")){
strafemod = new Vector3(1, 0, 0);
strafemod = transform.TransformDirection(strafemod);
strafemod = runSpeed * strafemod;
controller.Move (strafemod * Time.deltaTime);
}
}
}
};
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
// -------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------- END UPDATE --------------------------------
// -------------------------------------------------------------------------------------------------------------
@script RequireComponent(CharacterController)
What could be wrong?