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 /
  • Help Room /
avatar image
0
Question by SimonMK7 · Jun 20, 2020 at 01:05 AM · movementrotatebuttonskeyboard input

Rotate The Player in a given direction

I originally posted this on the Unity Forums but I'll go ahead and post it here as well.

Hello, i'm currently creating a top-down 3D shooter project and am at the testing phase where I'm testing the player's movement and so far it seems to be working well. I've gotten the camera to follow the player and have also implemented a way to allow the player to be able to shoot (even though it might still need a little bit of work). I also wanted to add a way to be able to have the player press the ABXY controls on a gamepad in order to rotate the player object/mesh and face a specific direction without it affecting the current directional movement (ex. using the directional pad or key input press for movement and using the abxy buttons to rotate/face the player object in said direction). However, this is where I'm currently having trouble as so far I don't exactly know how to make a player face a specific direction.

i've tryed using PlayerObj.transform.Rotate (Vector3.up RotationSpeed Time.deltaTime); but this only causes the PlayerObject to rotate around rather than a given direction.

I'll go ahead and share the code that I have so that anyone can get a better idea of what I currently have.

Edit: also apologies for my beginner-esque way of coding, I am just a beginner when it comes to Unity and am trying to further improve both in Unity and C# as a whole.


Player script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Player : MonoBehaviour
 {
     [Header("Player Camera")]
     [Tooltip("Select the camera to follow the player")]
     public GameObject camera;

     [Header("Player Movement Values")]
     [Tooltip("Main Player Object")]
     public GameObject PlayerObj;

     [Tooltip("Player Obj Movement Speed")]
     public float movementSpeed;

     [Tooltip("Player Mesh Rotation Speed")]
     public float RotationSpeed;

     private Rigidbody rb;
     private Vector3 direction;

     //public Vector3 Movement;

     [Header("Player Bullet Component")]
     [Tooltip("Bullet Spawn Point Object")]
     public GameObject bulletSpawnPoint;
     public GameObject Bullet;
     public float waitTime;
     private Transform BulletSpawned;

     void Start()
     {
         rb = GetComponent<Rigidbody> ();
     }

     void _Shoot()
     {
         BulletSpawned = Instantiate (Bullet.transform, bulletSpawnPoint.transform.position, Quaternion.identity);
         BulletSpawned.rotation = bulletSpawnPoint.transform.rotation;
     }

     void Movement()
     {
         float horizontalMove = Input.GetAxisRaw ("Horizontal");
         float verticalMove = Input.GetAxisRaw ("Vertical");

         direction = new Vector3 (x: horizontalMove, y: 0.0f, z: verticalMove);

         if (direction != Vector3.zero)
         {
             transform.rotation = Quaternion.Slerp (a: transform.rotation, b: Quaternion.LookRotation (direction), t: RotationSpeed * Time.deltaTime);
         }
         rb.MovePosition (transform.position + movementSpeed * Time.deltaTime * direction);
     }

     // Update is called once per frame
     void Update ()
     {
         Movement ();

         if (Input.GetKey (KeyCode.L) || (Input.GetKey (KeyCode.R)))
         {
             _Shoot ();
         }
         /*if (Input.GetKey (KeyCode.A))
         {
         PlayerObj.transform.Rotate (Vector3.up * RotationSpeed * Time.deltaTime);
         }*/
     }
 }


PlayerCamera script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class PlayerCamera : MonoBehaviour
 {
     [Header("Variables")]
     public Transform player;
     public float smooth = 0.3f;
     public float height;

     private Vector3 velocity = Vector3.zero;
  
     // Update is called once per frame
     void Update ()
     {
         Vector3 pos = new Vector3 ();
         pos.x = player.position.x;
         pos.y = player.position.y + height;
         pos.z = player.position.z - 1f;
 
         transform.position = Vector3.SmoothDamp (transform.position, pos, ref velocity, smooth);
     }
 }


Bullet script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Bullet : MonoBehaviour
 {
     [Header("Variables")]
     public float Speed;
 
     public float maxDistance;
  
     // Update is called once per frame
     void Update ()
     {
         transform.Translate (Vector3.forward * Time.deltaTime * Speed);
         maxDistance += 1 * Time.deltaTime;
          
         if (maxDistance >= 2)
             Destroy (this.gameObject);
     }
 }
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

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

transform.translate limits 1 Answer

EventTrigger on button not recognizing instantsiated player 1 Answer

Moving.Rotating a Raycast with Respect to Rotation 1 Answer

Push the objects when collide with other object 0 Answers

how i rotate my player suing the same joystick that i use for moveing 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