For some strange reason whenever i move my mouse up the player moves forward and when i look down it moves backwards i believe it is one of the following scripts that is the problem but i’m not sure. this is not my code but is from an asset pack.
using UnityEngine;
using System.Collections;
public class FirstPersonCharacter : MonoBehaviour
{
[SerializeField] private float runSpeed = 8f; // The speed at which we want the character to move
[SerializeField] private float strafeSpeed = 4f; // The speed at which we want the character to be able to strafe
[SerializeField] private float jumpPower = 5f; // The power behind the characters jump. increase for higher jumps
[SerializeField] private AdvancedSettings advanced = new AdvancedSettings(); // The container for the advanced settings ( done this way so that the advanced setting are exposed under a foldout
[SerializeField] private bool lockCursor = true;
[System.Serializable]
public class AdvancedSettings // The advanced settings
{
public float gravityMultiplier = 1f; // Changes the way gravity effect the player ( realistic gravity can look bad for jumping in game )
public PhysicMaterial zeroFrictionMaterial; // Material used for zero friction simulation
public PhysicMaterial highFrictionMaterial; // Material used for high friction ( can stop character sliding down slopes )
public float groundStickyEffect = 5f; // power of 'stick to ground' effect - prevents bumping down slopes.
}
private CapsuleCollider capsule; // The capsule collider for the first person character
private const float jumpRayLength = 0.7f; // The length of the ray used for testing against the ground when jumping
public bool grounded { get; private set; }
private Vector2 input;
private IComparer rayHitComparer;
void Awake ()
{
// Set up a reference to the capsule collider.
capsule = GetComponent<Collider>() as CapsuleCollider;
grounded = true;
rayHitComparer = new RayHitComparer();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
void OnDisable()
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
void Update()
{
if (Input.GetMouseButtonUp(0))
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
public void FixedUpdate ()
{
float speed = runSpeed;
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool jump = Input.GetButton("Jump");
input = new Vector2( h, v );
// normalize input if it exceeds 1 in combined length:
if (input.sqrMagnitude > 1) input.Normalize();
// Get a vector which is desired move as a world-relative direction, including speeds
Vector3 desiredMove = transform.forward * input.y * speed + transform.right * input.x * strafeSpeed;
// preserving current y velocity (for falling, gravity)
float yv = GetComponent<Rigidbody>().velocity.y;
// add jump power
if (grounded && jump) {
yv += jumpPower;
grounded = false;
}
// Set the rigidbody's velocity according to the ground angle and desired move
GetComponent<Rigidbody>().velocity = desiredMove + Vector3.up * yv;
// Use low/high friction depending on whether we're moving or not
if (desiredMove.magnitude > 0 || !grounded)
{
GetComponent<Collider>().material = advanced.zeroFrictionMaterial;
} else {
GetComponent<Collider>().material = advanced.highFrictionMaterial;
}
// Ground Check:
// Create a ray that points down from the centre of the character.
Ray ray = new Ray(transform.position, -transform.up);
// Raycast slightly further than the capsule (as determined by jumpRayLength)
RaycastHit[] hits = Physics.RaycastAll(ray, capsule.height * jumpRayLength );
System.Array.Sort (hits, rayHitComparer);
if (grounded || GetComponent<Rigidbody>().velocity.y < jumpPower * .5f)
{
// Default value if nothing is detected:
grounded = false;
// Check every collider hit by the ray
for (int i = 0; i < hits.Length; i++)
{
// Check it's not a trigger
if (!hits[i].collider.isTrigger)
{
// The character is grounded, and we store the ground angle (calculated from the normal)
grounded = true;
// stick to surface - helps character stick to ground - specially when running down slopes
//if (rigidbody.velocity.y <= 0) {
GetComponent<Rigidbody>().position = Vector3.MoveTowards (GetComponent<Rigidbody>().position, hits[i].point + Vector3.up * capsule.height*.5f, Time.deltaTime * advanced.groundStickyEffect);
//}
GetComponent<Rigidbody>().velocity = new Vector3(GetComponent<Rigidbody>().velocity.x, 0, GetComponent<Rigidbody>().velocity.z);
break;
}
}
}
Debug.DrawRay(ray.origin, ray.direction * capsule.height * jumpRayLength, grounded ? Color.green : Color.red );
// add extra gravity
GetComponent<Rigidbody>().AddForce(Physics.gravity * (advanced.gravityMultiplier - 1));
}
//used for comparing distances
class RayHitComparer: IComparer
{
public int Compare(object x, object y)
{
return ((RaycastHit)x).distance.CompareTo(((RaycastHit)y).distance);
}
}
}
the next possible one
using UnityEngine;
public class MouseRotator : MonoBehaviour {
// A mouselook behaviour with constraints which operate relative to
// this gameobject's initial rotation.
// Only rotates around local X and Y.
// Works in local coordinates, so if this object is parented
// to another moving gameobject, its local constraints will
// operate correctly
// (Think: looking out the side window of a car, or a gun turret
// on a moving spaceship with a limited angular range)
// to have no constraints on an axis, set the rotationRange to 360 or greater.
public Vector2 rotationRange = new Vector3(70,70);
public float rotationSpeed = 10;
public float dampingTime = 0.2f;
public bool autoZeroVerticalOnMobile = true;
public bool autoZeroHorizontalOnMobile = false;
public bool relative = true;
Vector3 targetAngles;
Vector3 followAngles;
Vector3 followVelocity;
Quaternion originalRotation;
// Use this for initialization
void Start () {
originalRotation = transform.localRotation;
}
// Update is called once per frame
void Update () {
// we make initial calculations from the original local rotation
transform.localRotation = originalRotation;
// read input from mouse or mobile controls
float inputH = 0;
float inputV = 0;
if (relative)
{
inputH = Input.GetAxis("Mouse X");
inputV = Input.GetAxis("Mouse Y");
// wrap values to avoid springing quickly the wrong way from positive to negative
if (targetAngles.y > 180) { targetAngles.y -= 360; followAngles.y -= 360; }
if (targetAngles.x > 180) { targetAngles.x -= 360; followAngles.x-= 360; }
if (targetAngles.y < -180) { targetAngles.y += 360; followAngles.y += 360; }
if (targetAngles.x < -180) { targetAngles.x += 360; followAngles.x += 360; }
// with mouse input, we have direct control with no springback required.
targetAngles.y += inputH * rotationSpeed;
targetAngles.x += inputV * rotationSpeed;
// clamp values to allowed range
targetAngles.y = Mathf.Clamp ( targetAngles.y, -rotationRange.y * 0.5f, rotationRange.y * 0.5f );
targetAngles.x = Mathf.Clamp ( targetAngles.x, -rotationRange.x * 0.5f, rotationRange.x * 0.5f );
} else {
inputH = Input.mousePosition.x;
inputV = Input.mousePosition.y;
// set values to allowed range
targetAngles.y = Mathf.Lerp ( -rotationRange.y * 0.5f, rotationRange.y * 0.5f, inputH/Screen.width );
targetAngles.x = Mathf.Lerp ( -rotationRange.x * 0.5f, rotationRange.x * 0.5f, inputV/Screen.height );
}
// smoothly interpolate current values to target angles
followAngles = Vector3.SmoothDamp( followAngles, targetAngles, ref followVelocity, dampingTime );
// update the actual gameobject's rotation
transform.localRotation = originalRotation * Quaternion.Euler( -followAngles.x, followAngles.y, 0 );
}
}
thanks