I started with Unity a few days ago and figured the most important thig is to control a character, but that seems much harder than i thought.
I got a bit of a problem with my code, i want that my character can rotate along his own axes with “a” and “d” which is working fine. I also want my camera to rotate around different directions(but on the same level) of my character when moving the mouse to the left or right and that is not working at all.
At first the camera was child to the object but doing that it would always move along when “a” or “d” was pressed. When removing it the camera would not follow my character when moving. I figured what if i remove it, add an invisible object at the same position as my character and then add a slightly different movement script, but that seemed way to complicated and there must be an easier way. I tried different codes already and searched the forums to find something but nothing seemed to work, or it wasn’t javascript.
Please Help me! ![]()
PS: I apologize for spelling mistakes or grammatical errors it’s not my native language ![]()
This is the complete code i got so far the rotation part is on the bottom.
var walkSpeed:float;
var walkAnimSpeed:float;
var backSpeed:float;
var backAnimSpeed:float;
var strafeSpeed:float;
var strafeAnimSpeed:float;
var runSpeed:float;
var runAnimSpeed:float;
var idleSpeed:float;
var rotationSpeed:float;
var jumpforce:float;
private var charController:CharacterController;
private var gravity:float;
function Start(){
charController = GetComponent(CharacterController);
}
function Update () {
if(Input.GetAxis("Vertical") < 0.1){
animation['idle'].speed = idleSpeed;
animation.CrossFade("idle");
}
if(Input.GetAxis("Vertical") > 0){
if(Input.GetButton("Shift")){
animation['walk'].speed = runAnimSpeed;
animation.CrossFade("walk");
charController.Move(transform.forward * runSpeed * Time.deltaTime);
}
else{
animation['walk'].speed = walkAnimSpeed;
animation.CrossFade("walk");
charController.Move(transform.forward * walkSpeed * Time.deltaTime);
}
}
else if(Input.GetAxis("Vertical") < 0){
if(Input.GetButton("Shift")){
charController.Move(transform.forward * -runSpeed * Time.deltaTime);
}
else{
animation['back'].speed = backAnimSpeed;
animation.CrossFade("back");
charController.Move(transform.forward * -backSpeed * Time.deltaTime);
}
}
if(Input.GetAxis("Strafe"))
if(Input.GetKey("q")){
animation['Strafe_L'].speed = strafeAnimSpeed;
animation.CrossFade("Strafe_L");
charController.Move(transform.right * -strafeSpeed * Time.deltaTime);
// Debug.Log ("q");
}
else if(Input.GetAxis("Strafe"))
if(Input.GetKey("e")){
animation['Strafe_R'].speed = strafeAnimSpeed;
animation.CrossFade("Strafe_R");
charController.Move(transform.right * strafeSpeed * Time.deltaTime);
// Debug.Log ("e");
}
if(Input.GetButton("Jump")){
animation['Jump'].speed = jumpforce;
animation.CrossFade("Jump");
charController.Move(transform.right * -gravity * Time.deltaTime);
}
if(Input.GetAxis("Horizontal"))
if(Input.GetKey("a")){
transform.Rotate(Vector3(0, -rotationSpeed * Time.deltaTime, 0));
// Debug.Log ("a");
}
else if(Input.GetAxis("Horizontal"))
if(Input.GetKey("d")){
transform.Rotate(Vector3(0, rotationSpeed * Time.deltaTime, 0));
// Debug.Log ("d");
}
transform.RotateAround(Vector3(0, Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime, 0));
}