- Home /
Problem is not reproducible or outdated, closed it himself
Making enemy push player
Hi guys!
I have this code here:
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
The "enemy" only circle around player, so what I want is the enemy pushes player so he can fall off.
Thanks is advance!!
Are the NPCs fully rotating to face the player? Try using Lerp ins$$anonymous$$d of Slerp, and/or increase the rotationSpeed.
I tried Lerp, but nothing happened.
To be more specific, I have a sphere which represents enemy, and sphere is moving towards Player. When sphere come closer, it just circle around my Player character, and doesn't push me at all.
Does your player and your enemy both have rigidbody components attached?
Edit : If you are using a character controller, you may want to look at the physics character controllers :
http://wiki.unity3d.com/index.php/PhysicsFPSWalker
http://wiki.unity3d.com/index.php/RigidbodyFPSWalker
from : http://wiki.unity3d.com/index.php/Scripts/Controllers
Yes, both Player and Enemy have RigidBody attached.
But I'm trying to enemy push Player, which have default FPS script attached, in your case it was just an Sphere with no movement scripts in it.
Found this in Documentation:
The Controller does not react to forces on its own and it does not automatically push Rigidbodies away.
If you want to push Rigidbodies or objects with the Character Controller, you can apply forces to any object that it collides with via the OnControllerColliderHit() function through scripting.
On the other hand, if you want your player character to be affected by physics then you might be better off using a Rigidbody ins$$anonymous$$d of the Character Controller.
$$anonymous$$aybe you guys can help me now... I'm new in Unity.
Thanks for your time. :)
Can you guys please help me with this? I really need enemy to push Character off the platform, otherwise it's useless.