- Home /
Why does my player object decide to ignore colliders whenever he feels like it? c#
ok here is my code for X movement...I'm making a sidescroller...I know the collision is taking place because I have an on collision event that stuns the player...but he just decides to ignore the collider of the bandit and thin walls...however I can't make the collider on the bandit any bigger because he is only so big...here is my codes
This one handles x movement and is located in the Update() function
float wx = Input.GetAxis("Horizontal") * Time.deltaTime * curspeed;
if(isstuned == 0)
{
transform.Translate(wx, 0, 0);
}
this is the one that handles collisions this is taking place...but for some reason he is not getting knocked back or stopped...(I don't think its the knockback code going the wrong way because it was ignoring collisions before I put this in)
IEnumerator OnCollisionEnter(Collision collision)
{
//with bandit
if (Time.time>nextDmg && collision.gameObject.tag == "Bandit")
{
nextDmg = Time.time + noDmg;
//if bandit is to the right
if (Physics.Raycast(transform.position,Vector3.right,10))
{
//take controls away from the player
isstuned -= 1;
//knock player to the left
rigidbody.velocity = -transform.right * knockback;
//minus 10 hp, and then print to console for debugging
hp -= BanditDmg;
print("-10 hp struck by bandit from the right"); //debug
yield return new WaitForSeconds(1);
//give controls back to the player
isstuned += 1;
}
//if bandit is to the left
if (Physics.Raycast(transform.position,Vector3.left,10))
{
//take controls away from the player
isstuned -= 1;
//knock player to the right
rigidbody.velocity = transform.right * knockback;
//minus 10 hp, and then print to console for debugging
hp -= BanditDmg;
print ("-10 hp struck by bandit from the left"); //debug
yield return new WaitForSeconds(1);
//give controls back to the player
isstuned += 1;
}
}
}
Answer by sneftel · Jul 11, 2011 at 08:21 PM
It's possible that the collision is occurring but neither of those raycasts is succeeding (because you're only doing a raycast from a single point). If you want to check which side the bandit is on, just compare the player's x-position with the bandit's.
how would I go about doing that? lol
cause that's what I originally wanted to do...but I couldn't do it cause I didn't know how so I just had to use a raycast lol
well, you clearly understand how to access a Transform
component; the position can be accessed as transform.position
, and the x component specifically can be accessed as transform.position.x
.
yeah...but how do you access the transform.position.x of the bandit from the player script? lol "gameObject.tag == "Bandit".transform.position.x" maybe?...but wouldn't that change the position of all the bandits and not the specific bandit I am trying to bounce named bandit1?
I suggest you look at the documentation for the Collision
class. It kind of sounds like you're treating things like gameobject.tag == "Bandit"
as magical incantations. Getting to know the objects you're using will help you out a lot here.
@deeredman1991 try looking through the highest rated questions on the site; they're pretty helpful and the first result is a huge list of tutorials. Additionally, just search "tutorials" here or "unity tutorials" on google and you're golden.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Instantiated Projectile IgnoreCollision Error 0 Answers
How to detect if collisions are being ignored unity 2018 2 Answers
Collider not continuing to Ignore CharacterController after center changes. 0 Answers
Alternatives to physics.ignorecollision() to ignore 2d collisions 0 Answers