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 Ben 20 · Feb 09, 2011 at 04:20 PM · charactercontroller

I need mouse look and CharacterController.Move to work on the same object

I have a player model attached to a game object that has the following script on it:

var speed = 4.0;

function Start() { CheckRotation(); }

function Update () { CheckRotation(); CheckMovement(); //WASD Keys are default }

function CheckMovement() { var controller : CharacterController = GetComponent(CharacterController);

 moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 moveDirection = transform.TransformDirection(moveDirection);
 moveDirection *= speed;

 controller.Move(moveDirection * Time.deltaTime);

}

function CheckRotation() { var playerPlane = new Plane(Vector3.up, transform.position); var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hitdist = 0.0;

 if (playerPlane.Raycast (ray, hitdist)) 
 {
     var targetPoint = ray.GetPoint(hitdist);
     var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 4.0 * Time.deltaTime); //This isn't instant
 }

}

The problem with this script is that the player always moves towards the mouse. I need the player to be able to push W and move north, not forward. If anyone has any ideas that would be great.

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 Ben 20 · Feb 09, 2011 at 04:30 PM 0
Share

This game is for a Top Down shooter by the way. hence why W is north ins$$anonymous$$d of forward.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by GesterX · Feb 09, 2011 at 05:20 PM

I am currently working on a top down game. Here is my code to rotate dependant on the mouse but move the player as you describe ("W" always goes north etc). It is adapted from the Evac City tutorial but it's not a direct copy. This will only work if you have a rigidbody attached to your character. You must also have the player tagged as Player and the mainCamera tagged as MainCamera. Finally to make it "feel" better: Turn off Gravity in your Rigidbody Set the rigidbody mass to 6 and drag to 12. Set the moveSpeed to 25000 in the inspector. In Edit>Project Settings>Input set the gravity and sensitivity on the horizontal and vertical axis to 1000.

public var moveSpeed = 100f;

//input variables private var inputRotation : Vector3; private var inputMovement : Vector3;

// calculation variables private var tempVector : Vector3; private var tempVector2 : Vector3;

function Start() { thePlayer = GameObject.FindWithTag ("Player"); theCamera = GameObject.FindWithTag ("MainCamera"); }

function Update () { FindPlayerInput(); ProcessMovement(); Camera Control(); }

function FindPlayerInput() { //vector for movement inputMovement = Vector3( Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") );

 // the position of the middle of the screen
 tempVector2 = Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f); 
 // find the position of the moue
 tempVector = Input.mousePosition; 

 //keeps the game 2D
 tempVector.z = tempVector.y;
 tempVector.y = 0;

 // the direction we want look is from the centre of the screen to where the mouse is
 inputRotation = tempVector - tempVector2;

}

function ProcessMovement() { rigidbody.AddForce (inputMovement.normalized moveSpeed Time.deltaTime); transform.rotation = Quaternion.LookRotation(inputRotation); transform.eulerAngles = Vector3(0,transform.eulerAngles.y + 180,0); transform.position = Vector3(transform.position.x,transform.position.y,transform.position.z); }

function CameraControl() { theCamera.transform.position = Vector3(transform.position.x, 30,transform.position.z); theCamera.transform.eulerAngles = Vector3(90,0,0); }

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 Ben 20 · Feb 09, 2011 at 05:33 PM 0
Share

I'll take a look. I got something kind of work to using positions relative to the camera, but there is a lot of sliding in $$anonymous$$e. Thanks for the code! :)

avatar image GesterX · Feb 09, 2011 at 05:36 PM 0
Share

No problem. Be sure to mark the answer correctly if it solves your problem

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

No one has followed this question yet.

Related Questions

How to use character controller to push down hinge joint properly 1 Answer

3rd person character movement problems 1 Answer

Gravity and Jumping for a Character Controller 1 Answer

No slipping off edges with Character Controller 0 Answers

jet pack physics with character controller 1 Answer


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