- Home /
Player using turret problem
Hi, i'm trying to create a stationary turret that players use it like gears of war. Video: https://youtu.be/Sx-hXfFdDi0?t=24s Except in my case my game is a topdown view like mostly tower defenses (not a fps or third person, so camera will not change).
So far i have this code in PlayerMovement:
//called at fixedupdate
public void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
// Perform the raycast and if it hits something on the floor layer...
if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
{
Vector3 playerToMouse = floorHit.point - transform.position;
// Ensure the vector is entirely along the floor plane.
playerToMouse.y = 0f;
Quaternion newRotatation = Quaternion.LookRotation(playerToMouse);
// Set the player's rotation to this new rotation.
playerRigidbody.MoveRotation(newRotatation);
}
}
At my TowerPlayerUser script i have:
//called once when player is stay in collider.
public void StuckTurrent(Collider other)
{
join = other.gameObject.AddComponent<FixedJoint>();
join.connectedBody = this.GetComponentInParent<Rigidbody>();
join.breakForce = float.PositiveInfinity;
join.breakTorque = float.PositiveInfinity;
join.enableCollision = false;
join.enablePreprocessing = true;
player = other.gameObject;
player.transform.position = grabPosition.position;
player.transform.LookAt(grabPosition.position);
}
My player can get closer, touch and use the tower. What is wrong is when mouse's cursor get closer enought of turret or player, the turns is going crazy and my camera going crazy too. So the error finishes with my player char floating whatever space...
Can someone help me how to fix this or if i'm doing it in wrong way, tell me what is best way to do it?
Thanks in advance
Answer by Dave-Hampson · May 04, 2015 at 07:26 AM
Maybe you could have a dead zone where if the mouse cursor is close to the player, it doesn't cause a rotation.
Or another option would be to rotate the turret "manually" without creating an actual physics "link".