Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Camera Error when moving backwarts

Camera Error when moving backwarts

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Camera Error when moving backwarts on Thu Jul 28, 2016 2:13 am

Garzec


Member [Lv. 2]
Member [Lv. 2]
Hi,
im trying to build a playercontrol in the style of the old N64 / PS1 games. The controller is fine but if u turn around and walk to the "user" the camera starts to bug. This only happens if u turn by the angle of 180, if you walk on the side its fine.

I just took a short video of it:

https://sendvid.com/aquy54le

The camera Script is:
Code:

public GameObject target;
    public float damping = 1;
    Vector3 offset;
    void Start()
    {
            offset = target.transform.position - transform.position;
    }
    void LateUpdate()
    {
            float currentAngle = transform.eulerAngles.y;
            float desiredAngle = target.transform.eulerAngles.y;
            float angle = Mathf.LerpAngle(currentAngle, desiredAngle, Time.deltaTime * damping);
            Quaternion rotation = Quaternion.Euler(0, angle, 0);
            transform.position = target.transform.position - (rotation * offset);
            transform.LookAt(target.transform);
    }

I dont think the Error depends on the playerControl because the player moves correctly without the camera Script. Anyway here is the Playercontroller:

Code:

// movementSpeed
    public float speed = 5;
    // jumpSpeed
    public float jumpPower = 15;
    // multiplier while jumping
    public float airMultiplier = 0.25f;
    // rotating
    protected float rotationSpeed = 450.0f;
    // camera object
    public Transform cameraTransform;
    // velocity
    private Vector3 velocity;
    // character
    private Transform thisTransform;
    // char Controller
    private CharacterController character;
    // character transform
    private Transform characterTransform;
    // Starting Point
    void Start()
    {
            // Reference
            thisTransform = GetComponent<Transform>();
            // Reference
            character = GetComponent<CharacterController>();
            // Reference
            characterTransform = character.transform;
    }
    // Character Look Direction
    void FaceMovementDirection()
    {
            // direction to look
            Vector3 targetDirection = character.velocity;
            targetDirection.y = 0.0f;
            // direction to move
            Vector3 moveDirection = characterTransform.forward;
            // rotate
            if (targetDirection != Vector3.zero)
            {
                    moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotationSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
                    moveDirection = moveDirection.normalized;
                    thisTransform.forward = moveDirection;
            }
    }
    void Update()
    {
            // inputs
            Vector3 movementInputs = new Vector3(0, 0, 0);
            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
            {
                    movementInputs.z = 1.0f;
            }
            else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
            {
                    movementInputs.z = -1.0f;
            }
            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
            {
                    movementInputs.x = -1.0f;
            }
            else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
            {
                    movementInputs.x = 1.0f;
            }
            // movement
            Vector3 movement = cameraTransform.TransformDirection(movementInputs);
            movement.y = 0;
            movement.Normalize();
            movement *= speed * ((movementInputs.x != 0.0f || movementInputs.z != 0.0f) ? 1.0f : 0.0f);
            // ground check
            if (character.isGrounded)
            {
                    // jump
                    if (Input.GetKeyDown(KeyCode.Space))
                    {
                            velocity = character.velocity;
                            velocity.y = jumpPower;
                    }
            }
            else
            {
                    // get back on the ground
                    velocity.y += Physics.gravity.y * Time.deltaTime;
                    movement.x *= airMultiplier;
                    movement.z *= airMultiplier;
            }
            movement += velocity;
            movement += Physics.gravity;
            movement *= Time.deltaTime;
            // move
            character.Move(movement);
            // don't move in the air
            if (character.isGrounded)
                    velocity = Vector3.zero;
            // rotate the character
            FaceMovementDirection();
    }

Just hoping that someone gets to know what to do here Smile

View user profile

2 Re: Camera Error when moving backwarts on Sat Jul 30, 2016 8:00 am

Garzec


Member [Lv. 2]
Member [Lv. 2]
Does no one got any idea =?

View user profile

3 Re: Camera Error when moving backwarts on Mon Aug 08, 2016 10:30 am

jakeyiii


Member [Lv. 3]
Member [Lv. 3]
Hello

We see that you have some issues with your project/script, however we can help you for a price. If you would like us to work for you then please feel free to contact us at http://RenntekStudios.co.uk or on Skype at RenntekStudios thanks

Our prices are very cheap. Little errors we can fix for free, otherwise projects we can discuss prices. Hope you get in touch shortly thank you...

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum