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 Nekoluffy · Dec 22, 2016 at 05:50 AM · c#raycastcharacter controllercharacter movement

infamous first light run code help

Hey guys! c: Im a noob programmer, ive been trying to program for years. ive only just gotten the hang of it however. took me a while to rack my brain around concepts and stuff and i lacked he focus to put in the time. Im starting to get there now.

I created this movement script from scratch. Basically. if you hold the Z key and run into a wall, you can run up the wall. if you just in the air, you jump via the transform up direction no matter what wall youre on. If youre not grounded then your rotation resets to the world rotation.

Im trying to create a run system like Infamous first light. But starting in 2D so its a little easier first.

Heres whats messng me up. I cant get the raycast to work on the lefthand side to read for a wall on the left. And it might have something to do with the rotation, but I cant figure out what it is exactly. And when i jump in the air the rotation is always facing right. Also when running along the wall, sometimes when letting go of the Z key, it doesnt do what ts supposed to do and get off the wall, and change back rotations. Sometimes it just sticks to the wall as if youre still holding it.

Thanks for the help in advance, ill keep racking my brain around it and trying to rework the code in the mean time. PS: Ignore some of the unused variables such as lineRenderer, i use it to test the length of raycast and such.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public bool grounded;
     public bool facingRight;
 
     Vector3 playerUpVector;
     Vector3 originalUpVec;
     Quaternion originalRot;
     Vector3 surfaceNormal;
 
     Rigidbody rb;
     Renderer rend;
     LineRenderer[] lineRends;
 
     float gravity = 10;
     float walkSpeed = 10;
 
 
 
     // Use this for initialization
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         rend = GetComponent<Renderer>();
         playerUpVector = transform.up;
         originalUpVec = transform.up;
         rb.freezeRotation = true;
         lineRends = GetComponentsInChildren<LineRenderer>();
         originalRot = transform.rotation;
         facingRight = true;
 
     }
 
     // Update is called once per frame
 
     void Update()
     {
         float h = Input.GetAxis("Horizontal");
         Vector3 movement = transform.right * h * Time.deltaTime * walkSpeed;
         
 
         if (movement.magnitude > 0)
         {
             if (h < 0)
             {
                 facingRight = false;
                 transform.rotation = Quaternion.Euler(0, 180, 0);
                 movement = transform.right * -h * Time.deltaTime * walkSpeed;
                 rb.MovePosition(rb.position + movement);
             }
             else if (h > 0)
             {
                 facingRight = true;
                 transform.rotation = Quaternion.Euler(0, 0, 0);
                 rb.MovePosition(rb.position + movement);
             }
             
         }
         Debug.DrawRay(new Vector3(rend.bounds.extents.x,rend.bounds.center.y,0), new Vector3(rend.bounds.center.x - (rend.bounds.extents.x/2), -rend.bounds.extents.y - .4f,0),Color.red);
 
         
     }
 
     void FixedUpdate()
     {
         if (grounded && transform.up != Vector3.up)
         { 
         rb.AddForce(-gravity * rb.mass * playerUpVector);
         }
         if (grounded && Input.GetKeyDown(KeyCode.Space))
         {
             rb.AddForce(transform.up * 500 );
         }
 
 
         RaycastHit hit;
 
         if (Input.GetKey(KeyCode.Z))
         {
             if (facingRight && Physics.Raycast(transform.position, transform.right, out hit, .501f))
             {
                 surfaceNormal = hit.normal;
 
                 Quaternion newRot = Quaternion.LookRotation(transform.forward, surfaceNormal);
                 transform.rotation = newRot;
                 rb.useGravity = false;
                 playerUpVector = hit.normal;
             }
             if (!facingRight && Physics.Raycast(transform.position, transform.right, out hit, .51f))
             {
                 surfaceNormal = hit.normal;
 
                 Quaternion newRot = Quaternion.LookRotation(transform.forward, surfaceNormal);
                 transform.rotation = newRot;
                 rb.useGravity = false;
                 playerUpVector = hit.normal;
             }
 
         }
 
         if (Physics.Raycast(transform.position, -transform.up, out hit, .51f))
         {
             grounded = true;
         }
         else
         {
             grounded = false;
         }
 
         if (Input.GetKeyUp(KeyCode.Z) || !grounded)
         {
             rb.useGravity = true;
             transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y,originalRot.z);
             playerUpVector = originalUpVec;
             
         }
 
     }
 }
 
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
0
Best Answer

Answer by Nekoluffy · Dec 23, 2016 at 03:49 AM

So I figured out what to doooooo for the most part. I lacked an understanding of vectors and vector cross products. But after a little tinkering. I got it.

 if (Physics.Raycast(ray, out hit, castDist))
             {
             //Vector3 myForward = Vector3.Cross(transform.up, hit.normal);
                 Quaternion dstRot = Quaternion.LookRotation(transform.forward, hit.normal);
             transform.rotation = dstRot;
             }

Basically you take the transforms forward and the hit.normal and you put it into quaternion.lookrotation. This gives you the rotation you want your character to face in a 2D environment.

the hit.normal picks up the wall on the right. so it works great on 90 degree angles, might wanna adjust it for cases where the surface is more rounded like a cylinder.

Comment
Add comment · 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

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

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

Camera don't follow sphere as was expected 0 Answers

Problem making my character walk backward 0 Answers

Trying to figure out WallRiding, with Vector3.Cross 0 Answers

Faux Gravity Character Rotate Problem 0 Answers

wall check is always returning true even though the distance doesn't fit my parameters :( 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