- Home /
Not Run while Aiming; Slope Terrain Making Object Faster
Hey, I would like to request a little help here, somehow I got stuck with what I think must be really simple to fix and I'm just trying the wrong thing.
I am having two problems here right now.
First is related to a character animation and the speed while moving of that animation.
I use my own custom script, so it might not be complete as I am relatively new to Unity's scripting, used to be part of the art team.
The situation I have is:
I have one character (Player) for a third person game, so I have by default three basic ways to move around:
1) Just walk (A,W,S,D). 2) Run (hold Shift, the ''Run'' Axes). 3) Walk while aiming ( Right Mouse Buttom, the ''Aim'' Axes and A,W,S,D).
Walk and run are working just fine. The problem is the walk while aiming part. For gameplay's sake we don't want the character to be able to run around WHILE aiming, the movement must be the same as the walk part.
The problem is, as much as we were able to easily set up the character to walk while aiming with the walk speed, if we hold down the ''Run'' Axes (Shift) WHILE aiming, the character will then change movement speed to the run speed, not good for us.
I tried some ways to fix this, like using an ''if'' condition, to make the speed equal the walk speed if we hold ''Aim'' AND ''Run', no success, then I tried to set that same condition inside the condition of we holding ''Aim'', no success too.
Last, I set up it so, if we hold Aim and move to any direction we will call another ''if'' condition, checking if we are holding ''Run'', then we move at normal speed. No good.
I am a bit out of ideas now... I would really appreciate if someone could check the code and find the error, or just give me an idea of how to set the condition.
This is the code:
//Basic movement of an object with Translate
//This holds the character turning
//Rotation Variables
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
//Movement Variables
private var moveDirection : Vector3 = Vector3.zero;
private var controller: CharacterController;
function Start () {
var controller : CharacterController =
GetComponent(CharacterController); //Getting the character controller
}
function Update () {
var speed : float = 3.0;
var rotateSpeed : float = 3.0;
var controller : CharacterController =
GetComponent(CharacterController); //We here handle the walking moviment, considering only the directional buttom is being pressed.
// Move forward
if (Input.GetAxisRaw ("Up"))
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var forwardSpeed : float = speed * Input.GetAxis ("Up");
controller.SimpleMove(forward *
forwardSpeed);
// Move Backward
if (Input.GetAxisRaw ("Down"))
var backward : Vector3 = transform.TransformDirection(Vector3.forward);
var backwardSpeed : float = speed * Input.GetAxis ("Down");
controller.SimpleMove(-backward *
backwardSpeed);
// Move Right
if (Input.GetAxisRaw ("Right"))
var right : Vector3 = transform.TransformDirection(Vector3.right);
var rightSpeed : float = speed * Input.GetAxis ("Right");
controller.SimpleMove(right *
rightSpeed);
// Move Left
if (Input.GetAxisRaw ("Left"))
var left : Vector3 = transform.TransformDirection(Vector3.right);
var leftSpeed : float = speed * Input.GetAxis ("Left");
controller.SimpleMove(-left *
leftSpeed);
// Here we make sure that the run animation play if we hold down shift
either with an animation already playing or not
//Run Forward
if(((Input.GetAxisRaw ("Run")) && (Input.GetAxisRaw ("Up")))
|| (Input.GetAxisRaw ("Up")) &&
(Input.GetAxisRaw (("Run"))))if (Input.GetAxisRaw ("Up"))
var forwardrun : Vector3 = transform.TransformDirection(Vector3.forward);
var forwardrunSpeed : float = speed * Input.GetAxis ("Up");
controller.SimpleMove(forwardrun *
forwardrunSpeed);
//Run Backward
if(((Input.GetAxisRaw ("Run")) && (Input.GetAxisRaw
("Down"))) || (Input.GetAxisRaw ("Down")) && (Input.GetAxisRaw (("Run"))))
if (Input.GetAxisRaw ("Down"))
var backwardrun : Vector3 = transform.TransformDirection(Vector3.forward);
var backwardrunSpeed : float = speed * Input.GetAxis
("Down"); controller.SimpleMove(-backwardrun * backwardrunSpeed);
//Run Left
if(((Input.GetAxisRaw ("Run")) && (Input.GetAxisRaw
("Left"))) || (Input.GetAxisRaw ("Left")) && (Input.GetAxisRaw (("Run"))))
if (Input.GetAxisRaw ("Left"))
var leftrun : Vector3 = transform.TransformDirection(Vector3.right);
var leftrunSpeed : float = speed * Input.GetAxis ("Left");
controller.SimpleMove(-leftrun *
leftrunSpeed);
//Run Right
if(((Input.GetAxisRaw ("Run")) && (Input.GetAxisRaw
("Right"))) || (Input.GetAxisRaw ("Right")) && (Input.GetAxisRaw (("Run"))))
if (Input.GetAxisRaw ("Right"))
var rightrun : Vector3 = transform.TransformDirection(Vector3.right);
var rightrunSpeed : float = speed * Input.GetAxis ("Right");
controller.SimpleMove(rightrun *
rightrunSpeed);
//It is important that the character do not run while aiming.
//We must set up that the character will only be able to move
//at the walking speed while aiming
//Aim and Walk Forward
if(((Input.GetAxisRaw ("Aim")) && (Input.GetAxisRaw ("Up")))
|| (Input.GetAxisRaw ("Up")) &&
(Input.GetAxisRaw (("Aim"))))if (Input.GetAxisRaw ("Run"))
controller.SimpleMove(forward);
//Aim and Walk Backward
if(((Input.GetAxisRaw ("Aim")) && (Input.GetAxisRaw
("Down"))) || (Input.GetAxisRaw ("Down")) && (Input.GetAxisRaw (("Aim"))))
if (Input.GetAxisRaw ("Run"))
controller.SimpleMove(-backward);
//Aim and Walk Left
if(((Input.GetAxisRaw ("Aim")) && (Input.GetAxisRaw
("Left"))) || (Input.GetAxisRaw ("Left")) && (Input.GetAxisRaw (("Aim"))))
if (Input.GetAxisRaw ("Run"))
controller.SimpleMove(-left);
//Aim and Walk Right
if(((Input.GetAxisRaw ("Aim")) && (Input.GetAxisRaw
("Right"))) || (Input.GetAxisRaw ("Right")) && (Input.GetAxisRaw (("Aim"))))
if (Input.GetAxisRaw ("Run"))
controller.SimpleMove(right);
//We must make sure the character will always face the camera so we set
it to rotate along the camera.
if(Input.GetAxisRaw ("Mouse X"))
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
var rotation = Quaternion.Euler(y, x, 0);
transform.rotation = rotation;
}
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);
}
@script RequireComponent(CharacterController)
This one is my most urgent problem, but there is another one.
I have an idea of what is going wrong here, but I would like someone to make a small check and give me an idea, for I really don't know how to fix this. I found another similar question but it didn't work...
What I have is:
Whenever my character walk down a slope, no matter the angle, it gets some insane speed up. I think what is actually happening is the character is ''falling'', using the gravity power to move at such fast speed.
I tried to chance the slope limit on the character controller, but no real success, this one I really have no idea on how to solve...
For both cases, I use the same controller above.
Here is a video showing what exactly happens ingame:
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
HeadLookController 0 Answers
Apply gravity 2 Answers
Character fall animation 1 Answer
Problem with character movement 2d when attaching animator 1 Answer