Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 NeoKuro · Jan 28, 2015 at 11:30 AM · c#rotationmovementspaceship

What is the cause of the distorted movement of my character?

I've been working on making a movement script for a 3D character (in space but only moving along the X/Z plane for now - Left right, forward back etc), the character moves independently of the camera (so the camera stays where it is/whatever position the player put it in, whilst the character rotates). At present, the camera isn't my focus, I currently use a modified version of the 'MouseOrbit' script for that, but will modify further to what I need.

The issue I have, well theres two, is;

Mainly, my character will only rotate 180 degrees. And its only in the negative direction (I have to press 'A', pressing 'D' will do nothing until I have first rotated in the negative X direction). I've not been able to figure out why. The script (below) is placed in the 'Player' prefab with the actual mesh model and camera is within it also (the 'Player' reference/prefab only has the position, character controller and the mentioned script);

The other issue I have is the movement is skewed noticably. When moving with a rotation of 0degrees (IE the direction you face when you spawn in) its fine. However if I rotate 90 degrees to the right, I find myself moving to the left (relatively - IE as if I was reversing at 0 degrees rotation). I had this problem before, and I was unable to fix it.

Below is the script I use for my movement. As movement is meant to be independent of the camera, any references to the camera position/rotation etc do nothing, I just haven't removed them yet.

Any help would be greatly appreciated

 using UnityEngine;
 using System.Collections;

 public class ShipControlScript : MonoBehaviour {

 private Vector3 position;
 private char userInput;
 public Vector3 moveDirection;
 public Vector3 targetDirection;
 public Vector3 newHeading;
 private bool rightClicked = false;
 private CollisionFlags collisionFlags;
 public Vector3 movement;
 public float moveSpeed = 0.0f;
 private float speedSmoothing = 10.0f;
 public float targetSpeed = 0.0f;
 private float impulse1Speed = 1.0f;
 private float impulse2Speed = 2.0f;
 private float impulse3Speed = 4.0f;
 private float impulse4Speed = 8.0f;
 private float rotateZ = 0.0f;
 private float rotateY = 0.0f;

 private string _CharacterState;

 public CharacterController character;
 public float rotateSpeed = 500.0f;

 // Use this for initialization
 void Start () 
 {
     position = character.transform.position;

     moveDirection = transform.TransformDirection (Vector3.forward);
 }
 
 // Update is called once per frame
 void Update () 
 {
     if (Input.GetMouseButtonDown (1))
         rightClicked = true;
     if (Input.GetMouseButtonUp (1))
         rightClicked = false;

     UpdateSmoothedMovementDirection ();

     movement = (moveDirection * moveSpeed);
     movement *= 1;
     //movement *= Time.deltaTime;
     transform.Translate (movement * Time.deltaTime);
     //print (character.transform.position);
     print("" + movement);


     transform.rotation = Quaternion.LookRotation(moveDirection);


 }


 void UpdateSmoothedMovementDirection()
 {
     Transform cameraTransform = Camera.main.transform;
     Vector3 forward = cameraTransform.TransformDirection (Vector3.forward);
     forward.y = 0;
     forward = forward.normalized;

     Vector3 right = cameraTransform.TransformDirection (forward.z, 0.0f, -forward.x);

     float h = Input.GetAxisRaw ("Horizontal");
     float v = Input.GetAxisRaw ("Vertical");

     rotateZ = (v * rotateSpeed * Mathf.Deg2Rad * Time.deltaTime) / 360;
     rotateY = (h * rotateSpeed * Mathf.Deg2Rad * Time.deltaTime) / 360;

     //newHeading.z = rotateZ;
     
     newHeading.x = rotateY;

     if (newHeading.z <= -360)
                     newHeading.z = 0;

     float curSmooth = speedSmoothing * Time.deltaTime;


     if (Input.GetKeyDown (KeyCode.Alpha1) || Input.GetKeyDown (KeyCode.Keypad1)) {

         _CharacterState = "Impulse 1";
         targetSpeed = impulse1Speed;
     } 
     else if (Input.GetKeyDown (KeyCode.Alpha2) || Input.GetKeyDown (KeyCode.Keypad2)) 
     {
         _CharacterState = "Impulse 2";
         targetSpeed = impulse2Speed;
     }
     else if (Input.GetKeyDown (KeyCode.Alpha3) || Input.GetKeyDown (KeyCode.Keypad3)) 
     {
         _CharacterState = "Impulse 3";
         targetSpeed = impulse3Speed;
     }
     else if (Input.GetKeyDown (KeyCode.Alpha4) || Input.GetKeyDown (KeyCode.Keypad4)) 
     {
         _CharacterState = "Impulse 4";
         targetSpeed = impulse4Speed;
     }
     /*
     if(_CharacterState == "Impulse 1")
         targetSpeed = impulse1Speed;
     else if(_CharacterState == "Impulse 2")
         targetSpeed = impulse2Speed;
     else if(_CharacterState == "Impulse 3")
         targetSpeed = impulse3Speed;
     else if(_CharacterState == "Impulse 4")
         targetSpeed = impulse4Speed;
     */
     targetDirection = -newHeading;// + forward;

     if (rightClicked) 
     {

     }
     else
         //targetDirection = moveDirection;
         
     moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);

     if(newHeading != Vector3.zero)
     {
         moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
         
         moveDirection = moveDirection.normalized;
     }

 }

}

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

2 People are following this question.

avatar image avatar image

Related Questions

(sprite2D) Movement like this game... 1 Answer

How do I create a spaceship 1 Answer

click to move workig script!! but pls help with rotation!! :( 1 Answer

Mouse Movement (Tracking) 1 Answer

How to make a RigidBody not go into ground when tilted foward? 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