• 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 Entyro · Jul 05, 2014 at 02:54 PM · transformplayerball

Player flies PROBLEM

Hi!

I have a script for a rolling ball player. The problem I have is when I try to go backwards the ball flies up in the air. That's because in my script I have that the movement is changed where the camera is looking. Like if I'm holding the "W" key and spin around the camera the player should go where the camera is facing.

The script:

 var ballSpeed : float;
 var cameraObject : GameObject;
 
 function Update () 
 { 
 //Forward
     var vertical : float = Input.GetAxis ("Vertical");
         rigidbody.AddForce(cameraObject.transform.forward * ballSpeed * vertical * Time.deltaTime);
 //Left, right, back
     var horizontal : float = Input.GetAxis ("Horizontal");
         rigidbody.AddForce(cameraObject.transform.right * ballSpeed * horizontal * Time.deltaTime);
 }



Does anyone know how to fix this problem?

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
1

Answer by JusticeAShearing · Jul 05, 2014 at 03:31 PM

The length of this answer may look discouraging, but the majority of it is code to be copied and pasted.

I don't know why your script does that, but I do have an alternative solution. Use my first (java) script, apply it to the character. Then use my two camera java-scripts on the camera. This will allow your character to move with the WASD combination, and give the camera a smooth following of your character.

Here's the code:

 //Movement.js
 
 var speed : float = 6.0;
 var twiceSpeed : float = 10.0;
 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 var rotateSpeed : float = 3.0;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
 function Update() {
     var controller : CharacterController = GetComponent(CharacterController);
     if (controller.isGrounded) {
         //Grounded, so recalculate
         //Move directly from axes
         moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
         
         //Rotation Code
         transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
         
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         
         if (Input.GetButton ("Jump")) {
             moveDirection.y = jumpSpeed;
         }
         
         //Animation Code
         if (Input.GetKeyDown("w"))
         {
             animation.Play("Full Walk");
         }
         else if (Input.GetKeyUp("w"))
         {
             animation.Stop();
             animation.Play("Idle");
         }
     }
     
     //Apply Gravity
     moveDirection.y -= gravity * Time.deltaTime;
     
     //Move Controller
     controller.Move(moveDirection * Time.deltaTime);
 }

Now, the camera scripts:

 //Smooth Follow.js
 
 /*
 This camera smoothes out rotation around the y-axis and height.
 Horizontal Distance to the target is always fixed.
 
 There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
 
 For every of those smoothed values we calculate the wanted value and the current value.
 Then we smooth it using the Lerp function.
 Then we apply the smoothed values to the transform's position.
 */
 
 // The target we are following
 var target : Transform;
 // The distance in the x-z plane to the target
 var distance = 10.0;
 // the height we want the camera to be above the target
 var height = 5.0;
 // How much we 
 var heightDamping = 2.0;
 var rotationDamping = 3.0;
 
 // Place the script in the Camera-Control group in the component menu
 @script AddComponentMenu("Camera-Control/Smooth Follow")
 
 
 function LateUpdate () {
     // Early out if we don't have a target
     if (!target)
         return;
     
     // Calculate the current rotation angles
     var wantedRotationAngle = target.eulerAngles.y;
     var wantedHeight = target.position.y + height;
         
     var currentRotationAngle = transform.eulerAngles.y;
     var currentHeight = transform.position.y;
     
     // Damp the rotation around the y-axis
     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 
     // Damp the height
     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 
     // Convert the angle into a rotation
     var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
     
     // Set the position of the camera on the x-z plane to:
     // distance meters behind the target
     transform.position = target.position;
     transform.position -= currentRotation * Vector3.forward * distance;
 
     // Set the height of the camera
     transform.position.y = currentHeight;
     
     // Always look at the target
     transform.LookAt (target);
 }

And the other camera script:

 //CameraRotation.js
 
 #pragma strict
 
 function Update () {
 
 if (Input.GetKey(KeyCode.UpArrow)) transform.Translate(0, 0, -1);
 
 if (Input.GetKey(KeyCode.DownArrow)) transform.Translate(0, 0, 1);
 
 if (Input.GetKey(KeyCode.RightArrow)) transform.Rotate(0, 1, 0);
 
 if (Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(0, -1, 0);
 
 }

I hope that this helps you.

Also, check your colliders. That has caused me to have objects flying through the fabric of Unity Time and Space many times before now.

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 Entyro · Jul 05, 2014 at 05:10 PM 0
Share

I get an error when I try to move. "Object reference not set to an instance of an object", and when I check where the problem is it goes to the line "if (controller.isGrounded) {". Do you know what the problem is?

avatar image JusticeAShearing · Jul 07, 2014 at 03:53 PM 0
Share

It it trying to find a character controller, which you evidently do not have attached to whomever has this script attached to them in your game.

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Third person movement similar to Max Payne 1 Answer

Raycast Destroys player. 1 Answer

Having trouble with player.transform 1 Answer

Acces to other object trigger thru other object script 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