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 RiddarN · Oct 11, 2015 at 03:09 PM · c#playercharactercharactercontrollervelocity

I want to add some Velocity controll to my CharacterController

Below I will post my Playercontroller script. In there I have a velocity variable that will controll the players movement.
I want to be able to make plattforms and other such things that will make my player fly up in the air when they interact and such.

To test the mechanics I am trying to add velocity when pressing jump, however; this is not taking effect.
The reason is because the velocity is reset to my "Horizontal / Vertical" * movementSpeed. How would I best go about on fixing this?

Sorry if I am unclear; I have a terrible headache and my main language is Swedish.

 using UnityEngine;
 using System.Collections;
 
 
 [RequireComponent (typeof(CharacterController))]
 public class FirstPersonController : MonoBehaviour {
     // Public variables
     public float movementSpeed = 10.0f;
     public float jumpSpeed = 10.0f;
     public float mouseSensitivity = 1.0f;
     public float maxHeadTilt = 75.0f;
 
     // Private Variables
     private Vector3 velocity = new Vector3(0,0,0);
     private float verticalRotation = 0.0f;
     private Vector3 movement = new Vector3(0,0,0);
 
     // Private Objects
     private CharacterController cc = null;
     private Camera _camera = null;
 
     // Use this for initialization
     void Start () {
         cc = GetComponent<CharacterController> ();
         _camera = GetComponentInChildren<Camera> ();
     }
     
     // Update is called once per frame
     void Update () {
 
         // Velocity
         velocity.x = Input.GetAxis ("Horizontal") * movementSpeed;
         velocity.y += Physics.gravity.y * Time.deltaTime;
         velocity.z = Input.GetAxis ("Vertical") * movementSpeed;
 
         // Inputs
         if (Input.GetButtonDown ("Jump")) 
         {
             velocity.z = jumpSpeed;
             velocity.y = jumpSpeed;
         }
 
         // MouseLook
         transform.eulerAngles += new Vector3(0, Input.GetAxis("Mouse X") * mouseSensitivity, 0);
         verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
         verticalRotation = Mathf.Clamp (verticalRotation, -maxHeadTilt, maxHeadTilt);
         _camera.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
 
         // Movement
         movement = new Vector3 (velocity.x, velocity.y, velocity.z);
         movement = transform.rotation * movement;
         cc.Move(movement * Time.deltaTime);
         Debug.Log (velocity);
     }
 }
 



Comment
Add comment · Show 2
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 Konomira · Oct 11, 2015 at 03:36 PM 0
Share

Do you want your character to be pushed both upwards and forwards when you jump? If not, you don't need

 velocity.z = jumpSpeed;
avatar image RiddarN Konomira · Oct 11, 2015 at 03:41 PM 0
Share

To test the mechanics I am trying to add velocity when pressing jump, however; this is not > taking effect.

I am using it to test. I am trying to make the character move forward, but only the .y value is being read. I am trying to make the character fly forward, but it is reset from the code above, making the velocity.z = Input.GetAxis ("Vertical") * movementSpeed.
basically, so far I can only modify my Y velocity.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Konomira · Oct 11, 2015 at 03:48 PM

Your moveSpeed is the same as your jumpSpeed, you wouldn't notice any difference.
if you were to have

 velocity.z += jumpSpeed;

instead of

 velocity.z = jumpSpeed;

then it should work.

Comment
Add comment · Show 4 · 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 RiddarN · Oct 11, 2015 at 03:58 PM 0
Share

Didn't work either.

http://i.imgur.com/25RnPFQ.png

This is the debug of my velocity. As you can see, it is being reset to 0 directly after pressing space. But that only applies to X and Z, not Y.

          velocity.x = Input.GetAxis ("Horizontal") * movementSpeed;
          velocity.y += Physics.gravity.y * Time.deltaTime;
          velocity.z = Input.GetAxis ("Vertical") * movementSpeed;


these are the lines that "ruins" it. But I don't know how to fix all of this.

avatar image Konomira RiddarN · Oct 11, 2015 at 04:09 PM 0
Share

Try this:

 if(Input.GetButtonDown("Jump"))
 {
     velocity.z = (Input.GetAxis ("Vertical") * movementSpeed) + jumpSpeed;
     velocity.y = jumpSpeed;
 }
avatar image RiddarN Konomira · Oct 11, 2015 at 04:20 PM 0
Share

Didn't quite work. Something that does give me the "desired effect" was this:
I added a new float, made the velocity automatically add the force into it's calculations at all times; then I just changed the float.

I can probably make an automated system that will strive to make my float equals to 0.

I appreciate your help! :D

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

restricting player movement with a path? 1 Answer

fps script not working 1 Answer

I have possibly a silly qustion regarding 2d Player Jump input 1 Answer

How do i ajust the player height acording to the height of the roof in a first person game? 1 Answer

How to crouch a FPS Controller? 2 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