- Home /
Player controller rotate on mousedrag
I have a mouse orbit script locked to my player, and its actually two scripts that interact the camera to the player.
The player controller rotates based on right mouse button. if we could stop this from happening i would be satisfied with this script.
I've done a decent ammount of searching and testing different pieces of code. I simply cannot figure out how to remove the player rotate without messing up the rest of the player controller.
any assistance would be appreciated:
using UnityEngine;
using System.Collections;
public delegate void JumpDelegate ();
public class ThirdPersonControllerNET : MonoBehaviour
{
public Rigidbody target;
// The object we're steering
public float speed = 1.0f, walkSpeedDownscale = 2.0f, turnSpeed = 2.0f, mouseTurnSpeed = 0.3f, jumpSpeed = 1.0f;
// Tweak to ajust character responsiveness
public LayerMask groundLayers = -1;
// Which layers should be walkable?
// NOTICE: Make sure that the target collider is not in any of these layers!
public float groundedCheckOffset = 0.7f;
// Tweak so check starts from just within target footing
public bool
showGizmos = true,
// Turn this off to reduce gizmo clutter if needed
requireLock = true,
// Turn this off if the camera should be controllable even without cursor lock
controlLock = false;
// Turn this on if you want mouse lock controlled by this script
public JumpDelegate onJump = null;
// Assign to this delegate to respond to the controller jumping
private const float inputThreshold = 0.01f,
groundDrag = 5.0f,
directionalJumpFactor = 0.7f;
// Tweak these to adjust behaviour relative to speed
private const float groundedDistance = 0.5f;
// Tweak if character lands too soon or gets stuck "in air" often
private bool grounded, walking;
private bool isRemotePlayer = true;
public bool Grounded
// Make our grounded status available for other components
{
get
{
return grounded;
}
}
public void SetIsRemotePlayer(bool val)
{
isRemotePlayer = val;
}
void Reset ()
// Run setup on component attach, so it is visually more clear which references are used
{
Setup ();
}
void Setup ()
// If target is not set, try using fallbacks
{
if (target == null)
{
target = GetComponent<Rigidbody> ();
}
}
void Start ()
// Verify setup, configure rigidbody
{
Setup ();
// Retry setup if references were cleared post-add
if (target == null)
{
Debug.LogError ("No target assigned. Please correct and restart.");
enabled = false;
return;
}
target.freezeRotation = true;
// We will be controlling the rotation of the target, so we tell the physics system to leave it be
walking = false;
}
void Update ()
// Handle rotation here to ensure smooth application.
{
if (isRemotePlayer) return;
float rotationAmount;
if (Input.GetMouseButton (1) && (!requireLock || controlLock || Screen.lockCursor))
// If the right mouse button is held, rotation is locked to the mouse
{
if (controlLock)
{
Screen.lockCursor = true;
}
rotationAmount = Input.GetAxis ("Mouse X") * mouseTurnSpeed * Time.deltaTime;
}
else
{
if (controlLock)
{
Screen.lockCursor = false;
}
rotationAmount = Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime;
}
target.transform.RotateAround (target.transform.up, rotationAmount);
if (Input.GetKeyDown(KeyCode.Backslash) || Input.GetKeyDown(KeyCode.Plus))
{
walking = !walking;
}
}
float SidestepAxisInput
// If the right mouse button is held, the horizontal axis also turns into sidestep handling
{
get
{
if (Input.GetMouseButton (1))
{
float sidestep = -(Input.GetKey(KeyCode.Q)?1:0) + (Input.GetKey(KeyCode.E)?1:0);
float horizontal = Input.GetAxis ("Horizontal");
return Mathf.Abs (sidestep) > Mathf.Abs (horizontal) ? sidestep : horizontal;
}
else
{
float sidestep = -(Input.GetKey(KeyCode.Q) ? 1 : 0) + (Input.GetKey(KeyCode.E) ? 1 : 0);
return sidestep;
}
}
}
void FixedUpdate ()
// Handle movement here since physics will only be calculated in fixed frames anyway
{
grounded = Physics.Raycast (
target.transform.position + target.transform.up * -groundedCheckOffset,
target.transform.up * -1,
groundedDistance,
groundLayers
);
// Shoot a ray downward to see if we're touching the ground
if (isRemotePlayer) return;
if (grounded)
{
target.drag = groundDrag;
// Apply drag when we're grounded
if (Input.GetButton ("Jump"))
// Handle jumping
{
target.AddForce (
jumpSpeed * target.transform.up +
target.velocity.normalized * directionalJumpFactor,
ForceMode.VelocityChange
);
// When jumping, we set the velocity upward with our jump speed
// plus some application of directional movement
if (onJump != null)
{
onJump ();
}
}
else
// Only allow movement controls if we did not just jump
{
Vector3 movement = Input.GetAxis ("Vertical") * target.transform.forward +
SidestepAxisInput * target.transform.right;
float appliedSpeed = walking ? speed / walkSpeedDownscale : speed;
// Scale down applied speed if in walk mode
if (Input.GetAxis ("Vertical") < 0.0f)
// Scale down applied speed if walking backwards
{
appliedSpeed /= walkSpeedDownscale;
}
if (movement.magnitude > inputThreshold)
// Only apply movement if we have sufficient input
{
target.AddForce (movement.normalized * appliedSpeed, ForceMode.VelocityChange);
}
else
// If we are grounded and don't have significant input, just stop horizontal movement
{
target.velocity = new Vector3 (0.0f, target.velocity.y, 0.0f);
return;
}
}
}
else
{
target.drag = 0.0f;
// If we're airborne, we should have no drag
}
}
void OnDrawGizmos ()
// Use gizmos to gain information about the state of your setup
{
if (!showGizmos || target == null)
{
return;
}
Gizmos.color = grounded ? Color.blue : Color.red;
Gizmos.DrawLine (target.transform.position + target.transform.up * -groundedCheckOffset,
target.transform.position + target.transform.up * -(groundedCheckOffset + groundedDistance));
}
}
axis sidestep function appears to have something to do with the issue
I do have another question while i'm listening to this lecture!
What all will I need to do to replace the viking?
A new $$anonymous$$odel A new Rig A new set of animations(x8)
Things i'm unsure of: Since the viking is inside of the playerPrefab, will i need to change code inside this script to recognize a new model?
Also, the input manager should stay the same because these are accessed inside of the code above, no?
I have a model, i've replaced the viking with and for some reason my model did not spawn, nor did the camera attatch.
We try to keep each question to a single issue. You need to open this as a new question (and you'll get more people looking at a fresh question). You will also need to create a clearer explanation of how you have things structures and where the scripts are attached.
Answer by robertbu · Aug 27, 2013 at 10:38 AM
As a quick test change line 97 to:
if (false && Input.GetMouseButton (1) && (!requireLock || controlLock || Screen.lockCursor))
If that works to give you the behavior you want, then remove lines 97 - 180 and line 115.
What a reply! I'm at school right now, I appreciate the help!
I've updated the code but cannot access photon on this connection >.>
Attempting to add new comments aren't working out for me >.>
I couldn't quite figure out how to follow your instructions properly. However, you did help me figure out where the issue was and i simply switched actions and added a L$$anonymous$$B press to signal the default setup. Now, if you interact with R$$anonymous$$B press it gives a proper response, and Q,W,E,A,S,D all work properly but the issue now lies within the camera-snapping.
I can position my camera exactly how i want (looking behind me while running toward the camera in simulation of a mob kite) but whenever i let go of R$$anonymous$$B and am pushing any movement controlls I get snapped immediately.
WOOT! I figured it out!! thank you for your help!
Your answer
Follow this Question
Related Questions
Get the collider component of a child element 1 Answer
Photon Unity Networking (instantiated object syncing) 0 Answers
Object name not changing correctly (photon) 0 Answers
The name `tranform' does not exist in the current context 2 Answers
Stream Send receive variable via photon cloud in js script 1 Answer