Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by KuPAfoo · Aug 27, 2013 at 05:55 AM · photonthird person controllermouse-drag

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KuPAfoo · Aug 27, 2013 at 04:46 PM 0
Share

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.

avatar image robertbu · Aug 27, 2013 at 04:49 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KuPAfoo · Aug 27, 2013 at 03:05 PM 0
Share

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

avatar image KuPAfoo · Aug 27, 2013 at 09:23 PM 0
Share

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.

avatar image KuPAfoo KuPAfoo · Aug 27, 2013 at 08:30 PM 0
Share

WOOT! I figured it out!! thank you for your help!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges