Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by YankoxD · Feb 10, 2021 at 12:46 AM · rotationthird person controllerlookrotationcontrollerslookatmouse

third person controller like in Assasian's creed or GTA5

Hello unity community i am trying to make a third person game where the player moves like in assasians creed. I am making somewhere in my code logic a mistake and i have spend 73h trying to fix my code. My character keeps rotating in funny ways. For now i have character with a sphere attached like child object and in the sphere the main camera. The sphere acts like axis where the camera rotates around the player and its positioned in the middle of the character. What i am trying to do is: 1. Free look while not pressing WSAD around the player. 2. When i press W the character should rotate to where the camera is facing and start walking 3. If i press A/S/D the character should walk left backwards and right without camera moving to its walking direction Here is my code for now

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class mouseLook : MonoBehaviour
 {
 
 
     public float mouseSpeed = 100f;
     public Transform camOrbit; //sphere inside the player
     public Transform playerBody; //player
     float xRotation = 0f;
     float yRotation = 0f;
     void Start()
     {
         Cursor.lockState = CursorLockMode.Locked;
         //Cursor.visible = false;
     }
     void Update()
     {
 
    
     float mouseX = Input.GetAxis("Mouse X") * mouseSpeed * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSpeed * Time.deltaTime;
         
 
 
         xRotation -= mouseY;
         xRotationPlayer -= mouseY;
         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
         yRotation += mouseX;
         yRotationPlayer += mouseX;
 
         camOrbit.transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
         if (Input.GetKeyDown(KeyCode.W))
        {
             playerBody.transform.localRotation = Quaternion.Euler(0f, yRotationPlayer, 0f);
         }
        if (Input.GetKey(KeyCode.W))
         {
            playerBody.transform.localRotation = Quaternion.Euler(0f, yRotationPlayer, 0f);
             camOrbit.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
             
        }
         if (Input.GetKeyUp(KeyCode.W))
        {
 
             yRotation = 0f;
         }
 
 
         
         if (Input.GetKey(KeyCode.A))
         {
            
                playerBody.transform.localRotation = Quaternion.Euler(0f, -90f, 0f);
 
            
        }
        if (Input.GetKey(KeyCode.D))
         {
 
             playerBody.transform.localRotation = Quaternion.Euler(0f, 90f, 0f); ;
 
        }
         if (Input.GetKey(KeyCode.A))
        {
 
            playerBody.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
          
        }
 
 
 
    }
 }
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

152 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 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 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 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

Synchronising the rotation around Y axis of two objects 1 Answer

How does LookRotation work in relation to ray cast? 1 Answer

Rotation Question in Top-down 3D Game 0 Answers

Align Key to Keyhole?? 0 Answers

FPS Controller rotation,FPS Controller rotation problem 0 Answers


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