- Home /
help on several mechanics
I'm creating a game and I'm encountering several issues. None are errors, but they aren't functioning the way I want them too.
I have a crouching, walking and sprinting mechanic. When I walk or sprint the character motor updates properly. However, when I crouch, the character motor doesn't update despite using the same code (just using different values).
I'm using a debug.drawray so I can see a visual representation of what the enemy sees. However, whenever the enemy rotates, the line doesn't line-up properly with the z-axis.
Vector3 losLeft = transform.TransformDirection(Quaternion.Euler(0, -fieldOfViewRange, 0) * transform.forward).normalized * drawLine;
Vector3 losRight = transform.TransformDirection(Quaternion.Euler(0, fieldOfViewRange, 0) * transform.forward).normalized * drawLine;
Debug.DrawRay(transform.position, losLeft, Color.green); Debug.DrawRay(transform.position, losRight, Color.green);
I have a crossbow and when it fires, it doesn't get stuck to a wall. Both the arrow and the wall have a collider. The bolt has a tag and I enable kinematic, translate it forward and then parent it to the object it hits.
Answer by Bunny83 · Nov 20, 2013 at 04:54 AM
What should we do about No 1?
No 2 is quite simple ;) You used TransformDirection but don't passed in a local direction but the worldspace direction. This would result in a double rotation. So either use TransformDirection and Vector3.forward instead of transform.forward, or just remove the TransformDirection.
ps: "drawLine" is the perfect example of a misnamed variable ;)
No 3 is almost like No 1 however slightly more information here. How do you "translate" the arrow? With "Translate()"? That would bypass the physics system and you don't get any collisions. Just attach a Rigidbody, set drag to 0, disable gravity and set the initial velocity (either directly or by using AddForce).
Thanks #2 is solved. However, still having an issue with #3. Does both the arrow and the wall need a rigidbody or only one of them? I've applied it both and disabled gravity and drag is set a zero.
Here's the code, maybe I'm missing something:
void OnCollisionEnter(Collision hit)
{
float depth = 0.3f;
if(hit.transform.tag == "Bolt")
{
hit.rigidbody.is$$anonymous$$inematic = true; //stop physics
//hit.transform.Translate(depth * Vector3.forward); //arrow peneration (took it out like you said)
hit.transform.parent = transform; //attach it to the enemy
}
}
This answer might help you with #3:
http://answers.unity3d.com/questions/462907/how-do-i-stop-a-projectile-cold-when-colliding-wit.html
$$anonymous$$aybe you want to share your "issue with #3" with us? What exactly happens? Does the arrow move? Does it stop at the target? If so, what's your actual problem...
The people here are quite good at guessing what people want to ask, but out of thin air there's not much we can do ;)
BTW, is the script you posted above on your arrow / bolt? If so it doesn't make much sense to check the object it hit for the tag "bolt". That would make it work only when hitting another bolt. Unless your name is Robin Hood that's a quite rare case :P
To answer you question in the comment above:
Only the moving object (probably the bolt / arrow) needs a Rigid body. However both objects need a collider, the bolt as well as the wall you want to hit.