- Home /
Character controls broken due to mouse looking script
EDIT: SOLVED After removing the Box Collider from the Player object, the issue is gone
I'm following a youtube series on how to create a top-down shooter. The tutorials use c#, and to teach myself to script better I've been re-writing it in JS.
The problem is that when I try and move around, the character moves seemingly randomly all over the place. Here's my code:
#pragma strict
@script RequireComponent(CharacterController);
//Components
var controller : CharacterController;
var cam : Camera;
//System
var motion : Vector3;
var input : Vector3;
var targetRotation : Quaternion;
var mousePos : Vector3;
//Handling
var rotationSpeed : float = 450;
var walkSpeed : float = 5;
var runSpeed : float = 8;
function Start()
{
cam = Camera.main;
}
function Update()
{
mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint(Vector3(mousePos.x,mousePos.y,cam.transform.position.y - transform.position.y));
targetRotation = Quaternion.LookRotation(mousePos - Vector3(transform.position.x,0,transform.position.z));
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y,targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
input = Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
motion = input;
motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1)?.7f:1;
motion *= (Input.GetButton("Run"))?runSpeed:walkSpeed;
motion += Vector3.up * -8;
controller.Move(motion * Time.deltaTime);
}
EDIT: SOLVED After removing the Box Collider from the Player object, the issue is gone
Could you add that as an Answer and then accept it as correct so people co$$anonymous$$g across this problem again can find the solution and this will also remove the Question from the Unanswered list.
Answer by Brontupisto · May 15, 2014 at 05:06 AM
After removing the Box Collider from the Player object, the issue is gone
Your answer
Follow this Question
Related Questions
Moving your player to another location onTrigger 0 Answers
Vertical mouse input inverted when player is turned around 0 Answers
Keeping Character From Walking Through Walls 1 Answer
auto run key 1 Answer
Making my look script turn smoothly 0 Answers