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 nreed132 · Mar 18, 2020 at 11:47 PM · movement scriptjumping

How can I fix my velocity breaking when I rotate the character?

I've made a script that takes my velocity and continues to have that velocity while I'm jumping. This worked fine until I made a camera controller. After the object rotates, it seems to move around randomly.. How can I make it so that the velocity works no matter what direction I'm facing?

Here's the script I'm using.

In the character itself:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     private Animator anim;
     float speed = 0;
     float sideways = 0;
     bool isOnGround;
     Vector3 vel = new Vector3(0, 0, 0);
     bool jumpCooldown = false;
     void Start()
     {
         anim = GetComponent<Animator>();
     }
     void Update()
     {
         Rigidbody rb = GetComponent<Rigidbody>();
 
         if (Input.GetKey(KeyCode.W))
         {
             speed += 0.1f;
         } else if (Input.GetKey(KeyCode.S))
         {
             speed -= 0.025f;
         } else
         {
             speed /= 2;
             if (speed < 0.1f && speed > -0.1f)
             {
                 speed = 0;
             }
         }
         if (Input.GetKey(KeyCode.LeftShift))
         {
             speed = Mathf.Clamp(speed, -1f, 1f);
         } else
         {
             speed = Mathf.Clamp(speed, -0.1f, 0.1f);
         }
 
 
         if (Input.GetKey(KeyCode.A))
         {
             sideways -= 0.1f;
         } else if (Input.GetKey(KeyCode.D))
         {
             sideways += 0.1f;
         } else
         {
             sideways /= 2;
             if (sideways < 0.1f && sideways > -0.1f)
             {
                 sideways = 0;
             }
         }
         if (Input.GetKey(KeyCode.LeftShift))
         {
             sideways = Mathf.Clamp(sideways, -1f, 1f);
         } else
         {
             sideways = Mathf.Clamp(sideways, -0.2f, 0.2f);
         }
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             if (isOnGround && !jumpCooldown)
             {
                 anim.SetTrigger("Jump");
                 vel = rb.velocity;
                 jumpCooldown = true;
                 Invoke("Cooldown", 1.0f);
             } else
             {
                 vel = new Vector3(0, 0, 0);
             }
         }
 
         //transform.Rotate(Vector3.up * Input.GetAxis("Mouse X"));
 
         if (!isOnGround)
         {
             transform.Translate(vel / 20);
         }
 
         if (speed > 0)
         {
             anim.SetBool("isWalking", true);
         } else
         {
             anim.SetBool("isWalking", false);
         }
 
         if (speed < 0)
         {
             anim.SetBool("isWalkingBackwards", true);
         } else
         {
             anim.SetBool("isWalkingBackwards", false);
         }
 
         if (speed > 0.5)
         {
             anim.SetBool("isRunning", true);
         } else
         {
             anim.SetBool("isRunning", false);
         }
 
         if (speed < -0.5)
         {
             anim.SetBool("isRunningBackwards", true);
         } else
         {
             anim.SetBool("isRunningBackwards", false);
         }
 
         if (sideways > 0)
         {
             anim.SetBool("isRight", true);
         } else
         {
             anim.SetBool("isRight", false);
         }
 
         if (sideways < 0)
         {
             anim.SetBool("isLeft", true);
         } else
         {
             anim.SetBool("isLeft", false);
         }
 
         if (sideways > 0.5)
         {
             anim.SetBool("isRunningRight", true);
         } else
         {
             anim.SetBool("isRunningRight", false);
         }
 
         if (sideways < -0.5)
         {
             anim.SetBool("isRunningLeft", true);
         } else
         {
             anim.SetBool("isRunningLeft", false);
         }
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.CompareTag("Ground"))
         {
             isOnGround = true;
         }
     }
 
     private void OnCollisionExit(Collision collision)
     {
         if (collision.gameObject.CompareTag("Ground"))
         {
             isOnGround = false;
         }
     }
 
     void Cooldown()
     {
         jumpCooldown = false;
     }
 }
 

