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 Aggressor · May 10, 2012 at 01:52 PM · character movementrtscollosion

Stopping 3rd Person Controller When It Hits Raised Terrtain

Hello!

I am working on a top down RTS game. I have created a click to move script for my 3rd person character and I've come up against a problem.

The character you control is stuck in a big crater and he's not supposed to be able to leave this crater. Currently I am using raycasting to pick a point on the terrain with a mouse click and the character turns and moves there.

However, when I click somewhere outside the terrain, my character moves through it. If I add a rigidbody it does stop when it hits the wall --- but it bounces off randomly and uncontrollably.

Is there a way to have my character just stop moving if he his the terrain wall? Here is my code:

 using UnityEngine;
 using System.Collections;

public class ClickCharacterController : MonoBehaviour

{

 bool canMove = false;
 public int speed = 5;
 RaycastHit hit; //Create an object to store all the raycast data
 Vector3 groundedHitPoint;


 void Start()
 {}
 
 
 void OnCollisionEnter ( Collision c )
 {
     //Debug.Log("Hitting");
     if ( c.gameObject.tag == "Ground" )
     {
       // Debug.Log("Terrain Collision");
        canMove = false;
     }
     else Debug.Log("Sorry Charlie.");
    
 }


 void Update() 
 {
     if(Input.GetMouseButton(1))
         {  
          //create a new ray from the source point from the camera to the mouse position
          Ray ray = (Camera.main.ScreenPointToRay(Input.mousePosition));
 
 
          //ray is the origin and vector, and out hit is a vector, passing by reference so it needs out keyword
             if (Physics.Raycast(ray, out hit)) 
          {
                 //Debug.Log("Hit: " + hit.transform.name);
               if (hit.transform.name == "Ground")
                  {
                    Debug.DrawLine(Camera.main.transform.position, hit.point);
                  }
           //have the cube turn to face the point
 
           groundedHitPoint = hit.point;
           groundedHitPoint.y = 1;
           transform.LookAt(groundedHitPoint);
           //Debug.Log("groundedhitpoint is : " + groundedHitPoint);
           canMove = true;
            }
 
        }
     if (canMove)
     {
        //OnCollisionEnter();
        //this moves an object
        transform.Translate(Vector3.forward*Time.deltaTime*speed);
        //Debug.Log ("Vector3.forward is: " + Vector3.forward);
        //GameObject.FindWithTag("ScavengerSprite").transform.position.y.Equals(1000);
 
        float  distance = Vector3.Distance(transform.position, groundedHitPoint);
        if (distance <= 0.1F)
        {
          canMove = false;
        }
     }
 
 }
 
 
 

}

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RTS style terrain changer. 2 Answers

RTS Regions 0 Answers

How to assign a game object with a raycast? 1 Answer

RTS Unit Scale Question: Will Physics Work? 1 Answer

RTS camera help 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