- Home /
The Sims-like movement script?
I need help making a script to make a character move around like in the sims. The one me and my programmer made just teleports the character to an unknown location. we are making a game called Reality, which is like The Sims. I would like it in C#.
Any Help?
edit: Here is the code:
using UnityEngine; using System.Collections;
public class ConstrainToTerrain : 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 () { if (Collision.gameObject.tag == "Terrain" ) { canMove = false; } //else //{ //canMove = true; //} }
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 == "Terrain")
{
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)
{
//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;
}
}
} }`enter code here`
Unity Answers isn't a job posting site like asking someone to code something for you. Ins$$anonymous$$d, provide a snippet of code, or something to help comment on.
For collaboration like you're asking, better to post in the Unity forums:
but I do have code, Chad$$anonymous$$.
I posted it just now.
Answer by DaveA · Jul 18, 2012 at 08:57 PM
Maybe these?
http://unifycommunity.com/wiki/index.php?title=Click_To_Move
http://unifycommunity.com/wiki/index.php?title=ClickCharacterController
Your answer
![](https://koobas.hobune.stream/wayback/20220613074348im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Camera Script - Bird's Eye View 1 Answer
Character getting stuck on curbs.. 0 Answers
C#:Unity:Having trouble reference Rigidbody2d from different script 2 Answers
Distribute terrain in zones 3 Answers