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 /
  • Help Room /
avatar image
0
Question by KylonZarr · May 13, 2020 at 07:37 PM · controllerscripting beginnerbeginnermovement scriptplayer movement

Controller / movement script rotate while moving HELP

Hello, Im very new to unity but having a blast learning, however ive run into a issue i cannot solve, or maybe ive just overworked myself searching. I have wsad for moving forward back left and right (there stuck to set directions tho and thats effecting my issue but is not the main problem) i have jump set to space (working good altho the camera is a tiny bit off sometimes) and i have my q and e keys set to rotate the camera.. all with the animations set and working. SO my main problem, I can't rotate WHILE moving in any direction and i can't move in the direction i've rotated too.. I want to rotate the camera and move forward in that direction. Here is my script

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

public class DemoCharacterController : MonoBehaviour {

 public static DemoCharacterController instance;
 private void Awake() {
     instance = this;
 }
 Animator anim;
 PlayerCharacter player;
 CharacterController cc;

 private float movementSpeed;

 public float walkSpeed = 1.5f;
 public float runSpeed = 4;
 public float rotationSpeed = 10;

 public float jumpForce = 25;
 private float currentJumpForce;

 public void PickupItem(int itemId) {
     player.EquipItem(itemId);
 }


 public float jumpTime=1.5f; // how long the whole jump takes
 private float jumpTimer; 

 public float jumpStartDelay = 0.35f;// delay jump to match the animation
 private float jumpStartDelayTimer;

 public Vector3 gravity = new Vector3(0,-21,0);

 void Start() {
     player = GetComponent<PlayerCharacter>();
     anim = player.animator;
     cc = GetComponent<CharacterController>();
 }

 void Update() {

     Vector3 movementDirection = UpdateInput();
     UpdateJump();

     if (movementDirection != Vector3.zero) {
         cc.Move((transform.forward * movementSpeed + new Vector3(0, currentJumpForce, 0) + gravity) * Time.deltaTime);
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movementDirection), Time.deltaTime * rotationSpeed); // rotate character to movement direction
     } else {
         cc.Move((new Vector3(0, currentJumpForce, 0)+gravity) * Time.deltaTime); // call this even we're not moving to apply gravity and jump force
     }
     anim.SetFloat("MovementSpeed", cc.velocity.magnitude); // play idle/walk/run animation
 }


 Vector3 UpdateInput() {
     Vector3 movementDirection = Vector3.zero;
     if (Input.GetKey(KeyCode.W)) {
         movementDirection += Vector3.forward;
     }
     if (Input.GetKey(KeyCode.S)) {
         movementDirection += -Vector3.forward;
     }
     if (Input.GetKey(KeyCode.D)) {
         movementDirection += Vector3.right;
     }
     if (Input.GetKey(KeyCode.A)) {
         movementDirection += -Vector3.right;
     }

     if (Input.GetKey(KeyCode.LeftShift)) {
         movementSpeed = runSpeed;
     } else {
         movementSpeed = walkSpeed;
     }
     return movementDirection;
 }

 void UpdateJump() {
     if (cc.isGrounded) { // make sure we're touching the ground before jumping
         if (jumpTimer <= 0 && jumpStartDelayTimer <= 0) { // are we already jumping?
             if (Input.GetKey(KeyCode.Space)) {
                 jumpStartDelayTimer = jumpStartDelay;
                 anim.SetBool("Jumping", true);// start jump animation
             } else {
                 anim.SetBool("Jumping", false); // we are not jumping and not pressing space -> disable jump animation
             }
         }
     }
     if (jumpTimer > 0) {
         jumpTimer -= 1 * Time.deltaTime;
     }
     if (jumpStartDelayTimer > 0) { // delay the jump to match with animation 
         jumpStartDelayTimer -= 1 * Time.deltaTime;
         if (jumpStartDelayTimer <= 0) {
             currentJumpForce = jumpForce; // start actual jump
             jumpTimer = jumpTime;
         }
     }
     if (currentJumpForce > 0) {
         currentJumpForce += gravity.y * Time.deltaTime; // reduce jump force
         currentJumpForce = Mathf.Clamp(currentJumpForce, 0, float.MaxValue); // make sure jumpfore doesn't go negative
     }
     else if (Input.GetKey("e"))
     {
         transform.Rotate(Vector3.up * 45 * movementSpeed * Time.deltaTime);
     }
     if (Input.GetKey("q"))
     {
         transform.Rotate(-Vector3.up * 45 * movementSpeed * Time.deltaTime);
     }
 }

}

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

229 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

Related Questions

How to display random questions without repeating the previous? 2 Answers

3D character sliding movement,3D Character Sliding 1 Answer

Can't control jumping while moving horizontally 0 Answers

How to write a script for an existing button 1 Answer

[2D] How to stop 2 vectors from adding force on each other 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