- Home /
Need help with CharacterController script
So basicaly I have a script on my player that when I am jumping the player is moving in the same direction he was before he jumped, it is working good, but I want to add additional feature when player is in the air and he presses either left or right he stops moving in the dirrection he was before jumping. I hope you understand what I mean.
this is the part of script wich is moving the player in direction he was before jumping :
moveVector.x = lastMotion.x;
and if i change it to :
moveVector.x = inputDirection;
then i can move while i'm jumping but I don't want that I want the feature that player is going the direction he was last in and it's locked, but he stops when he is pressing input direction.
Answer by Ardaaytac · Aug 31, 2016 at 05:40 AM
Attach this script to your ground object . Not your player character,
public bool IsGrounded;
ex. Class Name = GroundHit
In this script create:
public static GroundHit instance;
void Awake()
{
instance = this;
}
void OnCollisionStay (Collision collisionInfo)
{
IsGrounded = true;
}
void OnCollisionExit (Collision collisionInfo)
{
IsGrounded = false;
}
I didnt understand you clearly but if you want to add something when your player is on air use a boolean and set it true when it is not colliding with ground and set false when it exit the ground. and use this bool in your update function with GetKey. Hope understand you right.
Then go to your PlayerController script and use that on update function.
example:
void Update()
{
if(!GroundHit.instance.IsGrounded && Input.GetKey(KeyCode.P)
//Make something.
}
Answer by trevorchico · Aug 31, 2016 at 07:54 AM
@fonetic Not sure how your characterController script is set up but couldn't you just stop the movement of the player in mid air?
IF player is in air AND left or right is pressed, freeze position.
you could temporarily put constraints on the rigid body or just freeze the x&z coordinates
if(input.getkeyDown(left or right)) && transform.position.y>1) GetComponent().constraints = RigidbodyConstraints.FreezePositionX; GetComponent().constraints = RigidbodyConstraints.FreezePositionZ;
or create a temporary Vector3 that is set when you hold down the left or right key, then set the X&Y transform.positions to those values.
Answer by CapitalLlama · Aug 30, 2016 at 06:22 PM
If I understand you correctly, you want it so that if the play is running to the right, jumps, and then presses left, the player will no longer be moving to the right and fall straight down?
If that is the case then you can just do this
if(inputDirection != moveVector.x)
moveVector.x = 0;
That will basically just say if the player ever presses a direction he isn't going, then stop moving on the x axis.
This probably won't be 100% correct since I don't see the rest of your code, but it should point you in the right direction.
Answer by Dream_in_code · Aug 31, 2016 at 06:35 AM
So you want to flip the player? if he jumps right and you press left you want it to jump to left? it thats the case use this.
float masSpeed;//moving speed
float move = Input.GetAxis("Horizontal");
myAnim.SetFloat("speed", Mathf.Abs(move)); //playing animation
myRB.velocity = new Vector2(move * maxSpeed, myRB.velocity.y);
if (move > 0 && !facingRight)
{
flip();
}
else if (move < 0 && facingRight)
{
flip();
}
}
void flip()
{
facingRight = !facingRight;
Vector2 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
Your answer
