A script that doesn't allow moving objects to go through non-moving objects?
I have a player, a zombie, and walls. The player does not go through walls or the zombie. The zombie goes through walls. Movement script:
var target : Transform;
var moveSpeed = 20;
var rotationSpeed = 5;
var myTransform : Transform;
function Awake() {
myTransform = transform;
}
function Start() {
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
I no longer care what the script is, as long as it works. Can someone please respond?
Answer by Statement · Dec 21, 2015 at 01:36 AM
I suggest you reuse the movement script from the player as it already works as intended.
Then, make the zombie control the movement script to navigate toward the player, and make sure your player has a script that control the movement script on the player.
PlayerInput directs Movement script on Player game object
ZombieAI directs Movement script on Zombie game object
I guess your Movement script already controls a rigidbody or something, so make sure both the Zombie and Player have the necessary colliders and rigidbodies and whatever else it needs.
It sounds like movement is pretty much the same with the thing that differ being input method. One is algorithmic (Zombie) and one is driven by input (Player). Makes sense?
The movement script for the player was an imported package from the Standard Assets folder in the Asset Store. As I am not familiar with that script, I'm not sure I can successfully alter it in any way. It also takes into account a head bob and a jump. Would a DontGoThroughThings alternative simply be an OnCollisionEnter > (move back a bit) also be sufficient? I would have to research some more, but would that be possible?
Sure, but you could also capsulecast ahead of you before you move too. It gets a little tricky to make something like "OnCollisionEnter > (move back a bit)" because it'll mean you can't slide along walls (Like with capsule cast: if you are about to touch anything, you won't move forward. Like with your proposal: if you touch anything, you will move backward)
To slide along walls, you need to get the normal of the contact and figure out the slide from there. And by the way, if your world has complex geometry, be careful when you "push back out" or "apply sliding" to not slide through yet another wall.
I guess its up to you to decide to familiarize yourself with the standard asset scripts to see if it is modular already or if you should use rigidbodies (that automatically deal with collision) or make your own physics response calculation. I dunno which saves you most time and trouble, but going with something that already works sounds like a good place to begin, to avoid creating something that might be buggy if not done properly.
I get what you're saying, but the "OnCollisionEnter>(move back a bit)" was actually a last resort. They both have rigidbodies already. I'll try my own script to see if it really works well. The only real thing I'm trying to do right now is avoid defying physics. Thanks
Your answer
Follow this Question
Related Questions
Stopping Moving Objects? 0 Answers
Trigger activating before entering trigger. What do I do? 0 Answers
Jump is higher then normal 0 Answers
how can I add Sprint into my Script ?,How can I add Sprint function ?? 0 Answers
3D Trajectory Prediction 3 Answers