Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help With Mouse Look

Discussion in 'Scripting' started by Redstoneperson, Aug 27, 2019.

  1. Redstoneperson

    Redstoneperson

    Joined:
    Aug 26, 2019
    Posts:
    11
    Currently, the movement script I have lets you look left and right with a, and d, but I want a mouse script, that rotates the camera, (Inside of the player) , depending on where you point your mouse at, its a third person game, I also want the character to rotate with it, but the character can only rotate left and right, here is the movement script I have, so if you need it to fix this problem, here it is...

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class PlayerControllerRigidBody : MonoBehaviour {
    private string MoveInputAxis = "Vertical";
    private string TurnInputAxis = "Horizontal";
    public float rotationRate = 90;
    public float moveRate = 90;
    private Rigidbody rb;
    private void Start()
    {
    rb = GetComponent<Rigidbody>();
    }
    private void Update ()
    {
    float moveAxis = Input.GetAxis(MoveInputAxis);
    float turnAxis = Input.GetAxis(TurnInputAxis);
    ApplyInput(moveAxis, turnAxis);
    }
    private void ApplyInput(float moveInput,
    float turnInput)
    {
    Move(moveInput);
    Turn(turnInput);
    }
    private void Move(float input)
    {
    rb.AddForce(transform.forward * input * moveRate, ForceMode.Force);
    }
    private void Turn(float input)
    {
    transform.Rotate(0, input * rotationRate * Time.deltaTime, 0);
    }
    }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    Please use code tags.

    If you want help debugging your script, you should explain what currently happens when you run the script and how that is different from the desired effect.
     
    eses likes this.
  3. Redstoneperson

    Redstoneperson

    Joined:
    Aug 26, 2019
    Posts:
    11
    I never said there was a error looool
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @Redstoneperson

    You can rotate camera using mouse by using something like this:

    Code (CSharp):
    1. var mrotX = Input.GetAxis("Mouse X") * turnspeed;
    2. var mrotY = Input.GetAxis("Mouse Y") * turnspeed;
    This way you can still lock the Cursor, so it is not visible.

    I can't make sense of what you mean by "but the character can only rotate left and right,". I guess you mean that only the heading of character changes with camera heading (so tilt naturally doesn't affect it).

    For your character, you could make its forward direction the same as current camera forward (heading part) when W/up control axis is activated. Then again, when you only press A/D, you might allow only the character rotate left/right while camera stays looking forward, or you make camera auto rotate so that it is always behind the player.

    "I never said there was a error looool"

    BTW - that is not very polite, considering you asked for help.
     
  5. Redstoneperson

    Redstoneperson

    Joined:
    Aug 26, 2019
    Posts:
    11
    Thank you I will try it later..
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    So you solved your own problem before you posted, and this entire thread is a deliberate waste of everyone's time?
     
  7. Redstoneperson

    Redstoneperson

    Joined:
    Aug 26, 2019
    Posts:
    11
    legit I wanted someone to help make a script, there wasn't a error in the first place idiot... I was asking for someone to give a working script, you are just dumb, and can't read my post, I said I wanted help and someone could give me a mouse look script, there was no error anytime, I never said there was, and when I told you that you said I was wasting people's time, AND YOU STILL DIDN'T REREAD THE POST.
     
  8. Redstoneperson

    Redstoneperson

    Joined:
    Aug 26, 2019
    Posts:
    11
    Also, when the character can only turn left and right, I like mean that, it can't look up or down, but I want it to stay like that, the camera will move with the character always, so I want the character to rotate also, but if you move the camera up or down, it only moves the camera, thanks!