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.

  1. 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).

  2. 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);

  3. 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.

What should we do about No 1?

No 2 is quite simple :wink: 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 :wink:

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).