Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 rjpiston · Apr 17, 2020 at 09:27 AM · movementcharactercontrollerplayer movement

Trying to add 45deg strafing to my Character Controller.

Below is my Character Controller script. Currently the character rotates towards the mouse and I can move it using WASD in relation to the mouse position. (ie. W moves the character towards the mouse, S moves character away from mouse). I can strafe left and right along the players X axis, however I can't seem to get it to move forward/backwards as well as left/right. Essentially I have no 45deg angles. Let me know if you need anymore information.

 using UnityEngine;
 
 public class PlayerController03 : MonoBehaviour
 {
     public float rotateSpeed = 0.2f;
     public float walkSpeed = 3f;
     public float walkBackwardsSpeed = 2f;
     public float runSpeed = 6f;
     public float speedSmoothTime = 0.1f;
     public float gravity = -12f;
     public float jumpHeight = 1f;
 
     Animator animator;
     Camera cam;
     CharacterController controller;
 
     private Vector2 input;
     private Vector2 inputDir;
 
     bool aiming;
     bool running;
     float currentSpeed;
     float speedSmoothVelocity; 
     Vector3 velocity;
     float velocityY = 0f;
   
     void Start()
     {
         controller = GetComponentInChildren<CharacterController>();
         animator = GetComponentInChildren<Animator>();
         cam = Camera.main;
     }
 
     void Update()
     {
         //maps raw input to normalized vector2
         input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
         inputDir = input.normalized;
 
         //Grabs aiming and running 
         aiming = Input.GetMouseButton(1);
         running = Input.GetKey(KeyCode.LeftShift);
 
         //gets the current speed of the player depending on whether they're running or walking and dampens
         float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
 
         //activates aiming animation
         if(aiming){ 
             animator.SetBool("isAiming", true);
         }else{
             animator.SetBool("isAiming", false);
         }
 
         if(Input.GetKey(KeyCode.Space)){
             Jump();
         }
 
         //creates a ray to the current mouse position and rotates the player towards it
         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
         int layer_mask = LayerMask.GetMask("Ground");
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit, 100, layer_mask)){
             Vector3 targetDir = hit.point - transform.position;
             Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, rotateSpeed, 0.0f);
             if(inputDir != Vector2.zero || aiming)
                 transform.rotation = Quaternion.LookRotation(new Vector3(newDir.x, 0, newDir.z));
         }
         
         //sets the initial velocity, but this is when the player is standing still, but allows to jump in place
         velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
         //sets the velocity of the player based on input
         
         if(inputDir == Vector2.up)
             velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
         if(inputDir == Vector2.down)
             velocity = -transform.forward * currentSpeed + Vector3.up * velocityY;
         if(inputDir == Vector2.right)
             velocity = transform.right * currentSpeed + Vector3.up * velocityY;
         if(inputDir == Vector2.left)
             velocity = -transform.right * currentSpeed + Vector3.up * velocityY;
         if(inputDir == Vector2.zero){
             velocity = Vector3.Lerp(velocity, Vector3.zero, speedSmoothTime);
         }
         // Debug.Log(velocity);
     }
 
     void FixedUpdate(){
         Debug.Log(velocity);
         controller.Move(velocity * Time.deltaTime);
         currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
         velocityY += Time.deltaTime * gravity;
         if(controller.isGrounded){
             velocityY = 0;
         }
     }
 
     void Jump(){
         if(controller.isGrounded){
             float jumpVelocity = Mathf.Sqrt(-2*gravity * jumpHeight);
             velocityY = jumpVelocity;
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sp33dzer0 · Apr 20, 2020 at 08:51 AM

You have it set up so that it can recognize directional movement, but not diagonal movement. Here is some samples code from a project I did that had 45* movement.

 public void ShipMovement()
     {
         //Ship Movement
         float movementH = Input.GetAxis("Horizontal");
         float movementV = Input.GetAxis("Vertical");
 
         if (movementH == 0 && movementV == 0)
             Stop();
         else if (movementH < 0 && movementV == 0)
             MoveLeft();
         else if (movementH > 0 && movementV == 0)
             // print("Right");
             MoveRight();
         else if (movementV < 0 && movementH == 0)
             MoveDown();
         else if (movementV > 0 && movementH == 0)
             MoveUp();
         else if (movementV > 0 && movementH < 0)
             MoveUpLeft();
         else if (movementV > 0 && movementH > 0)
             MoveUpRight();
         else if (movementV < 0 && movementH < 0)
             MoveDownLeft();
         else if (movementV < 0 && movementH > 0)
             MoveDownRight();
         void Stop()
         {
             rb.velocity = new Vector2(0, 0);
         }
         void MoveLeft()
         {
             rb.velocity = new Vector2(-1, 0).normalized * fixedSpeed;
         }
         void MoveRight()
         {
             rb.velocity = new Vector2(1, 0).normalized * fixedSpeed;
         }
         void MoveUp()
         {
             rb.velocity = new Vector2(0, 1).normalized * fixedSpeed;
         }
         void MoveDown()
         {
             rb.velocity = new Vector2(0, -1).normalized * fixedSpeed;
         }
         void MoveUpLeft()
         {
             rb.velocity = new Vector2(-1, 1).normalized * fixedSpeed;
         }
         void MoveUpRight()
         {
             rb.velocity = new Vector2(1, 1).normalized * fixedSpeed;
         }
         void MoveDownLeft()
         {
             rb.velocity = new Vector2(-1, -1).normalized * fixedSpeed;
         }
         void MoveDownRight()
         {
             rb.velocity = new Vector2(1, -1).normalized * fixedSpeed;
         }
         shipPosition = this.transform.position;
         shipPosition.x = Mathf.Clamp(shipPosition.x, -horEdge, horEdge);
         shipPosition.y = Mathf.Clamp(shipPosition.y, -verEdge, verEdge);
         transform.position = shipPosition;
Comment
Add comment · Share
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

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

200 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

Related Questions

Can't jump while sprinting 1 Answer

i'm trying to move the player horizontal and vertical in lanes but he keep shaking/vibrating 1 Answer

Jet-Pack Script 1 Answer

Turning to face movement direction 0 Answers

Animation not working correctly, character is going up 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