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 waqas142 · Jul 19, 2018 at 08:53 PM · physicsscript.

Nedd help with character controller

Hi guys i am new to unity. i am working on a game where the character which is a ball needs to move with the help of mouse as i move mouse forward the ball goes forward and vise verca and same works for the directions mean the ball can go left and right with the help of a mouse. currently i am doing it with the help of input keys . a script would be appreciated. link for controller https://www.youtube.com/watch?v=ijGD49NmA7E

Player Controller

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public float speed;

 public GameObject camera;

 void FixedUpdate()
 {
     float movementHorizontal = Input.GetAxis("Horizontal");
     float movementVertical = Input.GetAxis("Vertical");

     Vector3 movementVector = new Vector3(movementHorizontal, 0.0f, movementVertical);
         movementVector = camera.transform.TransformDirection(movementVector);

     GetComponent<Rigidbody>().AddForce(movementVector * speed * Time.deltaTime);
 }

}

CameraController.cs

using UnityEngine; using System.Collections;

public class PlayerCameraController : MonoBehaviour {

 private GameObject player;
 private Vector3 offset;
 private float boardTiltMax = 15f; // Maximum angle to tilt the camera to fake the level tilting
 private Vector3 desiredPosition;
 private GameObject desiredPositionObject;

 private float rotationDamping = 10f;
 private float movementDamping = 150f;
 private float turnSpeed = 300f;

 private float turnAngle = 0.0f;

 // Use this for initialization
 void Start()
 {
     offset = transform.position;
     desiredPosition = transform.position;
     desiredPositionObject = new GameObject("cameraFollow");
     DontDestroyOnLoad(desiredPositionObject);
     desiredPositionObject.transform.position = transform.position;

     // fine the player object
     player = GameObject.Find("Player");

     if (player == null)
     {

         Debug.LogError("Could not find object \"Player\" ! Aborting GameCamera load.");
         DestroyImmediate(gameObject);
     }
 }

 void Update()
 {
     // Rotate the camera around the ball to adjust movement when Q or E are pressed (left/right movement)
     turnAngle += Input.GetAxis("Turn") * turnSpeed * Time.deltaTime;
 }

 void LateUpdate()
 {
     // find the ZX direction from the player to the camera
     var heading = transform.position - player.transform.position;
     heading.y = 0f;
     var distance = heading.magnitude;
     var direction = heading / distance;

     // Find the right vector for the diretion
     var rotationVectorRight = Vector3.Cross(direction, Vector3.up);

     // Move the camera
     desiredPositionObject.transform.position = player.transform.position + offset;

     // Rotate around the players Y axis by the turn value
     desiredPositionObject.transform.RotateAround(player.transform.position, Vector3.up, turnAngle);

     // Deal with forward/backward board rotation
     desiredPositionObject.transform.RotateAround(player.transform.position, rotationVectorRight, -Input.GetAxisRaw("Vertical") * boardTiltMax);

     // Ensure we're looking at the player before the roll rotation is applied
     desiredPositionObject.transform.LookAt(player.transform.position);

     // Apply the roll rotation
     desiredPositionObject.transform.RotateAround(desiredPositionObject.transform.position, direction, -Input.GetAxisRaw("Horizontal") * boardTiltMax);

     // Lerp the cameras position to match the target object
     transform.position = Vector3.Slerp(transform.position, desiredPositionObject.transform.position, Time.deltaTime * movementDamping);

     // Lerp the cameras rotation to match the target object
     transform.rotation = Quaternion.Lerp(transform.rotation,
                                           desiredPositionObject.transform.rotation,
                                           Time.deltaTime * rotationDamping);

     // Re-center the camera on the object to account for new roll rotation
     CenterCameraOnTarget();

 }

 private void CenterCameraOnTarget()
 {
     Plane plane = new Plane(transform.forward, player.transform.position);
     Ray ray = GetComponent<Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0.0f));
     float distance;
     plane.Raycast(ray, out distance);

     var point = ray.GetPoint(distance);
     var offset = player.transform.position - point;
     transform.position += offset;

} }

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

219 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 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

Structure Support System like fortnite 0 Answers

How to make flips, backflips like in game Backflip madness 2 Answers

Flipping Sprite with Box Collider attached 1 Answer

How to procedurally make a crisp object look smooth and curved. 2 Answers

How to create first person controller and (Script + Physic) 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