‎‎‎‎‎‎‎

In the camera:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour
 {
     public Transform CameraTarget;
     private float x = 0.0f;
     private float y = 0.0f;
 
     private int mouseXSpeedMod = 5;
     private int mouseYSpeedMod = 5;
 
     public float MaxViewDistance = 15f;
     public float MinViewDistance = 1f;
     public int ZoomRate = 20;
     private int lerpRate = 5;
     private float distance = 3f;
     private float desireDistance;
     private float correctedDistance;
     private float currentDistance;
 
     public float cameraTargetHeight = 1.0f;
 
     //checks if first person mode is on
     private bool click = false;
     //stores cameras distance from player
     private float curDist = 0;
 
     // Use this for initialization
     void Start()
     {
         Vector3 Angles = transform.eulerAngles;
         x = Angles.x;
         y = Angles.y;
         currentDistance = distance;
         desireDistance = distance;
         correctedDistance = distance;
     }
 
     // Update is called once per frame
     void LateUpdate()
     {
         x += Input.GetAxis("Mouse X") * mouseXSpeedMod;
         y += Input.GetAxis("Mouse Y") * mouseYSpeedMod;
         y = ClampAngle(y, -15, 25);
         Quaternion rotation = Quaternion.Euler(y, x, 0);
 
         desireDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ZoomRate * Mathf.Abs(desireDistance);
         desireDistance = Mathf.Clamp(desireDistance, MinViewDistance, MaxViewDistance);
         correctedDistance = desireDistance;
 
         Vector3 position = CameraTarget.position - (rotation * Vector3.forward * desireDistance);
 
         RaycastHit collisionHit;
         Vector3 cameraTargetPosition = new Vector3(CameraTarget.position.x, CameraTarget.position.y + cameraTargetHeight, CameraTarget.position.z);
 
         bool isCorrected = false;
         if (Physics.Linecast(cameraTargetPosition, position, out collisionHit))
         {
             position = collisionHit.point;
             correctedDistance = Vector3.Distance(cameraTargetPosition, position);
             isCorrected = true;
         }
 
         //?
         //condicion ? first_expresion : second_expresion;
         //(input > 0) ? isPositive : isNegative;
 
         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * ZoomRate) : correctedDistance;
 
         position = CameraTarget.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -cameraTargetHeight, 0));
 
         transform.rotation = rotation;
         transform.position = position;
 
         //CameraTarget.rotation = rotation;
 
         float cameraX = transform.rotation.x;
 
         //sets CHARACTERS x rotation to match cameras x rotation
         CameraTarget.eulerAngles = new Vector3(cameraX, transform.eulerAngles.y, transform.eulerAngles.z);
         //checks if middle mouse button is pushed down
         if (Input.GetMouseButtonDown(2))
         {
             //if middle mouse button is pressed 1st time set click to true and camera in front of player and save cameras position before mmb.
             //if mmb is pressed again set camera back to it's position before we clicked mmb 1st time and set click to false
             if (click == false)
             {
                 click = true;
                 curDist = distance;
                 distance = distance - distance - 1;
             }
             else
             {
                 distance = curDist;
                 click = false;
             }
         }
 
     }
 
     private static float ClampAngle(float angle, float min, float max)
     {
         if (angle < -360)
         {
             angle += 360;
         }
         if (angle > 360)
         {
             angle -= 360;
         }
         return Mathf.Clamp(angle, min, max);
     }
 }
 

Thank you! Any help is greatly appreciated! I understand these are very long, so whoever even attempts is amazing!

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

126 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

Related Questions

How to prevent player from moving in air while jumping in place ?? 0 Answers

i am trying to make some movement for my character but i am having some issues when it comes to jumping 1 Answer

Dampening Movement while in the Air 0 Answers

How to make your character jump? 1 Answer

Character floats down after jumping 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