• 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 HydraGames_ · Feb 23 at 09:05 AM · jumping

Jumping wont work with my 3rd person movement code

I used the Brackeys tutorial to make 3rd person movement code and when I tried adding some code for jumping into it the jump would be weirdly low all the time and slow down the speed of the character when I jump. Here's the code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ThirdPersonMovement : MonoBehaviour { public CharacterController controller; public Transform cam; public float speed = 3f; public float turnSmoothTime = 0.1f; float turnSmoothVelocity; [SerializeField] private Animator animator; [SerializeField] private float jumpSpeed = 20f;

 [SerializeField] private float jumpHeight = 1f;
 [SerializeField] private float gravity = 1f;
 private Vector3 direction = Vector3.zero;
 private float yVelocity;
 private bool canDoubleJump;

 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     float horizontal = Input.GetAxisRaw("Horizontal");
     float vertical = Input.GetAxisRaw("Vertical");
     Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
     Vector3 jumpVelocity = direction * jumpSpeed;

     if (controller.isGrounded)
     {
         canDoubleJump = false;
         if (Input.GetKeyDown(KeyCode.Space))
         {
             yVelocity = jumpHeight; 
             canDoubleJump = true;
         }
     }
     else
     {
         yVelocity -= gravity;
         if (Input.GetKeyDown(KeyCode.Space))
         {
             if (canDoubleJump == true)
             {
                 yVelocity += jumpHeight;
                 canDoubleJump = false;
             }
         }
     }

     jumpVelocity.y = yVelocity;


     if (direction.magnitude >= 0.1f)
     {
         float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
         float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
         transform.rotation = Quaternion.Euler(0f, angle, 0f);
         animator.SetBool("Run", true);

         Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
         controller.Move(moveDir.normalized * Time.deltaTime);
     }
     else
     {
         animator.SetBool("Run", false);
     }
     controller.Move(jumpVelocity.normalized * Time.deltaTime);
 }

}

Comment
Add comment · Show 1
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
avatar image logicandchaos · Feb 23 at 01:28 PM 0
Share

I'm not entirely sure but it looks like you go through a lot of calculations for jumpVelocity, but then you use a normalized version of it, normalized means it equals one.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GeroNL · Feb 23 at 11:20 PM

Try this :

  // make it into private
  private Vector3 moveDir = Vector3.zero;
  -------
  //
  jumpVelocity.y = yVelocity;
  
  if (direction.magnitude >= 0.1f)
  {
      float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
      float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
      transform.rotation = Quaternion.Euler(0f, angle, 0f);
      animator.SetBool("Run", true);
      moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
      // comment it, just make it into ona all values of movement
      // controller.Move(moveDir.normalized * Time.deltaTime);
  }
  else
  {
      animator.SetBool("Run", false);
  }
  // i don't know with you normalized used, cause it make the value back to max 1, like if in vector3 x=1, and z=3, when you normalized,
  //  it will be x = 1/3 and z = 3/3. but if it already move, these code maybe work
  controller.Move((jumpVelocity.normalized+moveDir.normalized) * Time.deltaTime);


Hope it help

Comment
Add comment · Show 2 · 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
avatar image HydraGames_ · Feb 24 at 04:33 AM 0
Share

Now the chickens constantly moving in the direction that it's facing and still not jumping, I probably just made the controls all weird and will change how they are, but I appreciate it!

avatar image GeroNL HydraGames_ · Feb 24 at 06:12 AM 0
Share

Sorry, i'm wrong, try to just change your code in this line :

 controller.Move(jumpVelocity.normalized * Time.deltaTime);

to be:

 controller.Move(new Vector3(horizontal ,jumpVelocity.normalized , vertical)* Time.deltaTime);


And try to erase the normalized, Hope it help.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

115 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

Related Questions

How can I stop the fps controller jumping up slopes? 1 Answer

Character Controller / Charactor Motor has no inputs? 1 Answer

please help me!!!!!!!!!!!!!!!!!!!! 2 Answers

Rigidbody, jumping and gravity help 0 Answers

How do I create variable Jump Height for a platformer? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges